Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rails Engines. Doing It Wrong. And Then Right.
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Robert Glaser
October 13, 2011
Programming
280
6
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rails Engines. Doing It Wrong. And Then Right.
Robert Glaser
October 13, 2011
More Decks by Robert Glaser
See All by Robert Glaser
RAG: The Architecture of Reliable AI
youngbrioche
0
87
Dein Produkt braucht nicht mehr Technik, sondern mehr Produkt
youngbrioche
3
540
Die Unbenutzbarkeit von Enterprise Web-Anwendungen
youngbrioche
0
71
Stop flying blind. Profiling your App's Internals.
youngbrioche
3
520
Bootstrapping & Boilerplating
youngbrioche
9
370
Travis CI
youngbrioche
4
340
Modern Web Development with Ruby on Rails
youngbrioche
2
330
Other Decks in Programming
See All in Programming
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
320
Foundation Models frameworkで画像分析
ryodeveloper
1
160
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
910
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
180
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
710
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
16
3.7k
コーディングルールの鮮度を保ちたい for SRE NEXT 2026 / keep-fresh-go-internal-conventions-sre-next-2026
handlename
0
160
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
140
Android CLI
fornewid
0
190
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
2
1.3k
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
3.8k
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
0
630
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
77
5.4k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
Product Roadmaps are Hard
iamctodd
55
12k
Exploring anti-patterns in Rails
aemeredith
3
450
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
270
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
430
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
360
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.2k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
230
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Darren the Foodie - Storyboard
khoart
PRO
3
3.5k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
Transcript
Rails Engines. Doing it wrong. And then right.
None
@mrreynolds
Why Engines? Extend your app with reusable models, controllers, views,
helpers, routes, locales and tasks.
It‘s an app within an app.
Rails::Engine < Rails::Railtie
What‘s a Railtie? • Core of the framework • Provides
hooks to extend Rails • ActiveRecord, ActionController etc. are all Railties
What is an Engine then ?
It‘s just a Railtie with some defaults.
Typical cases • CMS • Admin Frontend • Translation Frontend
Our problem • Build a vocabulary and thesauraus management system
• Adjust and extend it for every customer without forking it
github.com/innoq/iqvoc
First approach • iQvoc as main app • Vendor logic
as engine
The problem • Could not act as a standalone app
• Always had to be plugged into a main app
Second approach • Vendor logic as main app • iQvoc
as engine (and app)
The problem • A Rails 3.0 Engine can not act
as a standalone app by default (requires customization) • No out-of-the-box support for migrations, assets etc.
Options
Wait for Rails 3.1
Just hack it.
What do you need ?
Act as an engine… # lib/engine.rb module Iqvoc class Engine
< Rails::Engine end end
…only if we want to # config/initializers/iqvoc.rb unless Iqvoc.const_defined?(:Application) require
File.join(File.dirname(__FILE__), '../../lib/engine') end # config/application.rb module Iqvoc class Application < Rails::Application
Up next: Engine tasks # lib/engine.rb class Engine < Rails::Engine
paths.lib.tasks << "lib/engine_tasks" Only available when app is mounted as an engine!
What about Migrations? namespace :iqvoc do namespace :db do task
:migrate => :environment do ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true path = Iqvoc::Engine.find_root_with_flag("db").join('db/migrate') ActiveRecord::Migrator.migrate(path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby end end end
None
Rails 3.1 rake railties:copy_migrations
Routes Foo.application.routes.draw do Rails.application.routes.draw do
Rails 3.1 # Main app Rails.application.routes.draw do mount Foo::Engine =>
"/foo" end # Engine Foo::Engine.routes.draw do …
Rails 3.1 Namespace isolation module MyEngine class Foo < Rails::Engine
isolate_namespace Foo end end # Separate routers for each Engine foo.root_path main_app.root_path
If you isolate, don‘t forget to move things.
app/controllers/foo/things_controller.rb app/views/foo/things/new.html.erb …
Assets task :link do Iqvoc.for_static_folders do |source_common_dir, target_common_dir| File.unlink(target_common_dir) if
File.symlink?(target_common_dir) && ENV['force'] == "true" if !File.exists?(target_common_dir) puts "Linking #{source_common_dir} -> #{target_common_dir}" File.symlink(source_common_dir, target_common_dir) else puts "Symlink #{target_common_dir} already exists!" end end end
Rails 3.1 # ActionDispath::Static config.serve_static_assets = true or rake railties:create_symlinks
bundle in hell • No support for multiple locations of
a single gem
Forget about that: group :development do gem 'iqvoc', :path =>
'../iqvoc' end group :production do gem 'iqvoc', :git => '
[email protected]
:innoq/iqvoc.git' end
Forget about that as well:
None
Instead: Separate your Gemfiles
Rails 3.0 Engines Rails 3.1 Engines Rails 2.3 Engines
Engines = Mountable Apps
None
@drogus Piotr Sarnacki Say thanks to:
Thanks!