On Thu, Aug 5, 2010 at 5:16 AM, Bruno C. [email protected]
wrote:
which binds the name to the object value. It doesn’t change the class
defineFoo
the object again to have the updated definition of the class. Old
instances remain the same even after the class definition is changed.
Thanks Rick.
That’s all well and good, but I have to step back and say that I
detect a code smell called “stupid ruby tricks” here.
This type of dynamic class alteration can lead to all kinds of trouble
trying to understand, and debug the code. Debugging Rails callbacks
is tough enough even without such unorthodox code. If you pursue this
technique I predict several frustrating debugging sessions in your
future.
Why not just use the AR API and start with something like
class Account < ActiveRecord::Base
class << self
attribute_accessor :validating_description
end
validates_numericality_of :description :if => lambda { |account|
Account.validating_description}
end
Then just use
Account.validating_description = true
to turn it off and
Account.validating_description = false
to turn it off?
Even then, I’m not sure I understand the use case for turning this off
at the class label, usually such conditional validations are done on
the basis of instance state, which is why the proc associated with the
:if and :unless options takes the instance as an argument, of course
the proc is free to ignore that as I’ve done here.
But it’s your use case, and other than poking you to think about
whether you want to do it on a class or instance basis, I’ll defer to
your superior domain knowledge.
–
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale