Best way to require Haml on Rails3

I am developing a Rails3 engine application and I want to use Haml for presentations.

First, I did this to add this to the Gemfile engine:

gem "haml" 

While I tested my engine, it worked fine (I used https://github.com/josevalim/enginex to create the gem and test it using the application dummy).

My problems started when I tried to use the engine in a real Rails application. The application does not have a gem "haml" on its own Gemfile, and therefore it did not initialize Haml, so I got a pattern of errors not found, because it did not look for a .haml representation. I thought that requiring Haml in the Engine would be enough for the Rails application to do the same.

What I did now is add config / initializers / haml.rb to the engine with this code:

 require 'haml' Haml.init_rails(binding) 

Now it works, but I wonder if this is really a good way to do this. Why doesn't Rails call the Haml file “init.rb” and therefore correctly initialize Haml by simply adding the gem “haml” to the Gemfile engine?

+7
source share
2 answers

Two things are needed. Firstly, in .gemspec:

 s.add_dependency 'haml', ['>= 3.0.0'] 

And in your lib / gem_name.rb:

 require 'haml' 

And then run bundle both inside the precious and application directories.

+19
source

I think you will need to put haml in the gemspec engine as a dependency so that the connected one sets haml in the target application (and appears in its Gemfile.lock). Something like that:

 Gem::Specification.new do |s| s.add_dependency(%q<haml>, [">= 0"]) end 

I just tested this on one of my engines. Without dependencies in .gemspec, he did not install haml in the target application (did not appear in Gemfile.lock). After I added haml to gemspec as a dependency, it appears:

 PATH remote: /rails_plugins/mine/my_engine specs: my_engine (0.0.0) formtastic haml inherited_resources settingslogic sqlite3-ruby GEM remote: http://rubygems.org/ specs: #................ haml (3.0.25) #................ 

If you use a jeweler, it will automatically add dependencies to the gemspec depending on what is in your Gemfile .. it even adds developmentement_dependency if you have a group defined in your Gemfile. I just looked briefly at enginex, so I don’t know if it has a similar rake task to build gemspec.

This may help clarify some things:

http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/

+6
source

All Articles