+ All Categories
Home > Technology > Resource and view

Resource and view

Date post: 13-May-2015
Category:
Upload: papp-laszlo
View: 1,226 times
Download: 0 times
Share this document with a friend
Popular Tags:
24
Fitting for the occasion October 14, 2010 [email protected] MeetUP @ Balabit
Transcript
Page 1: Resource and view

Fitting for the occasion

October 14, [email protected]

MeetUP @ Balabit

Page 2: 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

Page 3: 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

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()

Page 4: Resource and view

Balabit Meetup - October 14, 2010 [email protected]

Sharing information

ProductModel

ProductsController

Page 5: Resource and view

Balabit Meetup - October 14, 2010 [email protected]

Sharing information

ProductModel

ProductsController

Page 6: Resource and view

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

Page 7: Resource and view

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

Page 8: Resource and view

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

Page 9: Resource and view

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

Page 10: Resource and view

Balabit Meetup - October 14, 2010 [email protected]

Fitting for the occasion

ProductModel

ProductController

Page 11: Resource and view

Balabit Meetup - October 14, 2010 [email protected]

Fitting for the occasion

ProductModel

ProductController

HTTP

iPhone UI / XML

Android UI / XML

XML

JSON

Page 12: Resource and view

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

Page 13: Resource and view

Balabit Meetup - October 14, 2010 [email protected]

Fitting for the occasion

ProductModel

ProductController

/products.html

/products.iphone

/products.xml

/products.rss

/products.js

Page 14: Resource and view

Balabit Meetup - October 14, 2010 [email protected]

Review

/app/models /app/controllers

/app/views /app/helpers

Page 15: Resource and view

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

Page 16: Resource and view

Balabit Meetup - October 14, 2010 [email protected]

Views

:menu

content

:header

Layout

:footer

product #1

product #2

product #3

index.html.erb

Page 17: Resource and view

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>

Page 18: Resource and view

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

Page 19: Resource and view

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>

Page 20: Resource and view

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

Page 21: Resource and view

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

Page 22: Resource and view

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')

Page 23: Resource and view

Balabit Meetup - October 14, 2010 [email protected]

Questions?

Page 24: Resource and view

Balabit Meetup - October 14, 2010 [email protected]

Thank you!


Recommended