Integração de gems com o Rails

7c12adb8b5521c060ab4630360a4fa27?s=47 Plataformatec
September 15, 2012

Integração de gems com o Rails

Por Carlos Antonio, no RS on Rails 2012.

7c12adb8b5521c060ab4630360a4fa27?s=128

Plataformatec

September 15, 2012
Tweet

Transcript

  1. 3.
  2. 8.
  3. 11.
  4. 16.

    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
  5. 17.

    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
  6. 18.
  7. 20.

    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
  8. 21.

    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
  9. 23.

    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
  10. 24.

    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
  11. 25.

    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
  12. 26.

    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
  13. 27.
  14. 29.
  15. 31.

    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
  16. 34.
  17. 38.
  18. 40.
  19. 42.
  20. 45.
  21. 46.

    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
  22. 49.

    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
  23. 50.

    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
  24. 53.

    module MyGem class Railtie < Rails::Railtie config.before_initialize { # Depois

    da config do usuário, mas antes dos # initializers. } end end
  25. 54.

    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
  26. 55.

    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
  27. 56.

    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
  28. 57.

    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...
  29. 58.

    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...
  30. 59.

    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
  31. 60.

    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
  32. 61.

    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
  33. 66.

    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
  34. 71.

    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
  35. 72.

    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
  36. 73.

    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
  37. 74.

    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
  38. 75.

    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
  39. 76.

    :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
  40. 77.
  41. 79.

    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
  42. 80.

    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
  43. 83.

    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
  44. 85.

    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
  45. 86.

    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
  46. 87.

    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
  47. 89.

    module MyGem module SimpleFormExtensions def special_input(*) # ... end end

    end SimpleForm::FormBuilder.send( :include, MyGem::SimpleFormExtensions)
  48. 90.

    module MyGem module SimpleFormExtensions def special_input(*) # ... end end

    end SimpleForm::FormBuilder.send( :include, MyGem::SimpleFormExtensions)
  49. 92.

    module MyGem module SimpleFormExtensions def special_input(*) # ... end end

    end ActiveSupport.on_load(:simple_form) do include MyGem::SimpleFormExtensions end
  50. 94.

    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
  51. 115.

    module MyGem class Railtie < ::Rails::Engine # Você pode alterar

    os paths padrão. paths.app.controllers << "lib/controllers" end end
  52. 118.

    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
  53. 119.

    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
  54. 131.
  55. 134.

    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
  56. 135.

    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
  57. 141.
  58. 144.
  59. 158.
  60. 162.

    >> require 'rails/all' => true >> class MyApp < Rails::Application;

    end => nil >> Rails.application => #<MyApp:0x007f99b98c8728 @initialized=false ... > >> class OtherApp < Rails::Application; end RuntimeError: You cannot have more than one Rails::Application
  61. 164.
  62. 166.
  63. 167.

    $ 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
  64. 168.

    Copyright (c) 2012 Carlos Antonio da Silva MIT License Permission

    is hereby granted, free of charge, to any person obtaining... License
  65. 169.

    # Rsonrails TODO: Write a gem description ## Installation Add

    this line to your application's Gemfile: gem 'rsonrails' ... ## Usage ... ## Contributing ... Readme
  66. 171.

    # -*- 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 = ["carlosantoniodasilva@gmail.com"] 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
  67. 174.
  68. 175.

    module Rsonrails class Railtie < Rails::Railtie # config.rsonrails = ActiveSupport::OrderedOptions.new

    # rake_tasks {} # config.to_prepare {} # initializer {} end end lib/rsonrails.rb
  69. 176.

    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
  70. 177.
  71. 180.

    $ 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
  72. 181.
  73. 183.
  74. 185.
  75. 193.