Is there a Rails equivalent of Sinatra's 'register'? - ruby-on-rails

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

Related

How do I configure the load order of Rails engines?

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.

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 - overriding features

I'm surprised to see how simple is to customize things in rails.
You start by installing a gem that provides some functionality you need and then you just customize what you need to.
An example is Spree, which can be customized by simply overriding templates and ruby files with custom code.
I'm just wondering who's allowing me to do this:
Is it Ruby?
Is it the architecture of these Gems?
Is it the Rails architecture?
Ruby allows meta-programming since its evaluated at runtime.
In a rails application, you can add classes like ruby String to the initializers folder and add new declarative methods, which will then be added to the String class and can be used with Strings.
Read more http://www.vitarara.org/cms/ruby_metaprogamming_declaratively_adding_methods_to_a_class
It's part rails and part ruby. Rails has been written (and has been rewritten to be more) extendable by developers and plugins over the years. Certain ways of hooking in and overriding functionality are enabled through ruby and others would be the same in many less dynamic languages.

How to generate ActiveResource specific template files

We are planning to supporting a ruby 1.9.x application (which has database connectivity via ActiveRecord, but no user interface). We used rails (3.1.3) generate command to generate the application template files, but we wouldn't need access to view specific files. What command should I use to generate only the files which would be needed REST services which will be exposed via an ActiveResource
I think this answer sheds some light on it:
Using ActiveResource without Rails
And this article seems to describe a project that exposes a RESTful API, without an interface http://www.therailsway.com/2007/9/3/using-activeresource-to-consume-web-services/
You could also just straight up delete the view files, and in your controllers, avoid rendering any of the templates. Rails will, by default, will try to find a template with a name matching the action, but you can tell your actions not to render anything.
The other option is to not write it as a web app, but instead as a Rack app, or just pure Ruby, if you don't need an interface. Rails is really built to create interactive web apps, so maybe everything that comes with Rails by default is really more than you need?

Ruby On Rails CMS Framework

I want to create a framework for rails application. It will be a rails application but packed into gem (like a Radiant CMS).
It must work like this:
gem install cmsframework
and then:
cmsframework the_app
After that we have a skeleton framework for a rails app, without any controllers, etc. All controllers are loaded from cmsframework gem.
If I want to rewrite some files (for example public/styles.css), I must simply create it in my app (the_app).
If I want new functions in my app I can create a plugin. But the main functionalities must be loaded from cmsframework gem.
What is the best way to implement this?
Maybe start here: http://guides.rails.info/plugins.html. Pay close attention to the parts about adding custom generators and packaging as a gem. This may help as well: http://railscasts.com/episodes/218-making-generators-in-rails-3.
You can use this framework it is very good CamaleonCMS

Resources