This project is archived and is in readonly mode.
ActiveModel Naming should take care of anonymous classes
Reported by gucki | July 30th, 2010 @ 04:14 PM | in 3.0.6
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.
Comments and changes to this ticket
-
gucki July 30th, 2010 @ 04:19 PM
- Tag set to activemodel, rails3.0rc
-
Xavier Noria August 9th, 2010 @ 01:32 PM
- 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.
-
gucki August 9th, 2010 @ 01:45 PM
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... -
Xavier Noria August 9th, 2010 @ 02:19 PM
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).
-
gucki August 9th, 2010 @ 02:30 PM
Ok, here we go again :-)
http://github.com/gucki/rails/commit/5f58dbb4807cd895a3faa91c484bfb... -
John Firebaugh August 9th, 2010 @ 04:45 PM
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.
-
gucki August 9th, 2010 @ 05:15 PM
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.
-
John Firebaugh August 9th, 2010 @ 06:44 PM
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).
-
gucki August 10th, 2010 @ 08:31 AM
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 :(
-
gucki August 25th, 2010 @ 02:48 PM
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... -
Rohit Arondekar August 26th, 2010 @ 04:22 AM
- 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.
-
gucki October 28th, 2010 @ 08:27 AM
In the hope it'll soon get fixed upstream :)
def lookup_ancestors self.ancestors.select { |x| x.respond_to?(:model_name) && !x.anonymous? } end
-
Santiago Pastorino February 2nd, 2011 @ 08:09 PM
- Milestone cleared.
Is this still an issue? gucki can you provide a patch again? Thanks.
-
Santiago Pastorino February 27th, 2011 @ 03:15 AM
- Milestone changed from 3.0.5 to 3.0.6
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>