Best way to require Haml on Rails3 engines

若如初见. 提交于 2019-11-28 00:38:10

问题


I'm developing a Rails3 engine application, and I want to use Haml for the views.

First, what I have done was to add this to the engine Gemfile:

gem "haml"

While I was testing my engine, it was working OK (I have used https://github.com/josevalim/enginex to generate the gem and test it with the dummy application).

My problems started when I tried to use the engine on a real Rails application. The application does not have gem "haml" on it's own Gemfile, and so it was not initializing Haml, so I was receiving template not found errors as it was not looking for the .haml views. I was thinking that by requiring Haml on the Engine it would be enought for it to be also required by the Rails application.

What I have done for now was to add a config/initializers/haml.rb on the engine with this code:

require 'haml'
Haml.init_rails(binding)

It's working now, but I'm wondering if this is really a good way to do it. Why Rails is not calling Haml "init.rb" file and so initializing Haml correctly by just adding gem "haml" to the engine Gemfile?


回答1:


Two things are necessary. First, in the .gemspec:

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

And in your lib/gem_name.rb:

require 'haml'

And then run bundle both inside the gem and app directories.




回答2:


I think you will have to put haml in the engine gemspec as a dependency in order for bundler to install haml in the target application (and show up in its Gemfile.lock). Something like this:

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

I just tested this out on one of my engines. Without the dependency in the .gemspec it did not install haml in the target app (did not appear in Gemfile.lock). After I added haml to the gemspec as a dependency, it does show up:

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 are using jeweler, it will add the dependencies to the gemspec automatically based on what is in your Gemfile.. it even adds a developement_dependency if you have the group defined in your Gemfile. I have only looked at enginex briefly, so I don't know if it has a similar rake task to build the gemspec.

This might help clarify some things:

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



来源:https://stackoverflow.com/questions/5015297/best-way-to-require-haml-on-rails3-engines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!