diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-10-02 11:22:59 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-10-02 11:22:59 -0700 |
commit | d15165196c6f62c4d28bd0e0f06fb210215767a6 (patch) | |
tree | 610d71eedf06e66ab1ce5e1d4dc681e9d15279e0 /activerecord/lib/active_record | |
parent | 636a909985c03cca5b342e20c6143f3b3e07c9d8 (diff) | |
download | rails-d15165196c6f62c4d28bd0e0f06fb210215767a6.tar.gz rails-d15165196c6f62c4d28bd0e0f06fb210215767a6.tar.bz2 rails-d15165196c6f62c4d28bd0e0f06fb210215767a6.zip |
define the join model without using closures on the methods
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations.rb | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 9fac0d618b..8aa59712be 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1598,36 +1598,49 @@ module ActiveRecord 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 |