How to package application layout into a Ruby gem?
-
10-06-2021 - |
Domanda
I'm working on a project comprising three different applications. They are supposed to share some models and the outer layout. To avoid code duplication, I'm trying to extract the application layout (project_name/app/views/layouts/application.html.haml
) into a gem.
I followed these steps:
- create the base gem structure with
bundle gem common
- place the layout file inside
common/app/views/layouts/application.html.haml
wrote the Gemspec descriptors
# -*- encoding: utf-8 -*- require File.expand_path('../lib/common/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["Arthur Alkmim"] gem.email = ["myemail@here"] gem.description = %q{Common project files} gem.summary = %q{Common project files, including layout and migrations} gem.homepage = "" gem.files = `git ls-files`.split($\) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.name = "common" gem.require_paths = ["lib"] gem.version = Common::VERSION end
commit the changes
gem build common.gemspec
(successful)rake install
(successful)- Place
gem 'common'
in the project Gemfile
But still the project won't load the application layout. What should I do to tell my project it has to load the layout files through the gem?
Thanks in advance.
Soluzione 2
I sort of solved it. I changed application.html.haml
to common.html.haml
and placed the relevant layout call in the ApplicationController. Seems like Rails won't let me package the application.html layout in a gem, but other layouts are okay. If somebody comes up with a better solution (less workaround-ish), please post!
Altri suggerimenti
Have you added your gem to your Rails Gemfile to include it in your application?
You can use the path option to specify a relative path in development. e.g.
gem 'common', :path => "../path/to/gem"
Don't forget to then run bundle install