Intro to Rails

250 views

Published on

What is Rails ?
What is Rack ?
What happens between the time you type

bundle exec rails s
and when you view localhost:3000 in a browser

Published in: Engineering, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
250
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Intro to Rails

  1. 1. Rails From the beginning to … well .. the beginning
  2. 2. What is Rails ?
  3. 3. What is Rack ?
  4. 4. What are gems ?
  5. 5. bundle exec rails s
  6. 6. $ which rails #!/usr/bin/env ruby_executable_hooks ! require 'rubygems' ! version = ">= 0" ! ……. ! gem 'railties', version load Gem.bin_path('railties', 'rails', version)
  7. 7. script/rails APP_PATH = File.expand_path('../../config/application', __FILE__) require File.expand_path('../../config/boot', __FILE__) require 'rails/commands'
  8. 8. config/boot.rb require 'rubygems' ! # Set up gems listed in the Gemfile. ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) ! require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
  9. 9. rails/commands ARGV << '--help' if ARGV.empty?   aliases = {   "g"  => "generate",   "d"  => "destroy",   "c"  => "console",   "s"  => "server",   "db" => "dbconsole",   "r"  => "runner" }   command = ARGV.shift command = aliases[command] || command   require 'rails/commands/commands_tasks'   Rails::CommandsTasks.new(ARGV).run_command!(command)
  10. 10. COMMAND_WHITELIST = %(plugin server console … help)   def run_command!(command)   if COMMAND_WHITELIST.include?(command)     send(command)   else     write_error_message(command)   end end ‘rails/commands/commands_tasks'
  11. 11. The happy scenario def set_application_directory!   Dir.chdir(File.expand_path('../../', APP_PATH)) unless   File.exist?(File.expand_path("config.ru")) end   def server   set_application_directory!   require_command!("server")     Rails::Server.new.tap do |server|     require APP_PATH     Dir.chdir(Rails.application.root)     server.start   end end   def require_command!(command)   require "rails/commands/#{command}" end
  12. 12. config/application.rb! ! ! ! require File.expand_path(‘../../preinitializer', __FILE__) require File.expand_path('../boot', __FILE__) ! # Pick the frameworks you want: require “active_record/railtie" ! if defined?(Bundler) Bundler.require(*Rails.groups(:assets => %w(development test))) end ! module Share class Application < Rails::Application …. end end
  13. 13. require “rails/commands/server" def start   print_boot_information   trap(:INT) { exit }   create_tmp_directories   log_to_stdout if options[:log_stdout]     super   ... end
  14. 14. print_boot_information def print_boot_information     ...     puts "=> Run `rails server -h` for more startup options"     puts "=> Ctrl-C to shutdown server" unless options[:daemonize] end
  15. 15. create_tmp_directories   def create_tmp_directories     %w(cache pids sessions sockets).each do |dir_to_make|       FileUtils.mkdir_p(File.join(Rails.root, 'tmp', dir_to_make))     end   end
  16. 16. log_to_stdout   def log_to_stdout     wrapped_app # touch the app so the logger is set up       console = ActiveSupport::Logger.new($stdout)     console.formatter = Rails.logger.formatter     console.level = Rails.logger.level       Rails.logger.extend(ActiveSupport::Logger.broadcast(console))   end
  17. 17. Rack::Server def start &blk … wrapped_app … server.run wrapped_app, options, &blk end
  18. 18. wrapped_app • Sets up the config.ru • Sets up the config/environment.rb • Loads up config/environments/RAILS_ENV.rb
  19. 19. config.ru require ::File.expand_path('../config/environment', __FILE__) ! ….
  20. 20. config/environment.rb • This file initialises the application via • Name::Application.initialize! • This actually calls railties/lib/rails/application.rb
  21. 21. railties/lib/rails/application.rb def initialize!(group=:default) #:nodoc: raise "Application has been already initialized." if @initialized run_initializers(group, self) @initialized = true self end
  22. 22. run_initializers(group, self) def run_initializers(group=:default, *args) return if instance_variable_defined?(:@ran) initializers.tsort_each do |initializer| initializer.run(*args) if initializer.belongs_to?(group) end @ran = true end
  23. 23. order of initialisers • You can define order of run • 01_something will be run before 02_something
  24. 24. middlewares • At this point, the rails app itself is a middleware • And now any written middle ware is called • middleware written in config/initializers • called via config/application.rb
  25. 25. routes • The default routes are now loaded • along with the custom routes in config/routes.rb
  26. 26. All files loaded • At this point load order is done • App servers like thin look for public directory to load from unless routes overloads it and points at other file.
  27. 27. Questions ? PLZKTHXBYE
  28. 28. Bonus - Load order |“load boot" |“end loading boot" |“load application.rb" |“end loading application.rb” ! [WEBrick|Thin|Puma] SERVER START ! |“load config.ru" |_ “load config/environment.rb" |__ “load config/environments/development.rb" |__ “end loading config/environments/development.rb" |__ "load an initialiser" |__ “finish loading initialiser” |__ “load routes” |__ “end loading routes" |_ “end loading config/environment.rb" |“end loading config.ru"

×