Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveModel Naming should take care of anonymous classes

#5252

When trying to use the ActiveModel Validations together with sequel like

class Message < Sequel::Model
  include ActiveModel::Validations
  validates :text, :presence => true, :length => { :within => 1..10000 }
end

I get the following error: ArgumentError (interning empty string).

activemodel (3.0.0.rc) lib/active_model/naming.rb:27:in to_sym' 
activemodel (3.0.0.rc) lib/active_model/naming.rb:27:inhuman'
activemodel (3.0.0.rc) lib/active_model/naming.rb:26:in map' 
activemodel (3.0.0.rc) lib/active_model/naming.rb:26:inhuman'
activemodel (3.0.0.rc) lib/active_model/errors.rb:313:in generate_message' 
activemodel (3.0.0.rc) lib/active_model/errors.rb:188:inadd'
activemodel (3.0.0.rc) lib/active_model/errors.rb:223:in add_on_blank' 
activemodel (3.0.0.rc) lib/active_model/errors.rb:221:ineach'
activemodel (3.0.0.rc) lib/active_model/errors.rb:221:in add_on_blank' 
activemodel (3.0.0.rc) lib/active_model/validations/presence.rb:9:invalidate'
activesupport (3.0.0.rc) lib/active_support/callbacks.rb:309:in send' 
activesupport (3.0.0.rc) lib/active_support/callbacks.rb:309:in_callback_before_47'
activesupport (3.0.0.rc) lib/active_support/callbacks.rb:409:in _run_validate_callbacks' 
activemodel (3.0.0.rc) lib/active_model/validations.rb:201:inrun_validations!'
activemodel (3.0.0.rc) lib/active_model/validations.rb:168:in valid?' /home/gucki/.rvm/gems/ree-1.8.7-2010.01@rails3/bundler/gems/sequel-a54e149/lib/sequel/model/base.rb:957:insave' ...

Jeremy (Sequel maintainer says): This looks like an ActiveModel bug, as they shouldn't be attempting to call to_sym on an empty string. Basically, they should be checking for anonymous classes and they aren't.

A quick fix is to modify ActiveModel::Naming from

  defaults = @klass.lookup_ancestors.map do |klass|
    klass.model_name.underscore.to_sym
  end

to

  defaults = @klass.lookup_ancestors.map do |klass|
    next if klass.name==""
    klass.model_name.underscore.to_sym
  end.compact

If this fix is okay, I can create a patch for it.

Reported by gucki · July 30th, 2010 @ 04:14 PM

State: open
Milestone: 3.0.6
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. gucki
    gucki
    • Tag set to activemodel, rails3.0rc

    July 30th, 2010 @ 04:19 PM

  2. Xavier Noria
    Xavier Noria
    • Importance changed from to Low

    Don't know about the solution as such, just wanted to comment that AS defines #anonymous? and if the patch is fine it would be preferred over the comparison by hand.

    August 9th, 2010 @ 01:32 PM

  3. gucki
    gucki

    Thanks for the hint with #anonymous?, that's indeed nicer.

    The patch against latest master is now available here:
    http://github.com/gucki/rails/commit/e0f7490a607f3444223c0769bd5282...

    August 9th, 2010 @ 01:45 PM

  4. Xavier Noria
    Xavier Noria

    Cool! Yeah in addition to being encapsulated, #name returns either an empty string or nil for anonymous classes/modules, depending on the interpreter.

    Rails no longer assumes all Active Support is loaded, you also need to cherry-pick this file active_support/core_ext/module/anonymous.rb:

    require 'active_support/core_ext/module/anonymous'
    

    to ensure #anonymous is available (it is irrelevant if it is due to previously required code, you know).

    August 9th, 2010 @ 02:19 PM

  5. gucki
  6. John Firebaugh
    John Firebaugh

    Why is lookup_ancestors returning an anonymous class in this case? My guess is the right solution lies in changing that, rather than in explicitly checking anonymous? in #human.

    August 9th, 2010 @ 04:45 PM

  7. gucki
    gucki

    You mean lookup_ancestors should always remove anonymous classes from its result? I guess also anonymous ancestors might be used somewhere and so should be included in the result, but this is to internal to me...sorry.

    August 9th, 2010 @ 05:15 PM

  8. John Firebaugh
    John Firebaugh

    If you look at the other places lookup_ancestors is used, none of them expect it to return anonymous classes. I think the !anonymous? check should be in lookup_ancestors, or in a Sequel-specific override of lookup_ancestors.

    BTW, this example works for me:

    class Message < Sequel::Model
      include ActiveModel::Validations
    
      def text
      end
    
      validates :text, :presence => true, :length => { :within => 1..10000 }
    end
    
    Message.new.valid?
    

    I think you must be using something more like:

    class Message < Sequel::Model(:message)
    

    Which, if you are plugging Sequel's active_model plugin into Sequel::Model, will indeed cause Message.lookup_ancestors to return the anonymous class created by Sequel::Model(:message).

    August 9th, 2010 @ 06:44 PM

  9. gucki
    gucki

    Well, I guess the core team has to decide. So please, could any of the core team members let us know where to patch? I'll then provide the tested patch asap :)

    BTW: You might be right, I might have missed the arguments to Sequel::Model in the example above. Sorry :(

    August 10th, 2010 @ 08:31 AM

  10. gucki
    gucki

    So here's the patch to make ActiveModel::Translation#lookup_ancestors ignore anonymous classes. Would be nice if it'd be included in 3.0 final. :)
    http://github.com/gucki/rails/commit/5aa7e64eefe388425575204ac7e829...

    August 25th, 2010 @ 02:48 PM

  11. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to open
    • Assigned user set to Santiago Pastorino

    Need more eyes on this ticket and patch. Assigning to Santiago for review.

    August 26th, 2010 @ 04:22 AM

  12. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 9th, 2010 @ 09:46 PM

  13. gucki
    gucki

    In the hope it'll soon get fixed upstream :)

    def lookup_ancestors
      self.ancestors.select { |x| x.respond_to?(:model_name) && !x.anonymous? }
    end
    

    October 28th, 2010 @ 08:27 AM

  14. Jeff Kreeftmeijer
  15. Jeff Kreeftmeijer
  16. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.

    Is this still an issue? gucki can you provide a patch again? Thanks.

    February 2nd, 2011 @ 08:09 PM

  17. Santiago Pastorino
  18. Santiago Pastorino
    Santiago Pastorino
    • Milestone changed from 3.0.5 to 3.0.6

    February 27th, 2011 @ 03:15 AM