-
-
Save robbyoconnor/9baf01a819d37dd6404e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Address < ActiveRecord::Base | |
has_many :phonings, :as => :phonable | |
has_many :phone_numbers, :through => :phonings | |
accepts_nested_attributes_for :phonings, :allow_destroy => true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test "By default adding the same phone number will create a new phone number row when adding a second phone number" do | |
a = Address.new(default_address( | |
"phonings_attributes"=> | |
{"1289233155995"=> | |
{"phone_number_attributes"=> | |
{"phone_number"=>"17032221234"}, | |
"_destroy"=>"false"} | |
} | |
)) | |
a.save! | |
assert_equal "Cville", Address.find(a.id).city | |
assert_equal "17032221234", Address.find(a.id).phone_numbers.first.phone_number | |
b = Address.new(default_address(:address1 => "234 Test", | |
"phonings_attributes"=> | |
{"1289233155995"=> | |
{"phone_number_attributes"=> | |
{"phone_number"=>"17032221234"}, | |
"_destroy"=>"false"} | |
} | |
)) | |
b.save! | |
assert_equal "Cville", Address.find(b.id).city | |
assert_equal "17032221234", Address.find(b.id).phone_numbers.first.phone_number | |
assert_equal @original_phone_number_count + 2, PhoneNumber.count | |
assert_equal 2, PhoneNumber.where(:phone_number => "17032221234").count | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test "When you override the autosave associated for the join table then you can point multiple addresses at the same phone number object." do | |
# overriding autosave_associated_records_for_phone_numbers | |
Phoning.send :define_method, :autosave_associated_records_for_phone_number do | |
if new_phone_number = PhoneNumber.where(:phone_number => phone_number.phone_number).first then | |
self.phone_number = new_phone_number | |
else | |
self.phone_number.save! | |
self.phone_number_id = self.phone_number.id | |
end | |
end | |
a = Address.new(default_address( | |
"phonings_attributes"=> | |
{"1289233155995"=> | |
{"phone_number_attributes"=> | |
{"phone_number"=>"17032221234"}, | |
"_destroy"=>"false"} | |
} | |
)) | |
a.save! | |
assert_equal "Cville", Address.find(a.id).city | |
assert_equal "17032221234", Address.find(a.id).phone_numbers.first.phone_number | |
b = Address.new(default_address(:address1 => "234 Test", | |
"phonings_attributes"=> | |
{"1289233155995"=> | |
{"phone_number_attributes"=> | |
{"phone_number"=>"17032221234"}, | |
"_destroy"=>"false"} | |
} | |
)) | |
b.save! | |
assert_equal "Cville", Address.find(b.id).city | |
assert_equal "17032221234", Address.find(b.id).phone_numbers.first.phone_number | |
assert_equal 3, PhoneNumber.count | |
assert_equal 1, PhoneNumber.where(:phone_number => "17032221234").count | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person < ActiveRecord::Base | |
has_many :phonings, :as => :phonable | |
has_many :phone_numbers, :through => :phonings | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PhoneNumber < ActiveRecord::Base | |
has_many :phonings, :dependent => :destroy | |
has_many :addresses, :through => :phonings, :source => :phonable, :source_type => "Address" | |
has_many :people, :through => :phonings, :source => :phonable, :source_type => "Person" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Phoning < ActiveRecord::Base | |
belongs_to :phone_number, :autosave => true | |
belongs_to :phonable, :polymorphic => true | |
accepts_nested_attributes_for :phone_number | |
def autosave_associated_records_for_phone_number | |
if new_phone_number = PhoneNumber.where(:phone_number => phone_number.phone_number).first then | |
self.phone_number = new_phone_number | |
else | |
self.phone_number.save! | |
self.phone_number_id = self.phone_number.id | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Phoning < ActiveRecord::Base | |
belongs_to :phone_number, :autosave => true | |
belongs_to :phonable, :polymorphic => true | |
accepts_nested_attributes_for :phone_number | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment