z, ? | toggle help (this) |
space, → | next slide |
shift-space, ← | previous slide |
d | toggle debug mode |
## <ret> | go to slide # |
c, t | table of contents (vi) |
f | toggle footer |
r | reload slides |
n | toggle notes |
p | run preshow |
module ProductsHelper
def formatted_price(product)
number_with_precision
(product.price_without_taxes/100.0),
:precision => 2
end
end
class Product
def formatted_price_without_taxes
Object.new.extend(
ActionView::Helpers::NumberHelper)
.number_with_precision(
(price_without_taxes / 100.0),
:precision => 2)
)
end
end
draper
" au Gemfilebundle
rails generate decorator nomdumodele
class ArticleDecorator < ApplicationDecorator
decorates :article
allows :description
def title
article.title.upcase
end
end
article = ArticleDecorator.find(1)
article.title #=> "MON ARTICLE"
article.description #=> "Ma description"
article.created_at #=> NoMethodError
app/decorators/model_decorator.rb
class ArticleDecorator < ApplicationDecorator
def author_name
article.author.first_name +
" " +
article.author.last_name
end
def excerpt
h.truncate article.body
end
end
ArticleDecorator.new(Article.find(params[:id]))
ArticleDecorator.find(params[:id])
ArticleDecorator.decorate(Article.all)
allow/denies
: accès direct aux méthodes du modèledecorates_association
pour chaîner les decorateursclass ApplicationDecorator < Draper::Base
def created_at
h.content_tag :span,
I18n.l(model.created_at),
:class => 'created_at'
end
end
decorates_association
class Article < ActiveRecord::Base
belongs_to :author
end
class ArticleDecorator < ApplicationDecorator
decorates :article
decorates_association :author
end
class AuthorDecorator < ApplicationDecorator
decorates :author
def name
"#{model.first_name} #{model.last_name}"
end
end
@article.author.name
state_machine
current_user
)to_json
, to_xml
, to_csv
...options_for_select
# app/decorators/author_decorator.rb
class AuthorDecorator < ApplicationDecorator
decorates :author
def self.choices_for_select
Author.
order('last_name DESC').
all.
collect {|p| [ p.last_name, p.id ] }
end
end
# app/views/articles/_form.html.erb
<%= form.select
:author_id,
AuthorDecorator.choices_for_select %>
# spec/decorators/author_decorator_spec.rb
require 'spec_helper'
describe AuthorDecorator do
context "given a new Author" do
subject {
AuthorDecorator.decorate(
FactoryGirl.build(:author,
:first_name => "Dupond"))
}
it "displays its first_name" do
subject.first_name.should == "Dupond"
end
end
end