redinger / validation_reflection

This plugin adds reflective access to validations
http://rubygems.org/gems/validation_reflection
MIT License
296 stars 18 forks source link

Rails 3.2 issue with undefined method 'write_inheritable_array' #12

Open JDutil opened 12 years ago

JDutil commented 12 years ago
/Users/JD/.rvm/gems/ruby-1.9.2-p290@ab_reps/gems/activerecord-3.2.0.rc2/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined method `write_inheritable_array' for #<Class:0x007f847e5442f0> (NoMethodError)
    from /Users/JD/.rvm/gems/ruby-1.9.2-p290@ab_reps/gems/validation_reflection-1.0.0/lib/validation_reflection.rb:82:in `block in remember_validation_metadata'
    from /Users/JD/.rvm/gems/ruby-1.9.2-p290@ab_reps/gems/validation_reflection-1.0.0/lib/validation_reflection.rb:81:in `each'
    from /Users/JD/.rvm/gems/ruby-1.9.2-p290@ab_reps/gems/validation_reflection-1.0.0/lib/validation_reflection.rb:81:in `remember_validation_metadata'
    from /Users/JD/.rvm/gems/ruby-1.9.2-p290@ab_reps/gems/validation_reflection-1.0.0/lib/validation_reflection.rb:60:in `block in validates_presence_of_with_reflection'
    from /Users/JD/.rvm/gems/ruby-1.9.2-p290@ab_reps/gems/validation_reflection-1.0.0/lib/validation_reflection.rb:91:in `ignoring_subvalidations'
    from /Users/JD/.rvm/gems/ruby-1.9.2-p290@ab_reps/gems/validation_reflection-1.0.0/lib/validation_reflection.rb:58:in `validates_presence_of_with_reflection'
Stefano1990 commented 12 years ago

I have the exact same issue...

ndemoreau commented 12 years ago

+1

marcoam commented 12 years ago

+1. If it helps, I updated from 3.0.9 -> 3.1 -> 3.2 Thanks!

ncri commented 12 years ago

Try changing

    def remember_validation_metadata(validation_type, *attr_names)
      configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
      attr_names.each do |attr_name|
        write_inheritable_array :validations,
          [ ActiveRecord::Reflection::MacroReflection.new(validation_type, attr_name.to_sym, configuration, self) ]
      end
    end

to:

   def remember_validation_metadata(validation_type, *attr_names)
      configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
      self.validations ||= []
      attr_names.each do |attr_name|
        self.validations << ActiveRecord::Reflection::MacroReflection.new(validation_type, attr_name.to_sym, configuration, self)
      end
    end

and

  def self.included(base)
    return if base.kind_of?(BoilerPlate::ActiveRecordExtensions::ValidationReflection::ClassMethods)
    base.extend(ClassMethods)
  end

to

  def self.included(base)
    return if base.kind_of?(BoilerPlate::ActiveRecordExtensions::ValidationReflection::ClassMethods)
    base.extend(ClassMethods)
    base.send :class_attribute, :validations
  end

Untested, as I' still have to work through other issues with Rails 3.2 before getting it running... ;-)

ncri commented 12 years ago

Okay, one more change is required to get it working (now tested ;-):

Change the reflect_on_all_validations method to

# Returns an array of MacroReflection objects for all validations in the class
def reflect_on_all_validations
  validations || []
end
JDutil commented 12 years ago

I'm not sure if you forgot to post some changes you made or if you didn't do the update off from master. I tried to just create a Rails 3.2 branch for a pull request and can't get the updates to work. Notably the code you want me to change away from doesn't match what you've got listed as what to change.

ncri commented 12 years ago

Ah, sorry, I might not have been on the latest version, yes... can you figure out what needs to be changed on master from my hints? Otherwise I can look if I can.

ncri commented 12 years ago

Okay, as far as i can see you only need to replace in my code:

 return if base.kind_of?(BoilerPlate::ActiveRecordExtensions::ValidationReflection::ClassMethods)

in def self.included(base) to

 return if base.kind_of?(::ValidationReflection::ClassMethods)

and

 attr_names.each do |attr_name| 

in remember_validation_metadata to

 attr_names.flatten.each do |attr_name|

to be conform with master

JDutil commented 12 years ago
base.send :class_attribute, :validations

Isn't working properly for me. I get:

/Users/JD/validation_reflection/lib/validation_reflection.rb:30:in `included': undefined method `class_attribute' for ValidationReflection::ClassMethods:Module (NoMethodError)
    from /Users/JD/validation_reflection/lib/validation_reflection.rb:62:in `include'
    from /Users/JD/validation_reflection/lib/validation_reflection.rb:62:in `<module:ClassMethods>'
    from /Users/JD/validation_reflection/lib/validation_reflection.rb:60:in `<module:ValidationReflection>'
    from /Users/JD/validation_reflection/lib/validation_reflection.rb:3:in `<top (required)>'
    from /Users/JD/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
    from /Users/JD/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
    from /Users/JD/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/JD/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/JD/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:251:in `require'
    from /Users/JD/validation_reflection/test/validation_reflection_test.rb:17:in `<top (required)>'

That shouldn't be looking for it on: ValidationReflection::ClassMethods:Module

It should just be ValidationReflection::ClassMethods right?

ncri commented 12 years ago

Okay, I was working on the old plugin version.

Here is the fix for master: https://github.com/redinger/validation_reflection/pull/13