Hello,
I’ve just created my first plugin, is really a simple one, and it has
only one validation, special for a spanish bank account.
As I need it in more than one RoR, I’ve decided to create as a plugin,
but now I’m having Stack level too deep every time I save a record, no
matter if this validation is from the same model or not, always Stack
level too deep.
If I remove the plugin, everything works as expected.
I’m totally newbie for plugin creation, I found some examples on
internet and tried to follow/adapt them …
Any blog, wiki, or how-to about plugin creation would be wellcome
thanks,
r.
Here are the files:
Vendor/plugins/cc_validator/init.rb:
Include hook code here
ActiveRecord::Validations::ClassMethods.send :include, CcValidator
ActiveRecord::Base.class_eval do
include ActiveRecord::Validations
end
Vendor/plugins/cc_validator/lib/cc_validator.rb
CcValidator
module CcValidator
Checks if an ActiveRecord property is a valid Spanish bank account
number.
def validates_cc_of(*attrs)
configuration = { :message =>
ActiveRecord::Errors.default_error_messages[:invalid], :on => :save,
:with => nil }
configuration.update(attrs.pop) if attrs.last.is_a?(Hash)
validates_each(attrs, configuration) do |record, attr_name, value|
record.errors.add(attr_name, configuration[:message]) unless
cc(value) == 0
end
end
private
Calculates the CC of a Spanish bank account number.
Return value should be 0 (cc succeeded). Any other value means
faillure.
http://es.wikipedia.org/wiki/Código_Cuenta_Cliente
def cc(account)
...
…
end
end