Permalink
Browse files

factory methods should not have side effects.

Move model mutation to the methods that are called on the model.
  • Loading branch information...
1 parent 5f169b2 commit eb8d48eb3ea27b7a7470eb9668748abd4914386b @tenderlove tenderlove committed Jul 23, 2013
@@ -223,7 +223,8 @@ def composed_of(part_id, options = {})
reader_method(name, class_name, mapping, allow_nil, constructor)
writer_method(name, class_name, mapping, allow_nil, converter)
- ActiveRecord::Reflection.create(:composed_of, part_id, nil, options, self)
+ reflection = ActiveRecord::Reflection.create(:composed_of, part_id, nil, options, self)
+ Reflection.add_reflection self, part_id, reflection
end
private
@@ -1205,7 +1205,8 @@ module ClassMethods
# has_many :reports, -> { readonly }
# has_many :subscribers, through: :subscriptions, source: :user
def has_many(name, scope = nil, options = {}, &extension)
- Builder::HasMany.build(self, name, scope, options, &extension)
+ reflection = Builder::HasMany.build(self, name, scope, options, &extension)
+ Reflection.add_reflection self, name, reflection
end
# Specifies a one-to-one association with another class. This method should only be used
@@ -1308,7 +1309,8 @@ def has_many(name, scope = nil, options = {}, &extension)
# has_one :club, through: :membership
# has_one :primary_address, -> { where primary: true }, through: :addressables, source: :addressable
def has_one(name, scope = nil, options = {})
- Builder::HasOne.build(self, name, scope, options)
+ reflection = Builder::HasOne.build(self, name, scope, options)
+ Reflection.add_reflection self, name, reflection
end
# Specifies a one-to-one association with another class. This method should only be used
@@ -1420,7 +1422,8 @@ def has_one(name, scope = nil, options = {})
# belongs_to :company, touch: true
# belongs_to :company, touch: :employees_last_updated_at
def belongs_to(name, scope = nil, options = {})
- Builder::BelongsTo.build(self, name, scope, options)
+ reflection = Builder::BelongsTo.build(self, name, scope, options)
+ Reflection.add_reflection self, name, reflection
end
# Specifies a many-to-many relationship with another class. This associates two classes via an
@@ -1557,7 +1560,8 @@ def belongs_to(name, scope = nil, options = {})
# has_and_belongs_to_many :categories, join_table: "prods_cats"
# has_and_belongs_to_many :categories, -> { readonly }
def has_and_belongs_to_many(name, scope = nil, options = {}, &extension)
- Builder::HasAndBelongsToMany.build(self, name, scope, options, &extension)
+ reflection = Builder::HasAndBelongsToMany.build(self, name, scope, options, &extension)
+ Reflection.add_reflection self, name, reflection
end
end
end
@@ -20,9 +20,7 @@ def self.create(macro, name, scope, options, ar)
klass = AggregateReflection
end
- reflection = klass.new(macro, name, scope, options, ar)
- add_reflection ar, name, reflection
- reflection
+ klass.new(macro, name, scope, options, ar)
end
def self.add_reflection(ar, name, reflection)

0 comments on commit eb8d48e

Please sign in to comment.