Jump to content

The ultimate community for Ruby on Rails developers.


Custom Gem to Call Application Methods

  • Please log in to reply
1 reply to this topic

#1 djmill



  • Members
  • 4 posts

Posted 07 November 2013 - 04:32 PM

Hi all,


I'll start with the general idea:

  • My application has validations for various models, but these validations exist across multiple models. So I'm working on making a central module/class that handles validations for multiple models to reduce redundancies. Let's call this class "Validator" and call its method "invalid?"


  • We also built a gem for the company to import customer records via Excel spreadsheets --  let's call this gem "Importer". Now we want access to our applications Validator class and its methods from within the gem. That way we can send a hash containing the attributes to validate and their respective methods to call within the application. So for example the gem receives a new object to create, but sees that it should validate ":start_date" and "end_date" (receiving these values from the application itself), but only if the object has these attributes.


My Problem:

  • When receiving a method in the Importer gem like: Validator.invalid? the Importer gem cannot find the application's context.


I'm not new to Rails but I've never had to call application methods from a gem. I think I have to do something in the Railties class to load it in, but I have no idea how/what I have to do.


If anyone has any experience in this, I'd really appreciate the help :)


Thanks in advance.

#2 djmill



  • Members
  • 4 posts

Posted 07 November 2013 - 06:10 PM

I've answered my own question. The class wasn't found because it was a class inside of a Module. For whatever reason, my validator works as Module when called from the Importer gem.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users