2012-05-24 38 views
4

我正在研究一个包含三个不同应用程序的项目。他们应该分享一些模型和外部布局。为了避免代码重复,我试图将应用程序布局(project_name/app/views/layouts/application.html.haml)提取到一个gem中。如何将应用程序布局打包到Ruby gem中?

我按照这些步骤:

  1. 里面创建common/app/views/layouts/application.html.haml
  2. bundle gem common

  3. 地方布局文件基本宝石结构写的Gemspec描述

    # -*- encoding: utf-8 -*- 
    require File.expand_path('../lib/common/version', __FILE__) 
    
    Gem::Specification.new do |gem| 
        gem.authors  = ["Arthur Alkmim"] 
        gem.email   = ["[email protected]"] 
        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. 提交更改

  5. gem build common.gemspec(成功)
  6. rake install(成功)
  7. gem 'common'项目的Gemfile

但还是该项目将不会加载应用程序的布局。我应该怎么做才能告诉我的项目必须通过gem加载布局文件?

在此先感谢。

+0

如何使用git子模块或svn外部?如果你走宝石路线,你将不得不建立/安装每当有变化。 – kreek

+0

@KreeK子模块和外部设备是一种痛苦。根据我的经验处理宝石更容易 - 只需运行“捆绑安装”即可。特别是Git子模块非常烦人。 –

回答

1

我有点解决它。我将application.html.haml更改为common.html.haml,并将相关的布局调用放置在ApplicationController中。看起来像Rails不会让我将application.html布局封装在gem中,但是其他布局也可以。如果有人提出了一个更好的解决方案(更少的解决方法 - ish),请发布!

+0

我会为你的宝石提供一个发电机。用你的版本替换'application.html.haml'。就像'rails g common:install'一样' – Kyle

+0

我认为这比装饰控制器更麻烦。每次更新宝石时,我都必须重新运行生成器,并且通过代码复制再次受到困扰。 –

+0

出于好奇 - 您是否在使用该gem的应用程序中删除了'app/views/layouts/application.html.haml'? – max

1

您是否已将您的Gem添加到您的Rails Gemfile中以将其包含在您的应用程序中?

您可以使用path选项在开发中指定相对路径。例如

gem 'common', :path => "../path/to/gem" 

不要忘了,然后运行bundle install

相关问题