Skip to content

Commit

Permalink
extend by adding relationships rather than monkey patching
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jul 23, 2013
1 parent bb1492e commit 7085aae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Expand Up @@ -14,9 +14,11 @@ module ActiveRecord::Associations::Builder
class Association #:nodoc: class Association #:nodoc:
class << self class << self
attr_accessor :valid_options attr_accessor :valid_options
attr_accessor :extensions
end end


self.valid_options = [:class_name, :foreign_key, :validate] self.valid_options = [:class_name, :foreign_key, :validate]
self.extensions = []


attr_reader :model, :name, :scope, :options, :reflection attr_reader :model, :name, :scope, :options, :reflection


Expand Down Expand Up @@ -48,14 +50,14 @@ def mixin
@model.generated_feature_methods @model.generated_feature_methods
end end


include Module.new { def build(*args); end }

def build def build
validate_options validate_options
define_accessors define_accessors
configure_dependency if options[:dependent] configure_dependency if options[:dependent]
@reflection = model.create_reflection(macro, name, scope, options, model) @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 @reflection
end end


Expand All @@ -64,13 +66,13 @@ def macro
end end


def valid_options def valid_options
Association.valid_options (Association.valid_options + Association.extensions.map(&:valid_options)).flatten
end end


def validate_options def validate_options
options.assert_valid_keys(valid_options) options.assert_valid_keys(valid_options)
end end

# Defines the setter and getter methods for the association # Defines the setter and getter methods for the association
# class Post < ActiveRecord::Base # class Post < ActiveRecord::Base
# has_many :comments # has_many :comments
Expand Down
12 changes: 6 additions & 6 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -127,17 +127,17 @@ module AutosaveAssociation
extend ActiveSupport::Concern extend ActiveSupport::Concern


module AssociationBuilderExtension #:nodoc: module AssociationBuilderExtension #:nodoc:
def build(model, reflection) def self.build(model, reflection)
model.send(:add_autosave_association_callbacks, reflection) model.send(:add_autosave_association_callbacks, reflection)
super end

def self.valid_options
[ :autosave ]
end end
end end


included do included do
Associations::Builder::Association.class_eval do Associations::Builder::Association.extensions << AssociationBuilderExtension
self.valid_options << :autosave
include AssociationBuilderExtension
end
end end


module ClassMethods module ClassMethods
Expand Down

0 comments on commit 7085aae

Please sign in to comment.