Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PadrinoDecorator

Takeshi Yabe
September 18, 2013
52

 PadrinoDecorator

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

Takeshi Yabe

September 18, 2013
Tweet

Transcript

  1. app/views/posts/show.slim - if @post.image.present? = image_tag @post.image.url - else =

    image_tag ‘default.png’ ͜Μͳͷ͕ͲΜͲΜͲΜͲΜ૿͑ͯ͘Δ WJFX
  2. 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 NPEFM
  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 7JFX༻ͷϝιου͕ .PEFMʹೖ͍ͬͯΔ ΩϞ͍ NPEFM
  4. 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 දࣔ༻ͷϩδοΫΛ%FDPSBUPSʹҠ͢
  5. 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 ςετ΋ॻ͖΍͘͢ͳΔ
  6. $ 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 ͔͍͔ͭͨ
  7. $ tree . ᵓᴷᴷ app ᴹ ᵓᴷᴷ app.rb ᴹ ᵓᴷᴷ

    controllers ᴹ ᵓᴷᴷ decorators ᴹ ᴹ ᵋᴷᴷ post_decorator.rb ᴹ ᵓᴷᴷ helpers ᴹ ᵋᴷᴷ views ᵋᴷᴷ spec ᵋᴷᴷ app ᵋᴷᴷ decorators ᵋᴷᴷ post_decorator_spec.rb %FDPSBUPS༻ͷϑΝΠϧΛͭ͘Γ·͢