Plug it on!... with railties

1,387 views

Published on

Slides for the MagmaRails conference @ colima, october 15th

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,387
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide













































  • Plug it on!... with railties

    1. 1. Plug it on!... with Railties
    2. 2. Edgar J. Suárez ['twitter.com','github.com', 'rails.mx'].each do |host|   link_to "#{host}/edgarjs" end mail_to "edgar.js@gmail.com"
    3. 3. Rail-tie Rail (HDR) @ sadi-junior / flickr.com
    4. 4. .init! Endeavour launch @ msimdottv / flickr.com
    5. 5. rails server
    6. 6. script/server require ‘config/application’ require ‘config/boot’
    7. 7. config/boot.rb Bundler.setup
    8. 8. config/application.rb require ‘rails/all’
    9. 9. rails/all.rb require ‘rails’
    10. 10. rails.rb require ‘pathname’ require ‘active_support’ ... require ‘rails/application’ ...
    11. 11. rails/application.rb require ‘rails/railties_path’ require ‘rails/plugin’ require ‘rails/engine’
    12. 12. rails/engine.rb require ‘rails/railtie’
    13. 13. rails/railtie.rb require ‘rails/initializable’ require ‘rails/configuration’
    14. 14. rails/configuration.rb require ‘rails/paths’ require ‘rails/rack’
    15. 15. rails/all.rb require 'rails' %w( active_record  action_controller  action_mailer   active_resource   rails/test_unit ).each do |framework|   begin     require "#{framework}/railtie"   rescue LoadError   end end
    16. 16. rails new your_app -O config/application.rb require 'rails/all'
    17. 17. rails new your_app -O config/application.rb require 'rails' # require 'active_record/railtie' require 'action_controller/railtie' require 'action_mailer/railtie' require 'active_resource/railtie' require 'rails/test_unit/railtie'
    18. 18. Modular origami : the collection so far @ fdecomite / flickr.com
    19. 19. Lego Trains @ psd / flickr.com
    20. 20. Railtie + Engine + Application + Plugin = MODULAR RAILS
    21. 21. Railtie
    22. 22. Railtie Initializers Configuration Rake tasks Generators
    23. 23. Initializers initializer "my_railtie.initialize" do |app|   app.middleware.use MyRailtie::Middleware end
    24. 24. Configuration config.generators.orm :my_railtie_orm # Add a to_prepare block which is executed once in production # and before which request in development config.to_prepare do   MyRailtie.setup! end
    25. 25. Rake tasks rake_tasks do   load "path/to/my_railtie.tasks" end
    26. 26. Generators generators do   require "path/to/my_railtie_generator" end
    27. 27. Engine < Railtie Initializers Configuration Paths Rake tasks Generators
    28. 28. Paths paths.app                 = "app" paths.app.controllers     = "app/controllers" paths.app.helpers         = "app/helpers" paths.app.models          = "app/models" paths.app.views           = "app/views" paths.lib                 = "lib" paths.lib.tasks           = "lib/tasks" paths.config              = "config" paths.config.initializers = "config/initializers" paths.config.locales      = "config/locales" paths.config.routes       = "config/routes.rb"
    29. 29. Application < Engine Initializers Configuration Holds routes Rake tasks Builds middleware stack Generators Paths
    30. 30. config/application.rb module YourRailsApp class Application < Rails::Application   end end
    31. 31. Plugin < Engine
    32. 32. Plugin To be continued... It's so damn Sexy @ picsaremine / flickr.com
    33. 33. .init! Endeavour launch @ msimdottv / flickr.com
    34. 34. config/environment.rb YourApp::Application.initialize!
    35. 35. YourApp::Application.initialize! initializers.each do |initializer|   initializer.run(*args) end def initializers railties.all { |r| initializers += r.initializers } end def all(&block)   @all ||= railties + engines + plugins   @all.each(&block) if block   @all end
    36. 36. railties + engines + plugins def railties   @railties ||= ::Rails::Railtie.subclasses.map(&:new) end def engines   @engines ||= ::Rails::Engine.subclasses.map(&:new) end def plugins  @plugins ||= begin     plugin_names = (@config.plugins || [:all]).map { |p| p.to_sym }     Plugin.all(plugin_names, @config.paths.vendor.plugins)   end end
    37. 37. Plugin.all def self.all(list, paths)   plugins = []   paths.each do |path|     Dir["#{path}/*"].each do |plugin_path|       plugin = new(plugin_path)       next unless list.include?(plugin.name) || list.include?(:all)       plugins << plugin     end   end   plugins.sort_by do |p|     [list.index(p.name) || list.index(:all), p.name.to_s]   end end
    38. 38. Plugin.new def initialize(root)   @name = File.basename(root).to_sym   config.root = root end initializer :load_init_rb, :before => :load_application_initializers do |app|   file   = Dir["#{root}/{rails/init,init}.rb"].first   config = app.config   eval(File.read(file), binding, file) if file && File.file?(file) end
    39. 39. How to make your Railtie
    40. 40. # railtie_name/lib/railtie_name/railtie.rb require 'rails' module RailtieName   class Railtie < Rails::Railtie     config.some_config = true     initializer "railtie_name.initializating" do |app|       # your initialization here     end   end end
    41. 41. require 'railtie_name/railtie'
    42. 42. Demo
    43. 43. Thanks! http://twitter.com/edgarjs http://github.com/edgarjs http://rails.mx/edgarjs http://freshout.us edgar.js@gmail.com

    ×