9. MVC
Browser Controller Action
Model-View-Controller
HTTP request
route.rb
GET /users/1
UsersController
Model
def show
@user = User.find(params[:id])
Database
respond_to do |format|
format.html
format.xml
end
View
#show.html.erb
end
<html>
def index
<h1>User Profile</h1>
......
<p><%= @user.nickname %></p>
end
</html>
end
19. Callbacks
def create
@post = Post.new(params[:post])
@post.content += quot; < #{Time.now.to_s} >quot;
if @post.save
flash[:notice] = 'Post was successfully created.'
redirect_to blog_path(@post)
else
render :action => quot;newquot;
end
end
20. Callbacks
before_create
class Post < ActiveRecord::Base
before_create :insert_timestamp
def insert_timestamp
self.content += quot; < #{Time.now.to_s} >quot;
end
end
23. Counter Cache
class Comment < ActiveRecord::Base
belongs_to :post
def after_save
self.update_counter_cache
end
def after_destroy
self.update_counter_cache
end
def update_conter_cache
self.post.comments_count = self.post.comments.size
slf.post.save(false)
end
end
24. Counter Cache
class Comment < ActiveRecord::Base
belongs_to :post, :counter_cache => true
end
class PostCommentsCount < ActiveRecord::Migration
def self.up
add_column :posts, :comments_count, :integer , :default => 0
end
def self.down
remove_column :posts,:comments_count
end
end
25. Named Scope
v.2.1
class Post < ActiveRecord::Base
named_scope :publish, :conditions => { :is_blocked => false }
named_scope :recent, :order => quot;created_at DESCquot;, :limit => 1
end
Post.publish
=> SELECT * FROM `posts` WHERE (`posts`.`is_blocked` = 0)
Post.recent
=> SELECT * FROM `posts` ORDER BY created_at DESC LIMIT 1
Post.publish.recent
=> SELECT * FROM `posts` WHERE (`posts`.`is_blocked` = 0)
ORDER BY created_at DESC LIMIT 1
26. STI (Single Table Inheritance )
class Post < ActiveRecord::Base
has_many :comments
before_create :insert_timestamp
def insert_timestamp
self.content += quot; < #{Time.now.to_s} >quot;
end
named_scope :publish, :conditions => { :is_blocked => false }
named_scope :recent, :order => quot;created_at DESCquot;, :limit => 1
end
class Article < Post
end
27. Polymorphic Association
• Post has_many comments
• Photo has_many comments Table ?
• News has_many comments
post_comment
photo_comment
news_comment
28. Polymorphic Association
class Post < ActiveRecord::Base
has_many :comments, :as => :resource
end
class Comment < ActiveRecord::Base
belongs_to :resource, :polymorphic => true
end
class PolymorphicComment < ActiveRecord::Migration
def self.up
remove_column :comments, :post_id
add_column :comments, :resource_id, :integer
add_column :comments, :resource_type, :string
end
def self.down
add_column :comments, :post_id, :integer
remove_column :comments, :resource_id
remove_column :comments, :resource_type
end
end
57. acts_as_taggable_on_steroids
• tagging
class Post < ActiveRecord::Base
acts_as_taggable
end
p = Post.find(:first)
p.tag_list # []
p.tag_list = quot;Funny, Sillyquot;
p.save
p.tag_list # [quot;Funnyquot;, quot;Sillyquot;]
p.tag_list.add(quot;Greatquot;, quot;Awfulquot;)
p.tag_list.remove(quot;Funnyquot;)
58.
59. validates_url_of
class Foo < ActiveRecord::Base
validates_url_of :url, :message => 'is not valid or not responding'.t
end
• http:// http:// https://
•
•
65. facebox_render (cont.)
• facebox
class ApplicationController < ActionController::Base
include FaceboxRender
end
Text
66. facebox_render (cont.)
• facebox
class ApplicationController < ActionController::Base
include FaceboxRender
end
Text
<%= facebox_link_to quot;Loginquot;, :url => new_session_url %>
67. facebox_render (cont.)
• facebox
class ApplicationController < ActionController::Base
include FaceboxRender
end
Text
<%= facebox_link_to quot;Loginquot;, :url => new_session_url %>
68. facebox_render (cont.)
• facebox
class ApplicationController < ActionController::Base
include FaceboxRender
end
Text
<%= facebox_link_to quot;Loginquot;, :url => new_session_url %>
def new
# do some thing you want
respond_to do |format|
format.html
format.js { render_facebox }
end
end
69. facebox_render (cont.)
• facebox
class ApplicationController < ActionController::Base
include FaceboxRender
end
Text
<%= facebox_link_to quot;Loginquot;, :url => new_session_url %>
def new
# do some thing you want
respond_to do |format|
format.html
format.js { render_facebox }
end
end
73. def batch
...
respond_to do |format|
format.html
format.js { render_to_facebox }
end
end
Text
74. def batch
...
respond_to do |format|
format.html
format.js { render_to_facebox }
end
end
Text
75. def batch
...
respond_to do |format|
format.html
format.js { render_to_facebox }
end
end
batch.html.erb
Text
76. def batch
...
respond_to do |format|
format.html
format.js { render_to_facebox }
end
end
batch.html.erb
Text
77. stickies
• flash[:notice]
• CSS
style Javascript close
error_stickie(quot;Your account has been disabledquot;)
warning_stickie(quot;Your account will expire in 3 daysquot;)
notice_stickie(quot;Account activatedquot;)
debug_stickie(quot;This only works when RAILS_ENV is developmentquot;)
<%= render_stickies %>
81. New Relic
• profiling your rails app
• trace code stack
• provide optimize direction
82. Capistrano
deploy
1. ssh to production server
2. svn checkout
3. run some your script (link file/copy config file/ clear cache…etc)
4. restart mongrel cluster