From df0821b2b856bbf24cf7ac53643147ac1f796c8c Mon Sep 17 00:00:00 2001 From: gchq Date: Wed, 13 Jul 2011 23:53:07 +0200 Subject: [PATCH] FogBugz #62: move address to a separate model/table. --- app/models/address.rb | 3 + app/models/contact.rb | 1 + db/migrate/20110713200056_create_addresses.rb | 49 ++++++ db/schema.rb | 144 +++++++++--------- test/fixtures/addresses.yml | 15 ++ test/unit/address_test.rb | 8 + 6 files changed, 152 insertions(+), 68 deletions(-) create mode 100644 app/models/address.rb create mode 100644 db/migrate/20110713200056_create_addresses.rb create mode 100644 test/fixtures/addresses.yml create mode 100644 test/unit/address_test.rb diff --git a/app/models/address.rb b/app/models/address.rb new file mode 100644 index 0000000..56351f1 --- /dev/null +++ b/app/models/address.rb @@ -0,0 +1,3 @@ +class Address < ActiveRecord::Base + has_many :contacts +end diff --git a/app/models/contact.rb b/app/models/contact.rb index a406295..9c9e041 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -1,6 +1,7 @@ class Contact < ActiveRecord::Base has_many :emails, :dependent => :destroy has_many :phones, :dependent => :destroy + has_one :address accepts_nested_attributes_for :emails, :reject_if => lambda { |a| a[:address].blank? }, :allow_destroy => true accepts_nested_attributes_for :phones, :reject_if => lambda { |a| a[:nr].blank? }, :allow_destroy => true diff --git a/db/migrate/20110713200056_create_addresses.rb b/db/migrate/20110713200056_create_addresses.rb new file mode 100644 index 0000000..1e77c91 --- /dev/null +++ b/db/migrate/20110713200056_create_addresses.rb @@ -0,0 +1,49 @@ +class CreateAddresses < ActiveRecord::Migration + def self.up + create_table :addresses do |t| + t.string :street + t.string :housenr + t.string :zipcode + t.string :place + t.string :country + + t.timestamps + end + + # There is no such thing as relevant production data, yet. + # Thus, no need to care about migrating existing data. + remove_column :contacts, :street + remove_column :contacts, :housenr + remove_column :contacts, :plz + remove_column :contacts, :place + remove_column :contacts, :country + + # Add FK reference + change_table :contacts do |t| + t.references :address + end + # Add the foreign key + execute <<-SQL + ALTER TABLE contacts + ADD CONSTRAINT fk_contacts_addresses + FOREIGN KEY (address_id) + REFERENCES addresses(id) + SQL + + # Force ActiveRecord to flush the cache and re-read the column information. + Contact.reset_column_information + end + + def self.down + execute "ALTER TABLE contacts DROP FOREIGN KEY fk_contacts_addresses" + remove_column :contacts, :address_id + + add_column :contacts, :country, :string + add_column :contacts, :place, :string + add_column :contacts, :plz, :string + add_column :contacts, :housenr, :string + add_column :contacts, :street, :string + + drop_table :addresses + end +end diff --git a/db/schema.rb b/db/schema.rb index 62ecd7b..e29b4a8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,68 +1,76 @@ -# This file is auto-generated from the current state of the database. Instead -# of editing this file, please use the migrations feature of Active Record to -# incrementally modify your database, and then regenerate this schema definition. -# -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). -# -# It's strongly recommended to check this file into your version control system. - -ActiveRecord::Schema.define(:version => 20110607192020) do - - create_table "contacts", :force => true do |t| - t.string "firstname" - t.string "lastname" - t.string "street" - t.string "housenr" - t.string "plz" - t.string "place" - t.string "country" - t.datetime "created_at" - t.datetime "updated_at" - t.integer "birth_day" - t.integer "birth_month" - t.integer "birth_year" - t.integer "user_id" - end - - create_table "emails", :force => true do |t| - t.string "desc" - t.string "address" - t.datetime "created_at" - t.datetime "updated_at" - t.integer "contact_id" - end - - add_index "emails", ["contact_id"], :name => "fk_emails_contacts" - - create_table "phones", :force => true do |t| - t.string "desc" - t.string "nr" - t.datetime "created_at" - t.datetime "updated_at" - t.integer "contact_id" - end - - add_index "phones", ["contact_id"], :name => "fk_phones_contacts" - - create_table "users", :force => true do |t| - t.string "email", :default => "", :null => false - t.string "encrypted_password", :limit => 128, :default => "", :null => false - t.string "reset_password_token" - t.datetime "remember_created_at" - t.integer "sign_in_count", :default => 0 - t.datetime "current_sign_in_at" - t.datetime "last_sign_in_at" - t.string "current_sign_in_ip" - t.string "last_sign_in_ip" - t.datetime "created_at" - t.datetime "updated_at" - end - - add_index "users", ["email"], :name => "index_users_on_email", :unique => true - add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true - -end +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended to check this file into your version control system. + +ActiveRecord::Schema.define(:version => 20110713200056) do + + create_table "addresses", :force => true do |t| + t.string "street" + t.string "housenr" + t.string "zipcode" + t.string "place" + t.string "country" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "contacts", :force => true do |t| + t.string "firstname" + t.string "lastname" + t.datetime "created_at" + t.datetime "updated_at" + t.integer "birth_day" + t.integer "birth_month" + t.integer "birth_year" + t.integer "user_id" + t.integer "address_id" + end + + add_index "contacts", ["address_id"], :name => "fk_contacts_addresses" + + create_table "emails", :force => true do |t| + t.string "desc" + t.string "address" + t.datetime "created_at" + t.datetime "updated_at" + t.integer "contact_id" + end + + add_index "emails", ["contact_id"], :name => "fk_emails_contacts" + + create_table "phones", :force => true do |t| + t.string "desc" + t.string "nr" + t.datetime "created_at" + t.datetime "updated_at" + t.integer "contact_id" + end + + add_index "phones", ["contact_id"], :name => "fk_phones_contacts" + + create_table "users", :force => true do |t| + t.string "email", :default => "", :null => false + t.string "encrypted_password", :limit => 128, :default => "", :null => false + t.string "reset_password_token" + t.datetime "remember_created_at" + t.integer "sign_in_count", :default => 0 + t.datetime "current_sign_in_at" + t.datetime "last_sign_in_at" + t.string "current_sign_in_ip" + t.string "last_sign_in_ip" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "users", ["email"], :name => "index_users_on_email", :unique => true + add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true + +end diff --git a/test/fixtures/addresses.yml b/test/fixtures/addresses.yml new file mode 100644 index 0000000..a30456f --- /dev/null +++ b/test/fixtures/addresses.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + street: MyString + housenr: MyString + zipcode: MyString + place: MyString + country: MyString + +two: + street: MyString + housenr: MyString + zipcode: MyString + place: MyString + country: MyString diff --git a/test/unit/address_test.rb b/test/unit/address_test.rb new file mode 100644 index 0000000..b824ec9 --- /dev/null +++ b/test/unit/address_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class AddressTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end