16. con g.ru
# This file is used by Rack-based
# servers to start the application.
require ::File.expand_path(
'../config/environment',
__FILE__
)
run Tutorial::Application
17. con g/boot.rb
require 'rubygems'
# Set up gems listed in the Gemfile.
gemfile = File.expand_path(
'../../Gemfile',
__FILE__
)
if File.exist?(gemfile)
ENV['BUNDLE_GEMFILE'] = gemfile
require 'bundler'
Bundler.setup
end
19. con g/environment.rb
# Load the rails application
require File.expand_path(
'../application',
__FILE__
)
# Initialize the rails application
Tutorial::Application.initialize!
20. con g/application.rb (1)
require File.expand_path(
'../boot',
__FILE__
)
require 'rails/all'
if defined?(Bundler)
Bundler.require(:default, Rails.env)
end
21. con g/application.rb (2)
module Tutorial
class Application < Rails::Application
config.encoding = "utf-8"
config.filter_parameters +=
[:password]
end
end
22. environments/production.rb
Tutorial::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_dispatch.x_sendfile_header = "X-Sendfile"
config.serve_static_assets = false
end
24. script/rails (1)
#!/usr/bin/env ruby
# This command will automatically
# be run when you run "rails" with
# Rails 3 gems installed from the
# root of your application.
ENV_PATH = File.expand_path(
'../../config/environment',
__FILE__
)
62. Path Scope
match "/admin/posts" => "posts#index"
match "/admin/users" => "users#index"
scope :path => "admin" do
match "/posts" => "posts#index"
match "/users" => "users#index"
end
63. Path Scope
match "/admin/posts" => "posts#index"
match "/admin/users" => "users#index"
scope "admin" do
match "/posts" => "posts#index"
match "/users" => "users#index"
end
65. Module Scope
match "/posts" => "admin/posts#index"
match "/users" => "admin/users#index"
scope :module => "admin" do
match "/posts" => "posts#index"
match "/users" => "users#index"
end
67. Both
match "admin/posts" => "admin/posts#index"
match "admin/users" => "admin/users#index"
namespace "admin" do
match "/posts" => "posts#index"
match "/users" => "users#index"
end
72. Scoping
scope "/posts" do
controller :posts do
get "/" => :index
end
end
get "/posts" => "posts#index"
73. Default Resource Route
controller :posts do
scope "/posts" do
get "/" => :index
post "/" => :create
get "/:id" => :show
put "/:id" => :update
delete "/:id" => :delete
get "/new" => :new
get "/:id/edit" => :edit
end
end
74. Default Resource Route
controller :posts do
scope "/posts" do
get "/" => :index, :as => :posts
post "/" => :create
get "/:id" => :show, :as => :post
put "/:id" => :update
delete "/:id" => :delete
get "/new" => :new, :as => :new_post
get "/:id/edit" => :edit, :as => :edit_post
end
end
107. Rack Endpoint
class MountedEndpoint
def call(env)
head = {"Content-Type" => "text/html"}
body = "script: #{env["SCRIPT_NAME"]}"
body += "path: #{env["PATH_INFO"]}"
[200, head, [body]]
end
end
108. Mounting
class MountedEndpoint
def call(env)
head = {"Content-Type" => "text/html"}
body = "script: #{env["SCRIPT_NAME"]}"
body += "path: #{env["PATH_INFO"]}"
[200, head, [body]]
end
end
mount "/end", :at => MountedEndpoint.new
109. Mounting
class MountedEndpoint
def call(env)
head = {"Content-Type" => "text/html"}
body = "script: #{env["SCRIPT_NAME"]}"
body += "path: #{env["PATH_INFO"]}"
[200, head, [body]]
end
end
mount "/end", :at => MountedEndpoint.new
# "/end/point" =>
# script: /end
# path: /point
113. Chainable Methods
★ select ★ order
★ from ★ limit
★ where ★ offset
★ joins ★ includes
★ having ★ lock
★ group ★ readonly
114. Controller
def index
@posts = Post.
where(:published => true).
order("publish_date desc")
end
115. Model
def index
@posts = Post.published
end
class Post < ActiveRecord::Base
scope :published,
where(:published => true).
order("publish_date desc")
end
116. Model
class Post < ActiveRecord::Base
scope :desc,
order("publish_date desc")
scope :published,
where(:published => true).desc
end
117. Controller
def index
@posts = Post.
where("created_at < ?", Time.now).
order("publish_date desc")
end
118. Controller
def index
@posts = Post.past
end
class Post < ActiveRecord::Base
scope :desc,
order("publish_date desc")
def self.past
where("created_at < ?",
Time.now).desc
end
end
119. Model
class Post < ActiveRecord::Base
scope :desc,
order("publish_date desc")
def self.past
where("created_at < ?",
Time.now).desc
end
def self.recent(number)
past.limit(5)
end
end
120. Pagination
class PostsController < ApplicationController
def index
@posts = Posts.page(5, :per_page => 10)
end
end
class Post < ActiveRecord::Base
def self.page(number, options)
per_page = options[:per_page]
offset(per_page * (number - 1)).
limit(per_page)
end
end
129. Be More Speci c
def welcome(user)
@user = user
mail(:to => user.email,
:subject => "Welcome man!") do |format|
format.html
format.text { render "generic" }
end
end
130. Defaults
default :from => "wycats@gmail.com"
def welcome(user)
@user = user
mail(:to => user.email,
:subject => "Welcome man!") do |format|
format.html
format.text { render "generic" }
end
end
132. Interceptors
class MyInterceptor
def self.delivering_email(mail)
original = mail.to
mail.to = "wycats@gmail.com"
mail.subject =
"#{original}: #{mail.subject}"
end
end
133. Interceptors
class MyInterceptor
def self.delivering_email(mail)
original = mail.to
mail.to = "wycats@gmail.com"
mail.subject =
"#{original}: #{mail.subject}"
end
end
config.action_mailer.
register_interceptor(MyInterceptor)