LoginSignup
63
63

More than 5 years have passed since last update.

Railtie, Engine 関係資料

Last updated at Posted at 2012-12-26

API Reference

Rails::Railtie

http://api.rubyonrails.org/classes/Rails/Railtie.html
日本語訳 http://blog.twiwt.org/e/e0445b

Railtie は Rails フレームワークのコアで、 Rails に機能を追加したり初期化プロセスを変更するためのいくつかのフックを提供します。

例えば、以下のような機能拡張を行う場合は Railtie が必要になります:

  • イニシャライザの作成
  • configuring a Rails framework for the application, like setting a generator
  • adding config.* keys to the environment
  • setting up a subscriber with ActiveSupport::Notifications
  • rake タスクの追加

Rails::Engine

Any Rails::Engine is also a Rails::Railtie, so the same methods (like rake_tasks and generators) and configuration options that are available in railties can also be used in engines.

RailtieとEngineの関係

Rails::Engine < Rails::Railtie

"すべてをつなぎ合わせるRailtie"
WEB+DB Press vol.58 特集「詳解Rails3」

Engine は Rack Application にもなっている。(Rails::Engine#call が存在する)
(Rackについては http://jp.rubyist.net/magazine/?0036-SinatraReintroduction#l10 など参照)

独自の config/routes.rb, config/locales/*, lib/tasks/* などを持ち、自動的にロードされる。

Railtie + アプリっぽい動作(?) = Engine

つかいかたの例

Railtie

あらゆるものがRailtieなので、何でもいいんだけど

  • ActiveRecord
  • rspec-rails
  • jquery-rails
  • haml-rails
module Haml
  module Rails
    class Railtie < ::Rails::Railtie
      config.generators.template_engine :haml

      config.before_initialize do
        Haml.init_rails(binding)
        Haml::Template.options[:format] = :html5
      end
    end
  end
end

configの設定とinitializeを行なっている。

Engine

  • Devise

その他

ActiveSupport.on_load

Railsの標準コンポーネントの拡張に便利。
http://api.rubyonrails.org/files/activesupport/lib/active_support/lazy_load_hooks_rb.html
http://blog.udzura.jp/2011/11/28/shibuya-rb-1st-with-activesupport-tips/
フックポイントは :action_controller, :action_view, :active_record, :action_mailer などが用意されている。

ActiveSupport::Concern

Rubyの既存クラスの拡張に便利。
http://api.rubyonrails.org/classes/ActiveSupport/Concern.html
http://guides.rubyonrails.org/plugins.html (記述が古い可能性あり)

今後 Sendagaya.rb でなにをやるか (10/15以降予定)

  • Engineの機能を知りたい
    • API リファレンスを読もう
    • ソースを読もう
    • Kaminariのソースを読もう
    • Crafting Rails Applications を読もう
  • Railtieの初期化、集約部分を知りたい
    • ソースを読もう
63
63
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
63
63