Intro
When you run ‘rails new helloworld’ it creates 41 folder, 41 files and 10 gem dependencies! Below is a default Rails 4.1.6 project contents presented (comments edited for brevity) and demystified.
Simplified Flow
bin/rails > boot.rb > application.rb > environment.rb > server started
Ruby Files
Ignoring backtrace_silencers.rb, inflections.rb and mime_types.rb (empty).
/config/application.rb
Runs boot and loads all gem files. Called by the rails executable.
require File.expand_path(‘../boot’, __FILE__) require ‘rails/all’ Bundler.require(*Rails.groups) module HelloWorld class Application < Rails::Application end end
/config/boot.rb
Sets environment variable to the location of the Gemfile then calls Bundler setup, which uses ENV to find the dependencies.
ENV[‘BUNDLE_GEMFILE’] ||= File.expand_path(‘../../Gemfile’, __FILE__) require ‘bundler/setup’ if File.exist?(ENV[‘BUNDLE_GEMFILE’])
/config/environment.rb
Generic config file defining the application. Runs all initialiser classes.
require File.expand_path(‘../application’, __FILE__) Rails.application.initialize!
/config/routes.rb
Map requests to controllers.
Rails.application.routes.draw do end
/config/environments/development.rb
Overriding /config/application.rb settings when run in development mode.
Rails.application.configure do # Changes don’t require a server restart config.cache_classes = false # Do not eager load code on boot. config.eager_load = false # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false # Don’t care if the mailer can’t send. config.action_mailer.raise_delivery_errors = false # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log # Raise an error on page load if there are pending migrations. config.active_record.migration_error = :page_load # Debug mode disables concatenation and preprocessing of assets. config.assets.debug = true # Adds additional error checking when serving assets at runtime. config.assets.raise_runtime_errors = true # Raises error for missing translations # config.action_view.raise_on_missing_translations = true end
/config/environments/production.rb
Overriding /config/application.rb settings when run in production mode.
Rails.application.configure do # Code is not reloaded between requests. config.cache_classes = true # Load most of Rails and app object in memory for performance config.eager_load = true # Full error reports are disabled and caching is turned on. config.consider_all_requests_local = false config.action_controller.perform_caching = true # Disable Rails’s static asset server (Apache or nginx will already do this). config.serve_static_assets = false # Compress JavaScripts and CSS. config.assets.js_compressor = :uglifier # Do not fallback to assets pipeline if a precompiled asset is missed. config.assets.compile = false # Generate digests for assets URLs. config.assets.digest = true # Log level not debug to keep file size down. config.log_level = :info # Send deprecation notices to registered listeners. config.active_support.deprecation = :notify # Use default logging formatter so that PID and timestamp are not suppressed. config.log_formatter = ::Logger::Formatter.new # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false end
/config/environments/test.rb
Overriding /config/application.rb settings when running test suites.
Rails.application.configure do # Code is not reloaded between requests. config.cache_classes = true # Do not eager load code on boot. config.eager_load = false # Configure static asset server for tests with Cache-Control for performance. config.serve_static_assets = true config.static_cache_control = ‘public, max-age=3600' # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false # Raise exceptions instead of rendering exception templates. config.action_dispatch.show_exceptions = false # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false # Tell Action Mailer not to deliver emails to the real world. config.action_mailer.delivery_method = :test # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr end
/config/initializers/assets.rb
# Version of your assets, change this if you want to expire all your assets. Rails.application.config.assets.version = ‘1.0'
/config/initializers/cookies_serializer.rb
# Serialise cookies in JSON. Rails.application.config.action_dispatch.cookies_serializer = :json
/config/initializers/filter_parameter_logging.rb
# Configure sensitive parameters which will be filtered from the log file. Rails.application.config.filter_parameters += [:password]
/config/initializers/session_store.rb
# Cookie and session name based of app name. Rails.application.config.session_store :cookie_store, key: ‘_hello-world_session’
/config/initializers/wrap_parameters.rb
Wraps the parameters hash into a nested hash allowing POST requests without having to specify any root elements.
# Enable parameter wrapping for JSON. ActiveSupport.on_load(:action_controller) do wrap_parameters format: [:json] if respond_to?(:wrap_parameters) end
YAML Files
/config/database.yml
Define which database, locations and basic settings.
# Default settings default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3 test: <<: *default database: db/test.sqlite3 production: <<: *default database: db/production.sqlite3
/config/secrets.yml
Security-related configs such as signed cookie validation. Never push this file to a public Git repository!
development: secret_key_base: randomlygeneratedkeyatleast30characterslong test: secret_key_base: randomlygeneratedkeyatleast30characterslong # Read production key from environment (can’t start app if doesn’t exist) production: secret_key_base: <%= ENV[“SECRET_KEY_BASE”] %>
/config/locales/en.yml
Internationalisation (i18n) properties file.
en: hello: “Hello world”
Other Files
Ignoring .gitignore, Gemfile.lock and README.rdoc.
/config.ru
Rackup file for running the Rack-compliant (adheres to a spec) web server.
require ::File.expand_path(‘../config/environment’, __FILE__) run Rails.application
/Gemfile
Dependencies for the project. Managed by Bundler.
source ‘https://rubygems.org' # Rails gem ‘rails’ # Use sqlite3 as the database for Active Record gem ‘sqlite3' # Use SCSS for stylesheets gem ‘sass-rails’, ‘~> 4.0.3' # Use Uglifier as compressor for JavaScript assets gem ‘uglifier’, ‘>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem ‘coffee-rails’, ‘~> 4.0.0' # Use jquery as the JavaScript library gem ‘jquery-rails’ # Turbolinks makes following links in your web application faster. gem ‘turbolinks’ # Build JSON APIs with ease. gem ‘jbuilder’, ‘~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem ‘sdoc’, ‘~> 0.4.0', group: :doc # Speed up development by keeping your application running in the background. gem ‘spring’, group: :development
/Rakefile
Load web application when rake is called.
require File.expand_path(‘../config/application’, __FILE__) Rails.application.load_tasks
/bin/bundle
Bundler gem management executable.
ENV[‘BUNDLE_GEMFILE’] ||= File.expand_path(‘../../Gemfile’, __FILE__) load Gem.bin_path(‘bundler’, ‘bundle’)
/bin/rails
Main Rails executable.
begin load File.expand_path(“../spring”, __FILE__) rescue LoadError end APP_PATH = File.expand_path(‘../../config/application’, __FILE__) require_relative ‘../config/boot’ require ‘rails/commands’
/bin/rake
Build tool executable (Ruby equivalent to Make).
begin load File.expand_path(“../spring”, __FILE__) rescue LoadError end require_relative ‘../config/boot’ require ‘rake’ Rake.application.run
/bin/spring
For development, keeps a copy of the running app in the background. Injects code into rails and rake executables.
unless defined?(Spring) require “rubygems” require “bundler” if match = Bundler.default_lockfile.read.match(/^GEM$.*?^ spring \((.*?)\)$.*?^$/m) ENV[“GEM_PATH”] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR) ENV[“GEM_HOME”] = “” Gem.paths = ENV gem “spring”, match[1] require “spring/binstub” end end
References
- guides.rubyonrails.org/v4.1.6/initialization.html (Official docs)
- southdesign.de/blog/rack.html (explanation on Rack)
Reblogged this on SutoCom Solutions.