Your SlideShare is downloading. ×
Padrino decorator
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Padrino decorator

400

Published on

渋谷.rb[:20130918] での発表資料です

渋谷.rb[:20130918] での発表資料です

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
400
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. デコレータの はなし 渋谷.rb[:20130918] Takeshi Yabe / @tyabe
  • 2. app/views/posts/show.slim - if @post.image.present? = image_tag @post.image.url - else = image_tag ‘default.png’ こんなのがどんどんどんどん増えてくる view
  • 3. models/post.rb class Post < ActiveRecord::Base def image_url if self.image.present? self.image.url else ‘default.png’ end end end app/views/posts/show.slim = image_tag @post.image_url model?
  • 4. models/post.rb class Post < ActiveRecord::Base def image_url if self.image.present? self.image.url else ‘default.png’ end end end app/views/posts/show.slim = image_tag @post.image_url View用のメソッドが Modelに入っている キモい model?
  • 5. Helper? app/helpers/posts_helper.rb StitchStitch::App.helpers do # ... end ・メソッド名の衝突を気にする必要がある ・OOPっぽくない
  • 6. models/post.rb class Post < ActiveRecord::Base end app/controllers/posts_controller.rb SampleProject::App.controllers :posts do get :show, with: :id do post = Post.find(params[:id]) @post_decorator = PostDecorator.new(post) render ‘posts/show’ end end app/views/posts/show.slim - if @post_decorator.is_new_page? = image_tag @post_decorator.image app/decorators/posts_decorator.rb class PostDecorator attr_reader :post def initialize(post) @post = post end def image_url if self.image.present? self.image.url else ‘default.png’ end end end 表示用のロジックを Decorator に移す
  • 7. spec/app/decorators/post_decorator_spec.rb require 'spec_helper' describe "PostDecorator" do it 'can construct a new instance' do post = create(:post) @post_decorator = PostDecorator.new(post, self) expect(@post_decorator.image_url).to eq ‘default.png’ end end テストも書きやすくなる
  • 8. 気軽に使いたいので Generatorを作った
  • 9. $ padrino g decorator Usage: padrino-gen decorator [name] Options: -r, [--root=ROOT] # The root destination # Default: . -a, [--app=APP] # The application destination path # Default: /app -d, [--destroy] -n, [--namespace=NAMESPACE] # The name space of your padrino project Description: padrino-gen decorator generates a new Padrino decorator つかいかた
  • 10. $ tree . ├── app │ ├── app.rb │ ├── controllers │ ├── decorators │ │   └── post_decorator.rb │ ├── helpers │ └── views └── spec └── app └── decorators └── post_decorator_spec.rb Decorator用のファイルをつくります
  • 11. Rails
  • 12. Draper も実現することは基本的には同じです が、方式に違いがあります。ActiveDecorator は モデルオブジェクトにモジュールを extend する のに対し、Draper はモデルオブジェクトをラップ するオブジェクトを作ってメソッドを移譲しま す。 Draper の方式ではメソッドを追加する前とした 後でオブジェクトの identity が変化してしまうた め、よくよく注意しないとハマります。 引用:Rubyist Magazine - Ruby on Rails: The Bad Parts http://magazine.rubyist.net/?0041-RailsTheBadParts
  • 13. Let's talk about it.

×