Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Engines

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
Tweet

More Decks by Janko Marohnić

Other Decks in Programming

Transcript

  1. Rails Engines
    Janko Marohnić
    janko-m @jankomarohnic

    View Slide

  2. ├── lib
    │ └── todo.rb
    └── todo.gemspec

    View Slide

  3. ├── lib
    │ └── todo.rb
    └── todo.gemspec
    ├── vendor
    ├── config
    ├── app

    View Slide

  4. lib/todo.rb
    module Todo
    # ...
    end
    module Todo
    class Engine < Rails::Engine
    end
    end

    View Slide

  5. ├── app
    │ └── views
    │ ├── assets
    │ ├── controllers
    │ ├── helpers
    │ ├── models
    ├── vendor
    │ └── assets
    ├── config
    │ └── locales
    ├── lib
    │ └── tasks
    │ ├── initializers

    View Slide

  6. 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

    View Slide

  7. $ 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

    View Slide

  8. 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

    View Slide

  9. Engine kao library

    View Slide

  10. Primjeri
    • Devise

    • ActiveAdmin

    • Spree

    • Kaminari

    View Slide

  11. Engine kao dio aplikacije

    View Slide

  12. 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”)

    View Slide

  13. ├── app
    ├── config
    ├── db
    ├── lib
    ├── script
    ├── vendor
    ├── public

    View Slide

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

    View Slide

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

    View Slide

  16. 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

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

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

    View Slide

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

    View Slide

  21. 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

    View Slide

  22. Izolirani Engine
    Todo
    Views
    Routes
    Helpers

    View Slide

  23. module Todo
    class Engine < Rails::Engine
    !
    end
    end
    module Todo
    class Engine < Rails::Engine
    isolate_namespace Todo
    end
    end
    Izolirani Engine

    View Slide

  24. Izolirani Engine
    todo.root_path
    todo.tasks_path
    todo.new_task_path
    URL helpers

    View Slide

  25. Izolirani Engine
    main_app.root_path
    main_app.about_path
    main_app.terms_path
    URL helpers

    View Slide

  26. Izolirani Engine
    Todo
    Views
    Routes
    Helpers

    View Slide

  27. Izolirani Engine
    Views
    Routes
    Helpers
    Todo

    View Slide

  28. Izolirani Engine
    Views
    Routes
    Helpers
    Authentication
    Legacy
    Todo
    Blog
    Marketing
    Mobile

    View Slide

  29. Rails::Engine
    Rails::Application
    Rails::Railtie

    View Slide

  30. Railtie
    initializers

    rake tasks

    View Slide

  31. Railtie
    Engine
    MVC

    routes
    initializers

    rake tasks

    View Slide

  32. Railtie
    Engine
    Application
    MVC

    routes
    initializers

    rake tasks
    booting

    initializing

    middleware

    View Slide

  33. Dokumentacija

    View Slide

  34. Engineovi su zakon
    Koristite ih, i radite svoje

    View Slide