Skip to content

Commit

Permalink
define the join model without using closures on the methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Oct 2, 2013
1 parent 636a909 commit d151651
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -1598,36 +1598,49 @@ def self.build(rhs_class, name, options)
end
end

def belongs_to_options(options)
rhs_options = {}

if options.key? :class_name
rhs_options[:foreign_key] = options[:class_name].foreign_key
rhs_options[:class_name] = options[:class_name]
end

if options.key? :association_foreign_key
rhs_options[:foreign_key] = options[:association_foreign_key]
end

rhs_options
end

def has_and_belongs_to_many1(name, scope = nil, options = {}, &extension)
# temporarily
habtm = JoinTableResolver.build self, name, options

join_class_name = "HABTM_#{name.to_s.camelize}"
this_class = self
rhs_name = name.to_s.singularize.to_sym

join_model = Class.new(ActiveRecord::Base) {
define_singleton_method(:name) { join_class_name }
define_singleton_method(:table_name) { habtm.join_table }
define_singleton_method(:compute_type) { |class_name|
this_class.compute_type class_name
}
belongs_to :left_side, :class => this_class

rhs_options = {}

if options.key? :class_name
rhs_options[:foreign_key] = options[:class_name].foreign_key
rhs_options[:class_name] = options[:class_name]
class << self;
attr_accessor :class_resolver
attr_accessor :name
attr_accessor :table_name_resolver
end

if options.key? :association_foreign_key
rhs_options[:foreign_key] = options[:association_foreign_key]
def self.table_name
table_name_resolver.join_table
end

belongs_to rhs_name, rhs_options
def self.compute_type(class_name)
class_resolver.compute_type class_name
end
}

join_model.name = "HABTM_#{name.to_s.camelize}"
join_model.table_name_resolver = habtm
join_model.class_resolver = self

join_model.belongs_to :left_side, :class => self
join_model.belongs_to rhs_name, belongs_to_options(options)

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

#middle_options = options.dup
Expand Down

0 comments on commit d151651

Please sign in to comment.