How can I successfully run initializers inside of a Rails engine, in order to configure third-party gems?
Here's an example setup (sorry for the verbosity):
1. Rails 3.1
2. I generated a Rails engine (sample-engine) and put it into vendor/. The Gemfile of the Rails app loads it with:
gem 'sample-engine', :path => 'vendor/sample-engine'
3. Some of the ActiveRecord models provided by my engine (in sample-engine/app/models) depend on a third-party tool. Let's say that's the Paperclip gem for dealing with file attachments.
4. My engine has a .gemspec that outlines the dependency:
Gem::Specification.new do |s|
...
s.add_dependency 'paperclip', '~>2.5.0'
...
end
5. My engine has an initializer (sample-engine/config/initializers/paperclip.rb) that does some Paperclip-related configuration. (The contents here don't matter.)
Paperclip.interpolates ... do
...
end
When I run the Rails app, I get:
> NoMethodError: undefined method `interpolates' for Paperclip:Module
> ...sample-engine/config/initializers/paperclip.rb:17:in `<top (required)>'
The only way I've found around this is to add:
require 'paperclip'
in sample-engine/lib/sample-engine.rb.
Is there a better way to do this?
Thanks!
Pete
Pete
--
Utah Ruby Users Group
ur...@googlegroups.com
http://utruby.org
http://bit.ly/urug_lunches
http://groups.google.com/group/urug/
- All meeting times and places can be found here.
** Please prefix your subject with "[JOB]" if your message is about job opportunities.
> Are you calling #gemspec in your engine's Gemfile? I think that will require any dependencies listed in your .gemspec file.
Yes, I am, but it appears that the Gemfile is irrelevant in this case. From what I can see, a Rails engine is just like any other gem - if you need to use another library, you have to "require" it. Rails provides no mechanism for loading the contents of the Gemfile at runtime. (The engine's Gemfile is only useful in development and testing.)
Pete
Pete