Skip to content

Commit

Permalink
have the builder create the middle reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Oct 2, 2013
1 parent 0af978d commit 7963c5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
13 changes: 2 additions & 11 deletions activerecord/lib/active_record/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1574,18 +1574,9 @@ def has_and_belongs_to_many1(name, scope = nil, options = {}, &extension)

join_model = builder.through_model

middle_name = [self.name.downcase.pluralize, name].join('_').gsub(/::/, '_').to_sym

middle_options = builder.middle_options join_model

hm_builder = Builder::HasMany.create_builder(self,
middle_name,
nil,
middle_options)
middle_reflection = hm_builder.build self
middle_reflection = builder.middle_reflection join_model

Builder::HasMany.define_callbacks self, middle_reflection

Reflection.add_reflection self, middle_reflection.name, middle_reflection

include Module.new {
Expand All @@ -1599,7 +1590,7 @@ def destroy_associations
}

hm_options = {}
hm_options[:through] = middle_name
hm_options[:through] = middle_reflection.name
hm_options[:source] = join_model.right_association_name

[:before_add, :after_add, :before_remove, :after_remove].each do |k|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ def self.add_right_association(name, options)
join_model
end

def middle_reflection(join_model)
middle_name = [lhs_model.name.downcase.pluralize,
association_name].join('_').gsub(/::/, '_').to_sym
middle_options = middle_options join_model
hm_builder = HasMany.create_builder(lhs_model,
middle_name,
nil,
middle_options)
hm_builder.build lhs_model
end

private

def middle_options(join_model)
middle_options = {}
middle_options[:class] = join_model
Expand All @@ -90,8 +103,6 @@ def middle_options(join_model)
middle_options
end

private

def belongs_to_options(options)
rhs_options = {}

Expand Down

0 comments on commit 7963c5d

Please sign in to comment.