PadrinoDecorator

2c246b10eca87568dda9f431c1134cb3?s=47 Takeshi Yabe
September 18, 2013
30

 PadrinoDecorator

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

2c246b10eca87568dda9f431c1134cb3?s=128

Takeshi Yabe

September 18, 2013
Tweet

Transcript

  1. σίϨʔλͷ ͸ͳ͠ ौ୩SC<> 5BLFTIJ:BCF!UZBCF

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

    image_tag ‘default.png’ ͜Μͳͷ͕ͲΜͲΜͲΜͲΜ૿͑ͯ͘Δ WJFX
  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 NPEFM
  5. 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
  6. )FMQFS app/helpers/posts_helper.rb StitchStitch::App.helpers do # ... end ɾϝιου໊ͷিಥΛؾʹ͢Δඞཁ͕͋Δ ɾ001ͬΆ͘ͳ͍

  7. 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ʹҠ͢
  8. 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 ςετ΋ॻ͖΍͘͢ͳΔ
  9. ؾܰʹ࢖͍͍ͨͷͰ (FOFSBUPSΛ࡞ͬͨ

  10. None
  11. $ 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 ͔͍͔ͭͨ
  12. $ tree . ᵓᴷᴷ app ᴹ ᵓᴷᴷ app.rb ᴹ ᵓᴷᴷ

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

  14. None
  15. None
  16. %SBQFS΋࣮ݱ͢Δ͜ͱ͸جຊతʹ͸ಉ͡Ͱ͢ ͕ɺํࣜʹҧ͍͕͋Γ·͢ɻ"DUJWF%FDPSBUPS͸ ϞσϧΦϒδΣΫτʹϞδϡʔϧΛFYUFOE͢Δ ͷʹର͠ɺ%SBQFS͸ϞσϧΦϒδΣΫτΛϥοϓ ͢ΔΦϒδΣΫτΛ࡞ͬͯϝιουΛҠৡ͠· ͢ɻ %SBQFSͷํࣜͰ͸ϝιουΛ௥Ճ͢Δલͱͨ͠ ޙͰΦϒδΣΫτͷJEFOUJUZ͕มԽͯ͠͠·͏ͨ ΊɺΑ͘Α͘஫ҙ͠ͳ͍ͱϋϚΓ·͢ɻ Ҿ༻ɿ3VCZJTU.BHB[JOF3VCZPO3BJMT5IF#BE1BSUT

    IUUQNBHB[JOFSVCZJTUOFU 3BJMT5IF#BE1BSUT
  17. None
  18. None
  19. None
  20. None
  21. -FUTUBMLBCPVUJU