Question

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:

  1. create the base gem structure with bundle gem common
  2. place the layout file inside common/app/views/layouts/application.html.haml
  3. 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
    
  4. commit the changes

  5. gem build common.gemspec (successful)
  6. rake install (successful)
  7. 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.

Was it helpful?

Solution 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!

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top