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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

  9. Engine kao library

    View full-size slide

  10. Primjeri
    • Devise

    • ActiveAdmin

    • Spree

    • Kaminari

    View full-size slide

  11. Engine kao dio aplikacije

    View full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

  22. Izolirani Engine
    Todo
    Views
    Routes
    Helpers

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  26. Izolirani Engine
    Todo
    Views
    Routes
    Helpers

    View full-size slide

  27. Izolirani Engine
    Views
    Routes
    Helpers
    Todo

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. Railtie
    initializers

    rake tasks

    View full-size slide

  31. Railtie
    Engine
    MVC

    routes
    initializers

    rake tasks

    View full-size slide

  32. Railtie
    Engine
    Application
    MVC

    routes
    initializers

    rake tasks
    booting

    initializing

    middleware

    View full-size slide

  33. Dokumentacija

    View full-size slide

  34. Engineovi su zakon
    Koristite ih, i radite svoje

    View full-size slide