Slide 1

Slide 1 text

Rails Engines Janko Marohnić janko-m @jankomarohnic

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

$ 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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Engine kao library

Slide 10

Slide 10 text

Primjeri • Devise • ActiveAdmin • Spree • Kaminari

Slide 11

Slide 11 text

Engine kao dio aplikacije

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Izolirani Engine Todo Views Routes Helpers

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Izolirani Engine Todo Views Routes Helpers

Slide 27

Slide 27 text

Izolirani Engine Views Routes Helpers Todo

Slide 28

Slide 28 text

Izolirani Engine Views Routes Helpers Authentication Legacy Todo Blog Marketing Mobile

Slide 29

Slide 29 text

Rails::Engine Rails::Application Rails::Railtie

Slide 30

Slide 30 text

Railtie initializers rake tasks

Slide 31

Slide 31 text

Railtie Engine MVC routes initializers rake tasks

Slide 32

Slide 32 text

Railtie Engine Application MVC routes initializers rake tasks booting initializing middleware

Slide 33

Slide 33 text

Dokumentacija

Slide 34

Slide 34 text

Engineovi su zakon Koristite ih, i radite svoje