Rails Engines

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Rails Engines

A presentation about what are Rails Engines, how can they help you and when should you use them.

Janko Marohnić

June 24, 2014

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

34 slides
  1. ├── app │ └── views │ ├── assets │ ├──

    controllers │ ├── helpers │ ├── models ├── vendor │ └── assets ├── config │ └── locales ├── lib │ └── tasks │ ├── initializers
  2. Engine App Todo::Engine.routes.draw do resources :tasks end Todo::Engine.routes.draw do resources

    :tasks end Rails.application.routes.draw do mount Todo::Engine, at: “/todo” end
  3. $ rake routes Prefix Verb URI Pattern Controller#Action root GET

    / home#index about GET /about pages#about faq GET /terms pages#terms todo /todo Todo::Engine ! Routes for Todo::Engine: tasks GET todo/tasks tasks#index POST todo/tasks tasks#create new_task GET todo/tasks/new tasks#new edit_task GET todo/tasks/:id/edit tasks#edit task GET todo/tasks/:id tasks#show PATCH todo/tasks/:id tasks#update PUT todo/tasks/:id tasks#update DELETE todo/tasks/:id tasks#destroy
  4. Engine App ├── db │ ├── migrate │ │ ├──

    20140614100712_create_tasks.rb │ │ ├── 20140614100941_add_position_to_tasks.rb │ │ ├── 20140614101121_create_owners.rb $ rake todo:install:migrations ├── db │ ├── migrate │ │ ├── 20140614100712_create_tasks.todo.rb │ │ ├── 20140614100941_add_position_to_tasks.todo.rb │ │ ├── 20140614101121_create_owners.todo.rb
  5. Rails.application.routes.draw do root to: “pages#home” ! controller :pages do get

    “/about”, to: :about get “/terms”, to: :terms end resources :songs end Rails.application.routes.draw do root to: “pages#home ! controller :pages do get “/about”, to: :about get “/terms”, to: :terms end resources :songs ! # LEGACY ROUTES # ! get “/start” => redirect(“/”) get “/about-us” => redirect(“/about”) get “/home” => redirect(“/about”) resources :tracks, controller: :songs ! get “/home/songs” => redirect(“/songs”) get “/authenticate” => “devise#login” get “/auth/google” => “/authenticate” get “/sign_in” => redirect(“/login”)
  6. ├── app ├── config ├── db ├── lib ├── script

    ├── vendor ├── public ├── engines
  7. ├── app ├── config ├── db ├── lib ├── script

    ├── vendor ├── public ├── engines │ └── legacy
  8. ruby “2.1.2” ! source “https://rubygems.org” ! gem “rails”, “~> 4.1.1”

    gem “pg” ! gem “legacy”, path: “engines/legacy” ! group :assets do # … end Gemfile
  9. Engine App Rails.application.routes.draw do controller :pages do get “/about”, to:

    :about get “/terms”, to: :terms end resources :songs ! ! end Rails.application.routes.draw do controller :pages do get “/about”, to: :about get “/terms”, to: :terms end resources :songs ! mount Legacy::Engine, at: “/” end Legacy::Engine.routes.draw do get “/start” => redirect(“/”) get “/about-us” => redirect(“/about”) get “/home” => redirect(“/about”) resources :tracks, controller: :songs end
  10. class ApplicationControler < ActionController::Base # … ! private ! def

    current_user User.find_by(id: session[:user_id]) end ! def authenticate! if not user_signed_in? redirect_to sign_in_path end end ! # … end
  11. ├── app ├── config ├── db ├── lib ├── script

    ├── vendor ├── public ├── engines │ └── legacy
  12. ├── app ├── config ├── db ├── lib ├── script

    ├── vendor ├── public ├── engines │ └── legacy │ ├── authentication
  13. Engine App class AuthController < ApplicationController def authenticate! # …

    end ! def current_user # … end end class ApplicationController < AC::Base before_filter :important_announcement ! private ! def important_announcement puts “Šime je glup” end end
  14. module Todo class Engine < Rails::Engine ! end end module

    Todo class Engine < Rails::Engine isolate_namespace Todo end end Izolirani Engine

More Decks by Janko Marohnić

Other Decks in Programming