简体   繁体   English

创建仅由Rails模板部分组成的Ruby gem是否有意义?

[英]Does it make sense to create a Ruby gem that consists of only Rails template partials?

I'm trying to understand what does and doesn't work in Ruby gems (primarily from the perspective of creating one for myself to reuse functionality from one project to the next). 我正在尝试了解Ruby gems中的工作原理和不工作原理(主要是从为我自己创建一个以在一个项目中重复使用功能到另一个项目的角度)。

When you create a Ruby gem, are you limited to including only Ruby functionality or could you create a gem that consisted of Rails templates, CSS files and JavaScripts? 创建Ruby gem时,您是否仅限于仅包含Ruby功能,还是可以创建由Rails模板,CSS文件和JavaScript组成的gem?

If so, how would the user of the gem access those resources in a Rails project (besides adding a 'config gem' statement to the environment.rb file? 如果是这样,gem的用户将如何在Rails项目中访问这些资源(除了在environment.rb文件中添加'config gem'语句之外?

No it doesn't. 不,不是。 Gems are code libraries, not static files. 宝石是代码库,而不是静态文件。

When you load a gem, you don't move the files to your public directory. 加载gem时,不会将文件移动到公共目录中。 You load a ruby file and get access to a module. 您加载一个ruby文件并可以访问模块。 So, you can only execute ruby code within it. 因此,您只能在其中执行ruby代码。
But, your ruby code can give you some helpers allowing you to easily build your design for example. 但是,您的ruby代码可以为您提供一些帮助,例如,您可以轻松构建自己的设计。

If you want to include some static file resources in each of your ruby projects, I'd see two solutions: 如果要在每个ruby项目中包括一些静态文件资源,我将看到两个解决方案:

  • Create a template project, including all the css, javascript etc. that you use every time. 创建一个模板项目,包括您每次使用的所有css,javascript等。 You can take suspenders as an example of this. 您可以以吊带为例。

  • Create a new repository with all these static files and add it as a submodule (with git or svn ) and include it in each of your projects. 使用所有这些静态文件创建一个新的存储库,并将其添加为子模块(使用gitsvn ),并将其包含在每个项目中。

The first solution would be my favorite, but you might want to take the other one. 第一个解决方案是我的最爱,但您可能想要采用另一个解决方案。

As dmathieu notes, gems are for ruby libraries that are installed in the system load path, not for things that go into your project directory. 如dmathieu所述,gem适用于安装在系统加载路径中的ruby库,而不适用于进入项目目录的内容。 If you do want to package your rails project skeleton as a gem, though, it would be simple enough to have a small ruby executable that served as a generator to copy the skeleton project tree into a target directory. 但是,如果您确实希望将rails项目框架打包为gem,那么将拥有一个小的ruby可执行文件作为生成器将框架项目树复制到目标目录中就足够简单了。 You could then package the whole thing up into a gem, your skeleton files could sit in the gem directory, and you could say generate_new_project someproject to copy them into your new project directory. 然后,您可以将整个程序打包到一个gem中,您的骨架文件可以放在gem目录中,并且可以说generate_new_project someproject可以将它们复制到新的项目目录中。

You can easily add partials and other HAML, Builder, or ERB views. 可以轻松添加局部视图和其他HAML,Builder或ERB视图。 The following structure should work: 以下结构应该起作用:

# in my_gem/rails/init.rb
ActionController::Base.append_view_path(File.expand_path(File.join(File.dirname(__FILE__), '..', 'views')))

# in my_gem/views/my_gem/_some_partial.html.erb
This is a partial from MyGem!

# in your_rails_app/views/some_controller/some_view.html.erb:
<%= render :partial => '/my_gem/some_partial' -%>

That doesn't directly answer your question about static files, though. 但是,这并不能直接回答您有关静态文件的问题。 Often the best bet is a generator that copies the CSS, JS, and other files to your public directory. 通常最好的选择是将CSS,JS和其他文件复制到公共目录的生成器。 Alternatively, you could use a StaticFilesController and put the static files in that views directory. 另外,您可以使用StaticFilesController并将静态文件放在该views目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM