Rails engines

827 views

Published on

This is a presentation about what Rails Engines are for the RubyGDL group talks

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
827
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
8
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Rails engines

  1. 1. Rails Engines ! @igmarin
  2. 2. Rails Engines • Engines are stand-alone Rails application that can be mounted inside of another application • Controllers and Models are defined within modules of the engine’s namespace • architecting an app with engines forces you to really think through your dependencies and makes you isolate things from the get go
  3. 3. Big Plus • Reduce errors from duplication • Reusable code
  4. 4. Generate Rails Engine we have a rails new plugin generator that will help us
  5. 5. $ rails plugin new awesome creates a skeleton for developing any kind of Rails extension with ability to run tests using dummy Rails application awesome |-- Gemfile |-- Gemfile.lock |-- MIT-LICENSE |-- README.rdoc |-- Rakefile |-- awesome.gemspec |-- lib | |-- awesome | | `-- version.rb | |-- awesome.rb | `-- tasks | `-- awesome_tasks.rake `-- test |-- awesome_test.rb |-- dummy
  6. 6. $ rails plugin new awesome --full --full tells that we want an engine including app: • An app directory tree • An a config/routes.rb awesome |-- Gemfile |-- Gemfile.lock |-- MIT-LICENSE |-- README.rdoc |-- Rakefile |-- app | |-- assets | |-- controllers | |-- helpers | |-- mailers | |-- models | `-- views |-- awesome.gemspec |-- bin | `-- rails |-- config | `-- routes.rb |-- lib | |-- awesome | | |-- engine.rb | | `-- version.rb | |-- awesome.rb | `-- tasks | `-- awesome_tasks.rake `-- test |-- awesome_test.rb |-- dummy
  7. 7. lib/awesome/engine.rb module Awesome class Engine < ::Rails::Engine end end
  8. 8. $ rails plugin new awesome --mountable namespace-isolated engine. Same as full • Asset manifest files (application.js and application.css) • A namespaced ApplicationController stub • A namespaced ApplicationHelper stub • A layout view template for the engine awesome |-- Gemfile |-- Gemfile.lock |-- MIT-LICENSE |-- README.rdoc |-- Rakefile |-- app | |-- assets | |-- controllers | |-- helpers | |-- mailers | |-- models | `-- views |-- awesome.gemspec |-- bin | `-- rails |-- config | `-- routes.rb |-- lib | |-- awesome | | |-- engine.rb | | `-- version.rb | |-- awesome.rb | `-- tasks | `-- awesome_tasks.rake `-- test |-- awesome_test.rb |-- dummy
  9. 9. lib/awesome/engine.rb module Awesome class Engine < ::Rails::Engine isolate_namespace Awesome end end test/dummy/config/routes.rb Rails.application.routes.draw do mount Awesome::Engine => "/awesome" end
  10. 10. Route Helpers awesome.root_url # /awesome ! main_app.root_url # /
  11. 11. Generators lib/generators/awesome/install_generator.rb ! module Awesome class InstallGenerator < Rails::Generators::Base def install run ‘bundle install’ route “mount Awesome::Engine => ‘/awesome’” rake ‘awesome:install:migrations’ rake ‘db:migrate’ end end end
  12. 12. Popular Engines • Devise —> Authentication • Spree —> e-Commerce • Forem —> Forum • RailsAdmin —> Admin Interface • Monologue —> Blogging
  13. 13. Resources • Great Book: Multitenancy on Rails by Ryan Bigg • https://www.youtube.com/watch?v=s3NJ15Svq8U • http://guides.rubyonrails.org/engines.html • http://tech.taskrabbit.com/blog/2014/02/11/rails-4- engines/ • http://gaslight.co/blog/maintainable-and-scalable- systems-with-rails-engines

×