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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
86
Dein Produkt braucht nicht mehr Technik, sondern mehr Produkt
youngbrioche
3
530
Die Unbenutzbarkeit von Enterprise Web-Anwendungen
youngbrioche
0
66
Stop flying blind. Profiling your App's Internals.
youngbrioche
3
510
Bootstrapping & Boilerplating
youngbrioche
9
360
Travis CI
youngbrioche
4
330
Modern Web Development with Ruby on Rails
youngbrioche
2
330
Other Decks in Programming
See All in Programming
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.3k
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
13k
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
280
はてなアカウント基盤 State of the Union
cockscomb
1
710
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.4k
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
940
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
320
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
590
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
1
340
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
260
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
Performance Engineering for Everyone
elenatanasoiu
0
220
Featured
See All Featured
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
240
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
220
Are puppies a ranking factor?
jonoalderson
1
3.6k
The SEO Collaboration Effect
kristinabergwall1
1
490
A Soul's Torment
seathinner
6
3k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
860
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.3k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
210
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
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!