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
--
Posted via http://www.ruby-forum.com/.
Maurício Linhares
2009-May-05 11:41 UTC
Re: My first plugin => my first Stack level too deep
Ok, first we''ll need the exception backtrace to be sure about where this code is breaking, but you can start by changing your plugin a little bit: Vendor/plugins/cc_validator/init.rb: ------------------------------------------- # Include hook code here ActiveRecord::Base.extend CcValidator - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Tue, May 5, 2009 at 5:02 AM, Raimon Fs <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > -- > Posted via http://www.ruby-forum.com/. > > > >
Maurício Linhares wrote:> Ok, first we''ll need the exception backtrace to be sure about where > this code is breaking, but you can start by changing your plugin a > little bit: > > Vendor/plugins/cc_validator/init.rb: > ------------------------------------------- > # Include hook code here > ActiveRecord::Base.extend CcValidatorHello Maurício, thanks, now it works perfect! now, where I can get more info about extending RoR with plug-ins ???? :-) thanks ... r. -- Posted via http://www.ruby-forum.com/.
Maurício Linhares
2009-May-05 12:34 UTC
Re: My first plugin => my first Stack level too deep
You can try this one -> http://peepcode.com/products/rails-2-plugin-patterns - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Tue, May 5, 2009 at 8:48 AM, Raimon Fs <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Maurício Linhares wrote: >> Ok, first we''ll need the exception backtrace to be sure about where >> this code is breaking, but you can start by changing your plugin a >> little bit: >> >> Vendor/plugins/cc_validator/init.rb: >> ------------------------------------------- >> # Include hook code here >> ActiveRecord::Base.extend CcValidator > > Hello Maurício, > > thanks, now it works perfect! > > now, where I can get more info about extending RoR with plug-ins ???? > > :-) > > thanks ... > > r. > -- > Posted via http://www.ruby-forum.com/. > > > >