FogBugz #62: move address to a separate model/table.

This commit is contained in:
gchq 2011-07-13 23:53:07 +02:00
parent 8c89652fbb
commit df0821b2b8
6 changed files with 152 additions and 68 deletions

3
app/models/address.rb Normal file
View file

@ -0,0 +1,3 @@
class Address < ActiveRecord::Base
has_many :contacts
end

View file

@ -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

View file

@ -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

View file

@ -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

15
test/fixtures/addresses.yml vendored Normal file
View file

@ -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

View file

@ -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