Browse files

extend by adding relationships rather than monkey patching

  • Loading branch information...
1 parent bb1492e commit 7085aae9019f5b6bab242471fe3c1be2997423ed @tenderlove tenderlove committed Jul 23, 2013
View
12 activerecord/lib/active_record/associations/builder/association.rb
@@ -14,9 +14,11 @@ module ActiveRecord::Associations::Builder
class Association #:nodoc:
class << self
attr_accessor :valid_options
+ attr_accessor :extensions
end
self.valid_options = [:class_name, :foreign_key, :validate]
+ self.extensions = []
attr_reader :model, :name, :scope, :options, :reflection
@@ -48,14 +50,14 @@ def mixin
@model.generated_feature_methods
end
- include Module.new { def build(*args); end }
-
def build
validate_options
define_accessors
configure_dependency if options[:dependent]
@reflection = model.create_reflection(macro, name, scope, options, model)
- super(@model, @reflection) # provides an extension point
+ Association.extensions.each do |extension|
+ extension.build @model, @reflection
+ end
@reflection
end
@@ -64,13 +66,13 @@ def macro
end
def valid_options
- Association.valid_options
+ (Association.valid_options + Association.extensions.map(&:valid_options)).flatten
end
def validate_options
options.assert_valid_keys(valid_options)
end
-
+
# Defines the setter and getter methods for the association
# class Post < ActiveRecord::Base
# has_many :comments
View
12 activerecord/lib/active_record/autosave_association.rb
@@ -127,17 +127,17 @@ module AutosaveAssociation
extend ActiveSupport::Concern
module AssociationBuilderExtension #:nodoc:
- def build(model, reflection)
+ def self.build(model, reflection)
model.send(:add_autosave_association_callbacks, reflection)
- super
+ end
+
+ def self.valid_options
+ [ :autosave ]
end
end
included do
- Associations::Builder::Association.class_eval do
- self.valid_options << :autosave
- include AssociationBuilderExtension
- end
+ Associations::Builder::Association.extensions << AssociationBuilderExtension
end
module ClassMethods

0 comments on commit 7085aae

Please sign in to comment.