diff options
Diffstat (limited to 'activerecord/lib')
5 files changed, 31 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index d8d68eb908..37ba1c73b1 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -88,7 +88,7 @@ module ActiveRecord::Associations::Builder # # Post.first.comments and Post.first.comments= methods are defined by this method... def self.define_accessors(model, reflection) - mixin = model.generated_feature_methods + mixin = model.generated_association_methods name = reflection.name define_readers(mixin, name) define_writers(mixin, name) diff --git a/activerecord/lib/active_record/associations/builder/singular_association.rb b/activerecord/lib/active_record/associations/builder/singular_association.rb index 2a4b1c441f..66b03c0301 100644 --- a/activerecord/lib/active_record/associations/builder/singular_association.rb +++ b/activerecord/lib/active_record/associations/builder/singular_association.rb @@ -8,7 +8,7 @@ module ActiveRecord::Associations::Builder def self.define_accessors(model, reflection) super - define_constructors(model.generated_feature_methods, reflection.name) if reflection.constructable? + define_constructors(model.generated_association_methods, reflection.name) if reflection.constructable? end # Defines the (build|create)_association methods for belongs_to or has_one association diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index d9cb14e4c6..d93c26c130 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -91,12 +91,12 @@ module ActiveRecord def initialize_generated_modules super - generated_feature_methods + generated_association_methods end - def generated_feature_methods - @generated_feature_methods ||= begin - mod = const_set(:GeneratedFeatureMethods, Module.new) + def generated_association_methods + @generated_association_methods ||= begin + mod = const_set(:GeneratedAssociationMethods, Module.new) include mod mod end diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index da6bc87950..efea9560ee 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -35,30 +35,41 @@ module ActiveRecord # end module Enum def enum(definitions) + klass = self definitions.each do |name, values| enum_values = {} name = name.to_sym - # def direction=(value) self[:direction] = DIRECTION[value] end - define_method("#{name}=") { |value| self[name] = enum_values[value] } + _enum_methods_module.module_eval do + # def direction=(value) self[:direction] = DIRECTION[value] end + define_method("#{name}=") { |value| self[name] = enum_values[value] } - # def direction() DIRECTION.key self[:direction] end - define_method(name) { enum_values.key self[name] } + # def direction() DIRECTION.key self[:direction] end + define_method(name) { enum_values.key self[name] } - pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index - pairs.each do |value, i| - enum_values[value] = i + pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index + pairs.each do |value, i| + enum_values[value] = i - # scope :incoming, -> { where direction: 0 } - scope value, -> { where name => i } + # scope :incoming, -> { where direction: 0 } + klass.scope value, -> { klass.where name => i } - # def incoming?() direction == 0 end - define_method("#{value}?") { self[name] == i } + # def incoming?() direction == 0 end + define_method("#{value}?") { self[name] == i } - # def incoming! update! direction: :incoming end - define_method("#{value}!") { update! name => value.to_sym } + # def incoming! update! direction: :incoming end + define_method("#{value}!") { update! name => value.to_sym } + end end end end + + def _enum_methods_module + @_enum_methods_module ||= begin + mod = Module.new + include mod + mod + end + end end end diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index df28451bb7..9d92e747d4 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -335,7 +335,7 @@ module ActiveRecord # the helper methods defined below. Makes it seem like the nested # associations are just regular associations. def generate_association_writer(association_name, type) - generated_feature_methods.module_eval <<-eoruby, __FILE__, __LINE__ + 1 + generated_association_methods.module_eval <<-eoruby, __FILE__, __LINE__ + 1 if method_defined?(:#{association_name}_attributes=) remove_method(:#{association_name}_attributes=) end |