Permalink
Browse files

extract more mutations to the caller

  • Loading branch information...
1 parent 35f94ac commit 49d50b9d80957e53a72ba8ffa1dd51f1247610b5 @tenderlove tenderlove committed Aug 1, 2013
@@ -26,8 +26,10 @@ def self.build(model, name, scope, options, &block)
raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)
builder = new(model, name, scope, options, &block)
+ reflection = builder.build
builder.define_accessors model.generated_feature_methods
- builder.build
+ builder.define_callbacks reflection
+ reflection
end
def initialize(model, name, scope, options)
@@ -52,11 +54,7 @@ def initialize(model, name, scope, options)
def build
configure_dependency if options[:dependent]
- reflection = ActiveRecord::Reflection.create(macro, name, scope, options, model)
- Association.extensions.each do |extension|
- extension.build @model, reflection
- end
- reflection
+ ActiveRecord::Reflection.create(macro, name, scope, options, model)
end
def macro
@@ -71,6 +69,12 @@ def validate_options
options.assert_valid_keys(valid_options)
end
+ def define_callbacks(reflection)
+ Association.extensions.each do |extension|
+ extension.build @model, reflection
+ end
+ end
+
# Defines the setter and getter methods for the association
# class Post < ActiveRecord::Base
# has_many :comments
@@ -12,17 +12,16 @@ def constructable?
!options[:polymorphic]
end
- def build
- reflection = super
- add_counter_cache_callbacks(reflection) if options[:counter_cache]
- add_touch_callbacks(reflection) if options[:touch]
- reflection
- end
-
def valid_dependent_options
[:destroy, :delete]
end
+ def define_callbacks(reflection)
+ super
+ add_counter_cache_callbacks(reflection) if options[:counter_cache]
+ add_touch_callbacks(reflection) if options[:touch]
+ end
+
def define_accessors(mixin)
super
add_counter_cache_methods mixin

0 comments on commit 49d50b9

Please sign in to comment.