spec/support/fake_helper.rb in cfoundry-0.5.1.rc4 vs spec/support/fake_helper.rb in cfoundry-0.5.1.rc5

- old
+ new

@@ -37,10 +37,11 @@ end def fake(attributes = {}) fake_attributes(attributes).each do |k, v| send(:"#{k}=", v) + setup_reverse_relationship(v) end self end @@ -112,10 +113,32 @@ def default_fakes self.class.defaults.merge( :guid => random_string("fake-#{object_name}-guid")) end + def setup_reverse_relationship(v) + if v.is_a?(Array) + v.each do |x| + setup_reverse_relationship(x) + end + + return + end + + return unless v.is_a?(Model) + + relation, type = find_reverse_relationship(v) + + v.client = @client + + if type == :one + v.send(:"#{relation}=", self) + elsif type == :many + v.send(:"#{relation}=", v.send(relation) + [self]) + end + end + def find_reverse_relationship(v) singular = object_name plural = plural_object_name v.class.to_one_relations.each do |attr, opts| @@ -196,6 +219,18 @@ def self.define_base_client_methods(&blk) FakeBase.module_eval(&blk) end end + Model.objects.each_value do |klass| + klass.to_many_relations.each do |plural, _| + Fake.define_many_association(klass, plural) + end + + FakeClient.class_eval do + plural = klass.plural_object_name + + attr_writer plural + Fake.define_many_association(self, plural) + end + end end