Resource and view
-
Upload
papp-laszlo -
Category
Technology
-
view
1.230 -
download
0
Transcript of Resource and view
Balabit Meetup - October 14, 2010 [email protected]
Rails 3.0
• Merb + Rails 2.3 = Rails 3.0
• Bundler
• Rack
• HTML 5
• Arel
Balabit Meetup - October 14, 2010 [email protected]
Rails 3.0
• Merb + Rails 2.3 = Rails 3.0
• Bundler
• Rack
• HTML 5
• Arel
users \ .where(users[:name].eq('nucc')) \ .project(users[:id]) \# => SELECT users.id FROM users WHERE users.name = 'nucc'
users.where(users[:name].eq('bob')).where(users[:age].lt(25))# => SELECT * FROM users where users.name=’bob’ and users.age < 25
users.where(users[:name].eq('bob').or(users[:age].lt(25)))# => SELECT * FROM users where users.name=’bob’ or users.age < 25
valid_users = users.where(users[:valid].eq(1))men = valid_users.where(users[:sex].eq(1))women = valid_users.where(users[:sex].eq(2))number_of_women = women.count()
Balabit Meetup - October 14, 2010 [email protected]
Sharing information
ProductModel
ProductsController
Balabit Meetup - October 14, 2010 [email protected]
Sharing information
ProductModel
ProductsController
Balabit Meetup - October 14, 2010 [email protected]
REST
• Representational State Transfer
• Roy Fielding - 2000
• SOAP is a protocol, REST is an architecture
• Representation of a resource
• iWiW, Facebook, GitHub, Twitter, Google Search
Balabit Meetup - October 14, 2010 [email protected]
REST Mapping
Verb Path Action
GET /products index
GET /products/:id show
POST /products create
GET /products/:id/edit edit
PUT /products/:id update
DELETE /products/:id delete
Balabit Meetup - October 14, 2010 [email protected]
Configuration
class Product < ActiveRecord::Baseend
class Products < ApplicationController def index @products = Product.all end
def show @product = Product.find params[:id] end
def update ... end ...end
Meetup::Application.routes.draw do |map| resources :productsend
/app/models/product.rb /app/controllers/products_controller.rb
config/routes.rb
Balabit Meetup - October 14, 2010 [email protected]
Using resource
class User < ActiveResource::Base site.url = “http://api.twitter.com”end
/app/models/user.rb
class UserController < ActionController::Base self.site = “http://api.twitter.com”
self.element_name = “user” self.proxy = “...” self.timeout = 5 self.ssl_options = { :key => “...” }end
/app/controller/user_controller.rb
Balabit Meetup - October 14, 2010 [email protected]
Fitting for the occasion
ProductModel
ProductController
Balabit Meetup - October 14, 2010 [email protected]
Fitting for the occasion
ProductModel
ProductController
HTTP
iPhone UI / XML
Android UI / XML
XML
JSON
Balabit Meetup - October 14, 2010 [email protected]
Respond to
class ProductsController < Application def index @products = Product.all
respond_to do |format| format.html # index.html.erb format.iphone # index.iphone.erb format.json { render :json => @products } format.xml { render :xml => @products} end end end
Balabit Meetup - October 14, 2010 [email protected]
Fitting for the occasion
ProductModel
ProductController
/products.html
/products.iphone
/products.xml
/products.rss
/products.js
Balabit Meetup - October 14, 2010 [email protected]
Review
/app/models /app/controllers
/app/views /app/helpers
Balabit Meetup - October 14, 2010 [email protected]
Rendering
render :xml => @products
render :json => @products
render :file => “../owners/owner.html.erb”
render :text => “Hello World”
render :update do |page|
page.replace_html(“products”, :partial => product, :object=> @product)
end
Balabit Meetup - October 14, 2010 [email protected]
Views
:menu
content
:header
Layout
:footer
product #1
product #2
product #3
index.html.erb
Balabit Meetup - October 14, 2010 [email protected]
Layout# layout.html.erb<html> <body> <div class=”header”> <%= yield :header %> </div> <div class=”content”> <div class=”menu”> <%= yield :menu %> </div> </div>
<div class=”content”> <%= yield %> </div>
...
# index.html.erb
<%= content_for :header do %> <span> BALABIT Products </span><% end %>
<%= content_for :menu do %> <li>Special menu item</li><% end %>
<div class=”products”> ...</div>
Balabit Meetup - October 14, 2010 [email protected]
Partial
content
product #1
product #2
product #3
/app/views/products/index.html.erb
#objectrender :partial => “product”, :object => ...
#arrayrender :partial => “product”, :collection => ...
/app/views/products/_product.html.erb
Balabit Meetup - October 14, 2010 [email protected]
Partial
content
product #1
product #2
product #3
/app/views/products/index.html.erb
#objectrender :partial => “product”, :object => ...
#arrayrender :partial => “product”, :collection => ...
/app/views/products/_product.html.erb
# /app/views/products/index.html.erb<div class=”products”> <%= render :partial => “product”, :collection => @products %></div>
# /app/views/products/_product.html.erb<div class=”product”> <span class=”title”><%= product.name %></span> <span class=”owner”><%= product.owner %></span></div>
# result<div class=”products”> <div class=”product”> <span class=”title”>SCB</span> <span class=”owner”>Marci</span> </div></div>
Balabit Meetup - October 14, 2010 [email protected]
Helpers
<div> <span> Number of products: </span> <%= number_of_products(@product) %><div>
<% form_for @product, :remote => true do | f | %> <%= f.text_field :version %><% end %>
/app/views/products/index.html.erbmodule ProductsHelper
def number_of_products( products ) products.select{ |p| p.active? }.size end
end
/app/helpers/products_helper.rb
Balabit Meetup - October 14, 2010 [email protected]
Cacheclass ProductController < Application
caches_page :index
def index @products = Product.all end
def create expire_page :action => :index ... end ...end
class ProductController < Application before_filter :authentication caches_action :index
def index @products = Product.all end
def create expire_page :action => :index ... end ...end
Balabit Meetup - October 14, 2010 [email protected]
Cache<html> <body>
<% cache do %> <div class=”menu”> <li>Show products</li> </div><% end %>
<% cache(:action => 'update', :action_suffix => 'products') do %> <div class=”products”> <%= render :partial => “product”, :collection => @products %> </div><% end %>
# expire_fragment( :controller => ‘products’, :action => 'index', :action_suffix => 'products')
Balabit Meetup - October 14, 2010 [email protected]
Questions?
Balabit Meetup - October 14, 2010 [email protected]
Thank you!