Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Integração de gems com o Rails @ RS on Rails

Integração de gems com o Rails @ RS on Rails

Carlos Antonio

September 15, 2012
Tweet

More Decks by Carlos Antonio

Other Decks in Programming

Transcript

  1. Railties
    INTEGRANDO GEMS
    COM O RAILS

    View full-size slide

  2. @cantoniodasilva
    Carlos Antonio

    View full-size slide

  3. Devise
    maintainer

    View full-size slide

  4. SimpleForm
    maintainer

    View full-size slide

  5. Responders
    maintainer

    View full-size slide

  6. Rails
    commiter

    View full-size slide

  7. Railties
    INTEGRANDO GEMS
    COM O RAILS

    View full-size slide

  8. The Road So Far...

    View full-size slide

  9. Plugins!
    vendor/plugins

    View full-size slide

  10. script/plugin install
    git://github.com/SomeGuy/
    my_awesome_plugin.git

    View full-size slide

  11. Engines!
    app/*

    View full-size slide

  12. module Rails
    class Plugin
    # Engines are plugins with an app/ directory.
    def engine?
    has_app_directory?
    end
    private
    def has_app_directory?
    File.directory?(File.join(directory, 'app'))
    end
    end
    end
    https://github.com/rails/rails/blob/2-3-stable/railties/lib/rails/plugin.rb

    View full-size slide

  13. module Rails
    class Plugin
    # Engines are plugins with an app/ directory.
    def engine?
    has_app_directory?
    end
    private
    def has_app_directory?
    File.directory?(File.join(directory, 'app'))
    end
    end
    end
    https://github.com/rails/rails/blob/2-3-stable/railties/lib/rails/plugin.rb
    # Engines are plugins with an app/ directory.
    def engine?
    has_app_directory?
    end
    private
    def has_app_directory?
    File.directory?(File.join(directory, 'app'))
    end

    View full-size slide

  14. app/ models
    controllers
    helpers
    config/ routes.rb

    View full-size slide

  15. module Rails
    class Plugin
    def view_path
    File.join(directory, 'app', 'views')
    end
    def controller_path
    File.join(directory, 'app', 'controllers')
    end
    def routing_file
    File.join(directory, 'config', 'routes.rb')
    end
    private
    def app_paths
    [ File.join(directory, 'app', 'models'),
    File.join(directory, 'app', 'helpers'),
    controller_path, metal_path ]
    end
    end
    end
    https://github.com/rails/rails/blob/2-3-stable/railties/lib/rails/plugin.rb

    View full-size slide

  16. module Rails
    class Plugin
    def view_path
    File.join(directory, 'app', 'views')
    end
    def controller_path
    File.join(directory, 'app', 'controllers')
    end
    def routing_file
    File.join(directory, 'config', 'routes.rb')
    end
    private
    def app_paths
    [ File.join(directory, 'app', 'models'),
    File.join(directory, 'app', 'helpers'),
    controller_path, metal_path ]
    end
    end
    end
    https://github.com/rails/rails/blob/2-3-stable/railties/lib/rails/plugin.rb
    def view_path
    File.join(directory, 'app', 'views')
    end
    def controller_path
    File.join(directory, 'app', 'controllers')
    end
    def routing_file
    File.join(directory, 'config', 'routes.rb')
    end
    private
    def app_paths
    [ File.join(directory, 'app', 'models'),
    File.join(directory, 'app', 'helpers'),
    controller_path, metal_path ]
    end

    View full-size slide

  17. Rails
    Active Record

    View full-size slide

  18. module Rails
    class Initializer
    def load_observers
    if gems_dependencies_loaded &&
    configuration.frameworks.include?(:active_record)
    ActiveRecord::Base.instantiate_observers
    end
    end
    def initialize_database
    if configuration.frameworks.include?(:active_record)
    ActiveRecord::Base.configurations =
    configuration.database_configuration
    ActiveRecord::Base.establish_connection
    end
    end
    end
    end
    https://github.com/rails/rails/blob/2-3-stable/railties/lib/initializer.rb

    View full-size slide

  19. module Rails
    class Initializer
    def load_observers
    if gems_dependencies_loaded &&
    configuration.frameworks.include?(:active_record)
    ActiveRecord::Base.instantiate_observers
    end
    end
    def initialize_database
    if configuration.frameworks.include?(:active_record)
    ActiveRecord::Base.configurations =
    configuration.database_configuration
    ActiveRecord::Base.establish_connection
    end
    end
    end
    end
    https://github.com/rails/rails/blob/2-3-stable/railties/lib/initializer.rb
    if gems_dependencies_loaded &&
    configuration.frameworks.include?(:active_record)
    ActiveRecord::Base.instantiate_observers
    end
    if configuration.frameworks.include?(:active_record)
    ActiveRecord::Base.configurations =
    configuration.database_configuration
    ActiveRecord::Base.establish_connection
    end

    View full-size slide

  20. module Rails
    class Initializer
    private
    def default_frameworks
    [ :active_record, :action_controller,
    :action_view, :action_mailer, :active_resource ]
    end
    end
    end
    https://github.com/rails/rails/blob/2-3-stable/railties/lib/initializer.rb

    View full-size slide

  21. module Rails
    class Initializer
    private
    def default_frameworks
    [ :active_record, :action_controller,
    :action_view, :action_mailer, :active_resource ]
    end
    end
    end
    https://github.com/rails/rails/blob/2-3-stable/railties/lib/initializer.rb
    def default_frameworks
    [ :active_record, :action_controller,
    :action_view, :action_mailer, :active_resource ]
    end

    View full-size slide

  22. ORMs
    Sequel MongoMapper
    DataMapper Mongoid

    View full-size slide

  23. module Rails
    class Initializer
    def process
    Rails.configuration = configuration
    check_ruby_version
    install_gem_spec_stubs
    set_load_path
    add_gem_load_paths
    require_frameworks
    set_autoload_paths
    add_plugin_load_paths
    load_environment
    preload_frameworks
    initialize_encoding
    initialize_database
    initialize_cache
    initialize_framework_caches
    initialize_logger
    initialize_framework_logging
    initialize_dependency_mechanism
    initialize_whiny_nils
    initialize_time_zone
    initialize_i18n
    initialize_framework_settings
    initialize_framework_views
    initialize_metal
    add_support_load_paths
    check_for_unbuilt_gems
    load_gems
    load_plugins
    https://github.com/rails/rails/blob/2-3-stable/railties/lib/initializer.rb
    # pick up any gems that plugins depend on
    add_gem_load_paths
    load_gems
    check_gem_dependencies
    # bail out if gems are missing - note that check_gem_dependencies
    # will have already called abort() unless $gems_rake_task is set
    return unless gems_dependencies_loaded
    load_application_initializers
    # the framework is now fully initialized
    after_initialize
    # Setup database middleware after initializers have run
    initialize_database_middleware
    # Prepare dispatcher callbacks and run 'prepare' callbacks
    prepare_dispatcher
    # Routing must be initialized after plugins to allow the former to
    # extend the routes
    initialize_routing
    # Observers are loaded after plugins in case Observers or observed
    # models are modified by plugins.
    load_observers
    # Load view path cache
    load_view_paths
    # Load application classes
    load_application_classes
    # Disable dependency loading during request cycle
    disable_dependency_loading
    # Flag initialized
    Rails.initialized = true
    end
    end
    end

    View full-size slide

  24. Single
    Responsibility
    Principle

    View full-size slide

  25. Single
    Responsibility
    Principle

    View full-size slide

  26. Modularidade

    View full-size slide

  27. Rails
    Active Record

    View full-size slide

  28. Rails
    Active Record

    View full-size slide

  29. Rails
    Action Controller
    Action Mailer
    Action View
    Active Record
    Active Resource

    View full-size slide

  30. Rails
    DataMapper
    Devise
    RSpec
    Mongoid
    etc...

    View full-size slide

  31. Rails::
    Railtie

    View full-size slide

  32. Configuração
    &
    Inicialização

    View full-size slide

  33. module Responders
    class Railtie < ::Rails::Railtie
    config.responders = ActiveSupport::OrderedOptions.new
    config.responders.flash_keys = [ :notice, :alert ]
    # ...
    end
    end
    https://github.com/plataformatec/responders/blob/v0.9.2/lib/responders.rb
    Responders

    View full-size slide

  34. module MyApp
    class Application < ::Rails::Application
    config.responders.flash_keys = [ :success, :failure ]
    end
    end

    View full-size slide

  35. Initializers

    View full-size slide

  36. module Responders
    class Railtie < ::Rails::Railtie
    config.responders.flash_keys = [ :notice, :alert ]
    initializer "responders.flash_responder" do |app|
    Responders::FlashResponder.flash_keys =
    app.config.responders.flash_keys
    # ...
    end
    end
    end
    https://github.com/plataformatec/responders/blob/v0.9.2/lib/responders.rb
    Responders

    View full-size slide

  37. module CarrierWave
    class Railtie < Rails::Railtie
    initializer "carrierwave.setup_paths" do
    CarrierWave.root =
    Rails.root.join(Rails.public_path).to_s
    # ...
    end
    end
    end
    https://github.com/jnicklas/carrierwave/blob/v0.6.2/lib/carrierwave.rb
    CarrierWave

    View full-size slide

  38. Initializers
    + Hooks

    View full-size slide

  39. module MyGem
    class Railtie < Rails::Railtie
    config.before_configuration {
    # Executa antes das configurações do usuário
    }
    end
    end

    View full-size slide

  40. module MyGem
    class Railtie < Rails::Railtie
    config.before_initialize {
    # Depois da config do usuário, mas antes dos
    # initializers.
    }
    end
    end

    View full-size slide

  41. module MyGem
    class Railtie < Rails::Railtie
    config.to_prepare {
    # cache_classes = false (development)
    # Roda toda vez antes de recarregar o ambiente.
    # cache_classes = true (production)
    # Roda antes da primeira requisição.
    }
    end
    end

    View full-size slide

  42. module MyGem
    class Railtie < Rails::Railtie
    config.before_eager_load {
    # Antes de carregar todo o código da aplicação,
    # somente com cache_classes true.
    }
    end
    end

    View full-size slide

  43. module MyGem
    class Railtie < Rails::Railtie
    config.after_initialize {
    # Após todos os demais (configs, initializers, etc),
    # uma única vez antes da primeira requisição.
    }
    end
    end

    View full-size slide

  44. Development
    before_configuration
    => Booting WEBrick
    before_initialize
    to_prepare
    after_initialize
    [...] INFO WEBrick 1.3.1
    to_prepare
    Request...
    to_prepare
    Request...
    to_prepare
    Production (cache_classes)
    before_configuration
    => Booting WEBrick
    before_initialize
    to_prepare
    before_eager_load
    after_initialize
    [...] INFO WEBrick 1.3.1
    Request...
    Request...

    View full-size slide

  45. Development
    before_configuration
    => Booting WEBrick
    before_initialize
    to_prepare
    after_initialize
    [...] INFO WEBrick 1.3.1
    to_prepare
    Request...
    to_prepare
    Request...
    to_prepare
    Production (cache_classes)
    before_configuration
    => Booting WEBrick
    before_initialize
    to_prepare
    before_eager_load
    after_initialize
    [...] INFO WEBrick 1.3.1
    Request...
    Request...

    View full-size slide

  46. module ActiveRecord
    class Railtie < Rails::Railtie
    config.after_initialize do
    ActiveSupport.on_load(:active_record) do
    instantiate_observers
    ActionDispatch::Reloader.to_prepare do
    ActiveRecord::Base.instantiate_observers
    end
    end
    end
    end
    end
    https://github.com/rails/rails/tree/3-2-stable/activerecord/lib/active_record/railtie.rb
    Active Record

    View full-size slide

  47. module Rails
    module Mongoid
    class Railtie < Rails::Railtie
    initializer "instantiate observers" do
    config.after_initialize do
    ::Mongoid::instantiate_observers
    ActionDispatch::Reloader.to_prepare do
    ::Mongoid.instantiate_observers
    end
    end
    end
    end
    end
    end
    https://github.com/mongoid/mongoid/blob/3.0.0-stable/lib/mongoid/railtie.rb
    Mongoid

    View full-size slide

  48. module Haml
    module Rails
    class Railtie < ::Rails::Railtie
    config.before_initialize do
    Haml.init_rails(binding)
    Haml::Template.options[:format] = :html5
    end
    end
    end
    end
    https://github.com/indirect/haml-rails/blob/v0.3.5/lib/haml-rails.rb
    Haml

    View full-size slide

  49. simple_form
    lib
    generators
    simple_form
    install_generator.rb
    https://github.com/plataformatec/simple_form
    SimpleForm

    View full-size slide

  50. simple_form
    lib
    generators
    simple_form
    install_generator.rb
    Namespace
    https://github.com/plataformatec/simple_form
    SimpleForm

    View full-size slide

  51. simple_form
    lib
    generators
    simple_form
    install_generator.rb
    Namespace
    Generator
    https://github.com/plataformatec/simple_form
    SimpleForm

    View full-size slide

  52. module SimpleForm
    module Generators
    class InstallGenerator < Rails::Generators::Base
    desc "Copy SimpleForm default files"
    source_root File.expand_path('../templates', __FILE__)
    def copy_config
    directory 'config'
    end
    def copy_scaffold_template
    # ...
    end
    end
    end
    end
    https://github.com/plataformatec/simple_form/blob/v2.0.2/lib/generators/simple_form/install_generator.rb
    SimpleForm

    View full-size slide

  53. rails generate
    simple_form:install

    View full-size slide

  54. Convenções

    View full-size slide

  55. module MyGem
    class Railtie < ::Rails::Railtie
    generators do
    require "my_gem/generators"
    end
    end
    end

    View full-size slide

  56. Generators
    + Hooks

    View full-size slide

  57. module ActiveRecord
    class Railtie < Rails::Railtie
    config.app_generators.orm :active_record,
    :migration => true, :timestamps => true
    end
    end
    https://github.com/rails/rails/tree/3-2-stable/activerecord/lib/active_record/railtie.rb
    Active Record

    View full-size slide

  58. module Rails
    module Mongoid
    class Railtie < Rails::Railtie
    def self.generator
    config.respond_to?(:app_generators) ?
    :app_generators : :generators
    end
    config.send(generator).orm :mongoid,
    migration: false
    end
    end
    end
    https://github.com/mongoid/mongoid/blob/3.0.0-stable/lib/mongoid/railtie.rb
    Mongoid

    View full-size slide

  59. module RSpec
    module Rails
    class Railtie < ::Rails::Railtie
    generators = config.respond_to?(:app_generators) ?
    config.app_generators : config.generators
    generators.integration_tool :rspec
    generators.test_framework :rspec
    end
    end
    end
    https://github.com/rspec/rspec-rails/blob/v2.11.0/lib/rspec-rails.rb
    RSpec

    View full-size slide

  60. module Responders
    class Railtie < ::Rails::Railtie
    if config.respond_to?(:app_generators)
    config.app_generators.scaffold_controller =
    :responders_controller
    else
    config.generators.scaffold_controller =
    :responders_controller
    end
    end
    end
    https://github.com/plataformatec/responders/blob/v0.9.2/lib/responders.rb
    Responders

    View full-size slide

  61. module Haml
    module Rails
    class Railtie < ::Rails::Railtie
    if ::Rails.version.to_f >= 3.1
    config.app_generators.template_engine :haml
    else
    config.generators.template_engine :haml
    end
    end
    end
    end
    https://github.com/indirect/haml-rails/blob/v0.3.5/lib/haml-rails.rb
    Haml

    View full-size slide

  62. :assets => true
    :force_plural => false
    :helper => true
    :integration_tool => nil
    :javascripts => true
    :javascript_engine => :js
    :orm => false
    :performance_tool => nil
    :resource_controller => :controller
    :resource_route => true
    :scaffold_controller => :scaffold_controller
    :stylesheets => true
    :stylesheet_engine => :css
    :test_framework => false
    :template_engine => :erb
    https://github.com/rails/rails/blob/3-2-stable/railties/lib/rails/generators.rb

    View full-size slide

  63. module MyGem
    class Railtie < Rails::Railtie
    console do
    puts "Hello console!"
    end
    end
    end

    View full-size slide

  64. module ActiveRecord
    class Railtie < Rails::Railtie
    console do |app|
    if app.sandbox?
    require "active_record/railties/console_sandbox"
    end
    ActiveRecord::Base.logger = Logger.new(STDERR)
    end
    end
    end
    https://github.com/rails/rails/tree/3-2-stable/activerecord/lib/active_record/railtie.rb
    Active Record

    View full-size slide

  65. module PryRails
    class Railtie < Rails::Railtie
    console do
    # ... código para carregar o Pry
    end
    end
    end
    https://github.com/rweng/pry-rails/blob/master/lib/pry-rails/railtie.rb
    Pry Rails

    View full-size slide

  66. Rails
    4
    SPOILER

    View full-size slide

  67. RAILS 4
    https://github.com/rails/rails/commit/951b582

    View full-size slide

  68. module PryRails
    class Railtie < Rails::Railtie
    console do
    if Rails::VERSION::MAJOR == 4
    Rails.application.config.console = Pry
    end
    end
    end
    end
    https://github.com/rweng/pry-rails/blob/master/lib/pry-rails/railtie.rb
    Pry Rails

    View full-size slide

  69. module RSpec
    module Rails
    class Railtie < ::Rails::Railtie
    rake_tasks do
    load "rspec/rails/tasks/rspec.rake"
    end
    end
    end
    end
    https://github.com/rspec/rspec-rails/blob/v2.11.0/lib/rspec-rails.rb
    RSpec

    View full-size slide

  70. module ActiveRecord
    class Railtie < Rails::Railtie
    rake_tasks do
    load "active_record/railties/databases.rake"
    end
    end
    end
    https://github.com/rails/rails/tree/3-2-stable/activerecord/lib/active_record/railtie.rb
    Active Record

    View full-size slide

  71. module Rails
    module Mongoid
    class Railtie < Rails::Railtie
    rake_tasks do
    load "mongoid/railties/database.rake"
    end
    end
    end
    end
    https://github.com/mongoid/mongoid/blob/3.0.0-stable/lib/mongoid/railtie.rb
    Mongoid

    View full-size slide

  72. Framework
    Loading

    View full-size slide

  73. module MyGem
    module SimpleFormExtensions
    def special_input(*)
    # ...
    end
    end
    end
    SimpleForm::FormBuilder.send(
    :include, MyGem::SimpleFormExtensions)

    View full-size slide

  74. module MyGem
    module SimpleFormExtensions
    def special_input(*)
    # ...
    end
    end
    end
    SimpleForm::FormBuilder.send(
    :include, MyGem::SimpleFormExtensions)

    View full-size slide

  75. module SimpleForm
    class FormBuilder
    end
    end
    ActiveSupport.run_load_hooks(
    :simple_form, SimpleForm::FormBuilder)

    View full-size slide

  76. module MyGem
    module SimpleFormExtensions
    def special_input(*)
    # ...
    end
    end
    end
    ActiveSupport.on_load(:simple_form) do
    include MyGem::SimpleFormExtensions
    end

    View full-size slide

  77. module ActiveRecord
    class Base
    end
    end
    ActiveSupport.run_load_hooks(
    :active_record, ActiveRecord::Base)
    https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/base.rb
    Active Record

    View full-size slide

  78. module ActiveRecord
    class Railtie < Rails::Railtie
    initializer "active_record.initialize_timezone" do
    ActiveSupport.on_load(:active_record) do
    self.time_zone_aware_attributes = true
    self.default_timezone = :utc
    end
    end
    end
    end
    https://github.com/rails/rails/tree/3-2-stable/activerecord/lib/active_record/railtie.rb
    Active Record

    View full-size slide

  79. module MyGem
    module ActiveRecordExtensions
    end
    end
    ActiveSupport.on_load(:active_record) do
    include MyGem::ActiveRecordExtensions
    end

    View full-size slide

  80. ActiveSupport.on_load(:active_record) do
    include MyGem::ActiveRecordExtensions
    end
    ActiveSupport.on_load(:action_controller) do
    include MyGem::ActionControllerExtensions
    end

    View full-size slide

  81. Criar uma
    Railtie?

    View full-size slide

  82. module MyGem
    class Railtie < Rails::Railtie
    end
    end

    View full-size slide

  83. Você nem sempre
    precisa de uma
    Railtie!

    View full-size slide

  84. Initializers
    Railtie: quando?

    View full-size slide

  85. rake tasks
    Railtie: quando?

    View full-size slide

  86. Expor configs
    de sua gem
    Railtie: quando?

    View full-size slide

  87. Extensões para o
    console
    Railtie: quando?

    View full-size slide

  88. Generators em outro
    diretório
    Railtie: quando?

    View full-size slide

  89. Alterar um
    generator default
    Railtie: quando?

    View full-size slide

  90. Rails::
    Engine

    View full-size slide

  91. Engine < Railtie

    View full-size slide

  92. Paths + Autoload

    View full-size slide

  93. app/ *
    assets
    controllers
    helpers
    mailers
    models
    views

    View full-size slide

  94. https://github.com/plataformatec/devise/tree/v2.1.2/app

    View full-size slide

  95. config/ environments/
    #{Rails.env}.rb
    initializers/*.rb
    locales/*.{rb,yml}
    routes.rb

    View full-size slide

  96. https://github.com/plataformatec/devise/tree/v2.1.2/config

    View full-size slide

  97. lib/ assets/*
    tasks/*.rake
    vendor/ assets/*

    View full-size slide

  98. https://github.com/rails/jquery-rails/tree/v2.1.2/vendor/assets/javascripts

    View full-size slide

  99. module MyGem
    class Railtie < ::Rails::Engine
    # Você pode alterar os paths padrão.
    paths.app.controllers << "lib/controllers"
    end
    end

    View full-size slide

  100. Criar uma
    Engine?

    View full-size slide

  101. module MyGem
    class Railtie < ::Rails::Engine
    end
    end

    View full-size slide

  102. module Jquery
    module Rails
    class Engine < ::Rails::Engine
    end
    end
    end
    https://github.com/rails/jquery-rails/blob/v2.1.2/lib/jquery/rails/engine.rb
    Jquery Rails

    View full-size slide

  103. module Devise
    class Engine < ::Rails::Engine
    config.devise = Devise
    # Force routes to be loaded if we are doing any
    # eager load.
    config.before_eager_load { |app| app.reload_routes! }
    initializer "devise.url_helpers" do
    Devise.include_helpers(Devise::Controllers)
    end
    initializer "devise.omniauth" do |app|
    # ...
    end
    # ...
    end
    end https://github.com/plataformatec/devise/blob/v2.1.2/lib/devise/rails.rb
    Devise

    View full-size slide

  104. Quando eu preciso
    de uma Engine?

    View full-size slide

  105. Rotas
    Railtie: quando?

    View full-size slide

  106. Models
    Railtie: quando?

    View full-size slide

  107. Controllers
    Railtie: quando?

    View full-size slide

  108. Views
    Railtie: quando?

    View full-size slide

  109. app/*
    Railtie: quando?

    View full-size slide

  110. config/*
    Railtie: quando?

    View full-size slide

  111. Rails::
    Plugin

    View full-size slide

  112. Plugin < Engine

    View full-size slide

  113. rails plugin install

    View full-size slide

  114. vendor/plugins

    View full-size slide

  115. Criar uma
    Plugin?

    View full-size slide

  116. Você não cria :)

    View full-size slide

  117. module Rails
    class Plugin < Engine
    def self.inherited(base)
    raise "You cannot inherit from Rails::Plugin"
    end
    end
    end
    https://github.com/rails/rails/blob/3-2-stable/railties/lib/rails/plugin.rb

    View full-size slide

  118. module Rails
    class Plugin < Engine
    def self.inherited(base)
    raise "You cannot inherit from Rails::Plugin"
    end
    end
    end
    https://github.com/rails/rails/blob/3-2-stable/railties/lib/rails/plugin.rb
    def self.inherited(base)
    raise "You cannot inherit from Rails::Plugin"
    end

    View full-size slide

  119. Lembre-se:
    interno ao Rails :)

    View full-size slide

  120. Quando eu preciso
    de um Plugin?

    View full-size slide

  121. Você não precisa
    (não mais :)

    View full-size slide

  122. Rails
    4
    SPOILER

    View full-size slide

  123. RAILS 4
    https://github.com/rails/rails/commit/dad7fdc

    View full-size slide

  124. Rails::
    Application

    View full-size slide

  125. Application < Engine

    View full-size slide

  126. require
    config/boot.rb
    Rails boot

    View full-size slide

  127. require
    railties + engines
    Rails boot

    View full-size slide

  128. Define
    Rails.application
    Rails boot

    View full-size slide

  129. Run
    before_configuration
    Rails boot

    View full-size slide

  130. Load
    config/environment/ENV.rb
    Rails boot

    View full-size slide

  131. Run
    before_initialize
    Rails boot

    View full-size slide

  132. Initialize
    railties
    Rails boot

    View full-size slide

  133. Configure
    railties
    Rails boot

    View full-size slide

  134. Run
    railties#initializers
    Rails boot

    View full-size slide

  135. Build
    middleware
    Rails boot

    View full-size slide

  136. Run
    to_prepare
    Rails boot

    View full-size slide

  137. Run
    before_eager_load
    Rails boot

    View full-size slide

  138. Run
    after_initialize
    Rails boot

    View full-size slide

  139. Criar uma
    Application?

    View full-size slide

  140. rails new my_app

    View full-size slide

  141. module MyApp
    class Application < Rails::Application
    end
    end

    View full-size slide

  142. >> require 'rails/all'
    => true
    >> class MyApp < Rails::Application; end
    => nil
    >> Rails.application
    => #
    >> class OtherApp < Rails::Application; end
    RuntimeError:
    You cannot have more than one Rails::Application

    View full-size slide

  143. Single
    Responsibility
    Principle

    View full-size slide

  144. Criando uma gem

    View full-size slide

  145. $ bundle gem rsonrails
    create rsonrails/Gemfile
    create rsonrails/Rakefile
    create rsonrails/LICENSE.txt
    create rsonrails/README.md
    create rsonrails/.gitignore
    create rsonrails/rsonrails.gemspec
    create rsonrails/lib/rsonrails.rb
    create rsonrails/lib/rsonrails/version.rb
    Initializating git repo in /Users/carlos/rsonrails
    bundle gem

    View full-size slide

  146. Copyright (c) 2012 Carlos Antonio da Silva
    MIT License
    Permission is hereby granted, free of
    charge, to any person obtaining...
    License

    View full-size slide

  147. # Rsonrails
    TODO: Write a gem description
    ## Installation
    Add this line to your application's Gemfile:
    gem 'rsonrails'
    ...
    ## Usage
    ...
    ## Contributing
    ...
    Readme

    View full-size slide

  148. source 'https://rubygems.org'
    # Specify your gem's dependencies in
    rsonrails.gemspec
    gemspec
    Gemfile

    View full-size slide

  149. # -*- encoding: utf-8 -*-
    lib = File.expand_path('../lib', __FILE__)
    $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
    require 'rsonrails/version'
    Gem::Specification.new do |gem|
    gem.name = "rsonrails"
    gem.version = Rsonrails::VERSION
    gem.authors = ["Carlos Antonio da Silva"]
    gem.email = ["[email protected]"]
    gem.description = %q{TODO: Write a gem description}
    gem.summary = %q{TODO: Write a gem summary}
    gem.homepage = ""
    gem.files = `git ls-files`.split($/)
    gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
    gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
    gem.require_paths = ["lib"]
    end
    rsonrails.gemspec

    View full-size slide

  150. require "rsonrails/version"
    module Rsonrails
    # Your code goes here...
    end
    lib/rsonrails.rb

    View full-size slide

  151. module Rsonrails
    VERSION = "0.0.1"
    end
    lib/rsonrails/version.rb

    View full-size slide

  152. module Rsonrails
    class Railtie < Rails::Railtie
    # config.rsonrails =
    ActiveSupport::OrderedOptions.new
    # rake_tasks {}
    # config.to_prepare {}
    # initializer {}
    end
    end
    lib/rsonrails.rb

    View full-size slide

  153. module Rsonrails
    class Railtie < Rails::Railtie
    # config.rsonrails =
    ActiveSupport::OrderedOptions.new
    # rake_tasks {}
    # config.to_prepare {}
    # initializer {}
    end
    end
    require "rsonrails/version"
    require "rsonrails/railtie"
    module Rsonrails
    # Your code goes here...
    end
    lib/rsonrails/railtie.rb
    lib/rsonrails.rb

    View full-size slide

  154. require "rsonrails/version"
    if defined?(Rails)
    require "rsonrails/railtie"
    end
    module Rsonrails
    # Your code goes here...
    end
    lib/rsonrails.rb

    View full-size slide

  155. require "bundler/gem_tasks"
    Rakefile

    View full-size slide

  156. $ rake -T
    rake build # Build rsonrails-0.0.1.gem
    into the pkg directory
    rake install # Build and install rsonrails-0.0.1.gem
    into system gems
    rake release # Create tag v0.0.1 and build and push
    rsonrails-0.0.1.gem to Rubygems
    rake tasks

    View full-size slide

  157. Bundler
    <3 <3 <3

    View full-size slide

  158. Railtie
    Engine
    Application

    View full-size slide

  159. plataformatec/
    simple_form

    View full-size slide

  160. plataformatec/
    responders

    View full-size slide

  161. plataformatec/
    devise

    View full-size slide

  162. rails/rails/
    **/*railtie.rb

    View full-size slide

  163. http://pragprog.com/book/jvrails/crafting-rails-applications

    View full-size slide

  164. WITH GREAT
    MOUSTACHE
    COMES GREAT
    RESPONSIBILITY

    View full-size slide

  165. Estamos contratando!
    plataformatec.com.br

    View full-size slide

  166. INTEGRANDO GEMS
    COM O RAILS
    @cantoniodasilva
    Obrigado!

    View full-size slide