Forum: Ruby on Rails My first plugin => my first Stack level too deep

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mongeta 9. (Guest)
on 2009-05-05 12:02
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
Maurício L. (Guest)
on 2009-05-05 15:42
(Received via mailing list)
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
Mongeta 9. (Guest)
on 2009-05-05 15:48
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.
Maurício L. (Guest)
on 2009-05-05 16:34
(Received via mailing list)
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
This topic is locked and can not be replied to.