How do I configure the load order of Rails engines? - ruby-on-rails

I have a Rails application that is using multiple gems. Each gem provides assets, that are added to the Rails asset path:
main app
admin engine with customised forms
wysiwyg engine
I want to override some of the partials provided by the wysiwyg engine inside the admin engine. I know that I need to affect the order that each engine adds its asset paths to the ActionView lookup context used by render for partial resolution, but I'm not sure how to do this.

This is actually documented by Rails but it took me a long time to find the relevant documentation. I assumed I needed to affect the bundler gem file load order, but I actually needed to define the order that engines (railties) are initialised:
https://api.rubyonrails.org/classes/Rails/Engine.html#class-Rails::Engine-label-Loading+priority
In my config/application.rb I added:
config.railties_order = [:main_app, Admin::Engine, :all]
I would prefer to define the dependency between the admin engine and the wysiwyg engine, but this does address my issue.

Related

Is there a Rails equivalent of Sinatra's 'register'?

I'm in the process of creating a rubygem which will be used with both Sinatra and Rails applications. Ideally, I'd like to have a single gem which can work with both frameworks. It's very simple - it provides some helpers, styles, scripts and view partials.
For Sinatra, I use the register method to register the module, which in turn adds the helpers, adds some entries to the load paths and optionally creates some actions/routes. So far so good.
My question is: What is the rails equivalent of this? Engines?
Since you need to define routes, I think a rails engine would work best.
You can load helpers with railties too, but I don't think it's possible to define routes with railties.
Rails Engines:
http://edgeguides.rubyonrails.org/engines.html
Railties:
http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html

Rails Spree internals

I am trying to use Spree with my RoR application. Ok, I do follow all those guides and FAQs on official website when I want to customize something. That's ok and no problem with it. One question, to which I could not find a clue -- how is that possible, that there is nothing in apps/view, apps/models folders, but it's still working? I mean, yes, I can create something in these folders and redefine the behavior of my views (actually, this is one of the ways of customization), but I really want to understand the internals. I am pretty new to Rails and got used to classic app folder structure.
what you are wondering about is the magic of Rails Engines.
Ruby on Rails allows you to define Engines (your app is one too) and when it looks for views/controllers/etc.. all mounted engines are part of the search path.
So the view is inside the Spree gem, not visible to you - but it still looks in there.
If you put something in your view folder with the same name, it will take precedence over the views in the Rails engine you have in the Gem.
Here is a good guide on how Engines work in Rails:
http://edgeguides.rubyonrails.org/engines.html
One good example of these Engines is the jQuery-rails Gem you probably use inside your Application.
It has no code at all (except for some fallbacks for Rails 3.0 and below that don't have an asset pipeline), but the jQuery.js file in the app/assets/javascripts folder. And since the engine is in the load path you can require the asset that's in there..
The engine itself has the same folder structure as your app (app/views, app/controllers ...)
You can look at the internal structure of Spree here: https://github.com/spree/spree/tree/master/core/app

Rails gem with CSS and javascript

I've got a simple rails gem (created using bundler) and I'd like to extend it by adding some CSS and javascript functionality. However, I'm unsure how to go about this and where to add the files. In particular, I need need more information on how it all fits together with the asset pipeline once it gets included in another project.
Can anyone give me the lowdown on how this works and either provide some simple examples or link to a tutorial? Literally 1 css and 1 js file is all I'm looking to include. Thanks.
You could write the gem as an engine. This allows you to have an app folder in the gem just as any Rails application would have. You can add models, views, controllers, assets etc.
Once you have it set up it's quite intuitive and it's a familiar way to create a gem if you're used to creating Rails apps.
This should get you started:
http://coding.smashingmagazine.com/2011/06/23/a-guide-to-starting-your-own-rails-engine-gem/

Rails 3 - differences between Engines and Plugins

Unfortunately official guide for creating Rails plugins is outdated: http://guides.rubyonrails.org/plugins.html and I didn't find any good tutorial for creating engines only this code: https://github.com/mankind/Rails-3-engine-example and enginex https://github.com/josevalim/enginex were helpful.
So... what are differences between plugins and engines in Rails 3? (I'm thinking of plugins and engines with controllers and views)?
And how can I create plugin with views and controllers? Or maybe in Rails 3 it's better to use engines? Can I use main app model (for example user model, logged user information) inside my engine?
Engines are just required as gems in your app. Since Rails 3.1 you even don't have to require their assets such as .css or .js
In Rails 2.x, Engines didn't exist, so plugins enabled people to have these functionalities but all code was copied inside the app.

Create plugins or gems for Rails 3?

I have features I would like to be portable between my own Rails applications.
I wonder if I should create a gem or a plugin for each feature I want to be portable (sharable).
They are just for Rails (for now) because they include css, html, js and image files.
But I have been wondering, the things provided with plugins could be provided with gems too but not the opposite? So maybe it's better to learn how to create gems, because then you I don't have to learn how to create both gems and plugins? And gems seem to be more popular these days.
But then, from what I can understand one gem is shared between all rails app in the OS. So that means I can not customize it for each Rails app right? In that case, maybe creating a plugin is better cause it should be allowed to customize (editing css, js etc) each feature and to have it stored inside the Rails app itself and not in the OS level.
Some advices would be appreciated!
UPDATE:
So gem works great with css, html, js and image files? In a plugin I think you can have a MVC, your own models, views and controllers. Quoted from Rails guides "Storing models, views, controllers, helpers and even other plugins in your plugins". Is this possible too in a gem? Eg. I want to add a extension that gives me a nice Shopping cart (with own migrations, mvc, asset files) that will be hooked into the current Rails app. Is this possible as gem or only as plugin?
You're right that gems offer a little more than plugins. Versioning and dependencies on other gems being the main ones for me.
One gem needn't be shared across everything using ruby. You can install multiple versions of a single gem and specify in your environment.rb that a gem requires a specific version. E.g.
config.gem 'my-gem', :version => '1.2.3'
Also you can freeze gems into your rails application so that you know you are working with a specific version.
You might want to look at the jeweler gem which makes creating your own gems easier.
UPDATE
To include CSS, javascript etc I think you'll need to make an Rails engine which can then be bundled as a plugin or a gem. I've not done this but there's some coverage here and here.
The push with Rails 3 seems to be towards gems and away from plugins as a lot of support has been added to make gems work as well or better than plugins ever did. A well constructed gem is a great thing to have and share between different applications, and also reduces the amount of testing you will have to do since you can test the gem thoroughly before integration.
For extensions to Rails that use CSS, HTML and other assets, it might be that you need to build an engine to bundle this all up and allow it to fit neatly into an application.
As of Rails 4, plugins will no longer be supported.
Gems are the way forward.

Resources