Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

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. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. module MyGem class Railtie < Rails::Railtie config.before_initialize { # Depois

    da config do usuário, mas antes dos # initializers. } end end
  14. 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
  15. 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
  16. 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
  17. 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...
  18. 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...
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. :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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. module MyGem module SimpleFormExtensions def special_input(*) # ... end end

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

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

    end ActiveSupport.on_load(:simple_form) do include MyGem::SimpleFormExtensions end
  38. 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
  39. module MyGem class Railtie < ::Rails::Engine # Você pode alterar

    os paths padrão. paths.app.controllers << "lib/controllers" end end
  40. 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
  41. 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
  42. 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
  43. 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
  44. >> 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
  45. $ 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
  46. Copyright (c) 2012 Carlos Antonio da Silva MIT License Permission

    is hereby granted, free of charge, to any person obtaining... License
  47. # Rsonrails TODO: Write a gem description ## Installation Add

    this line to your application's Gemfile: gem 'rsonrails' ... ## Usage ... ## Contributing ... Readme
  48. # -*- 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
  49. module Rsonrails class Railtie < Rails::Railtie # config.rsonrails = ActiveSupport::OrderedOptions.new

    # rake_tasks {} # config.to_prepare {} # initializer {} end end lib/rsonrails.rb
  50. 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
  51. $ 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