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

rails decorators

mmr
July 19, 2014

rails decorators

Implementing decorator pattern using draper

mmr

July 19, 2014
Tweet

More Decks by mmr

Other Decks in Programming

Transcript

  1. <h1> <%= @product.title %> </h1> ! <label> Boja: </label> <%

    unless @product.color.empty? %> <span> <%= @product.color %> </span> <% else %> <span> Nema opcije </span> <% end %> ! <label> Size: </label> <% unless @product.size.empty? %> <span> <%= @product.size %> </span> <% else %> <span> Nema opcije </span> <% end %>
  2. <h1> <%= @product.title %> </h1> ! <label> Boja: </label> <%

    unless @product.color.empty? %> <span> <%= @product.color %> </span> <% else %> <span> Nema opcije </span> <% end %> ! <label> Size: </label> <% unless @product.size.empty? %> <span> <%= @product.size %> </span> <% else %> <span> Nema opcije </span> <% end %>
  3. <h1> <%= @product.title %> </h1> ! <label> Boja: </label> <%

    unless @product.color.empty? %> <span> <%= @product.color %> </span> <% else %> <span> Nema opcije </span> <% end %> ! <label> Size: </label> <% unless @product.size.empty? %> <span> <%= @product.size %> </span> <% else %> <span> Nema opcije </span> <% end %>
  4. <p> <%= @product.retail_price %> </p> ! <p> <%= @product.price %>

    </p> ! <% if @product.retail_price %> <label> Usredite: </label> <p> <%= @product_retail_price - @product.price%> </p> <% end %> ! <% if @product.product_images.count > 0 %> <%= image_tag @product.product_images.first.image_url(:cover) %> <% else %> <%= image_tag ‘fallback/default_cover.png’%> </end>
  5. <p> <%= @product.retail_price %> </p> ! <p> <%= @product.price %>

    </p> ! <% if @product.retail_price %> <label> Usredite: </label> <p> <%= @product_retail_price - @product.price%> </p> <% end %> ! <% if @product.product_images.count > 0 %> <%= image_tag @product.product_images.first.image_url(:cover) %> <% else %> <%= image_tag ‘fallback/default_cover.png’%> </end>
  6. <p> <%= @product.retail_price %> </p> ! <p> <%= @product.price %>

    </p> ! <% if @product.retail_price %> <label> Ustedite: </label> <p> <%= @product_retail_price - @product.price%> </p> <% end %> ! <% if @product.product_images.count > 0 %> <%= image_tag @product.product_images.first.image_url(:cover) %> <% else %> <%= image_tag ‘fallback/default_cover.png’%> </end>
  7. <p> <%= @product.retail_price %> </p> ! <p> <%= @product.price %>

    </p> ! <% if @product.retail_price %> <label> Ustedite: </label> <p> <%= @product_retail_price - @product.price%> </p> <% end %> ! <% if @product.product_images.count > 0 %> <%= image_tag @product.product_images.first.image_url(:cover) %> <% else %> <%= image_tag ‘fallback/default_cover.png’%> </end>
  8. <h1> <%= @product.title %> </h1> ! <label> Boja: </label> <%

    unless @product.color.empty? %> <span> <%= @product.color %> </span> <% else %> <span> Nema opcije </span> <% end %> ! <label> Size: </label> <% unless @product.size.empty? %> <span> <%= @product.size %> </span> <% else %> <span> Nema opcije </span> <% end %> ! <p> <%= @product.retail_price %> </p> ! <p> <%= @product.price %> </p> ! <% if @product.retail_price %> <label> Usredite: </label> <p> <%= @product_retail_price - @product.price%> </p> <% end %> ! <% if @product.product_images.count > 0 %> <%= image_tag @product.product_images.first.image_url(:cover) %> <% else %> <%= image_tag ‘fallback/default_cover.png’%> </end>
  9. #app/helpers/product_helper.rb ! module ProductHelper def cover_image(product) first_image = product.product_images.try(:first) !

    if first_image h.content_tag :img, first_image.url(:cover) else h.content_tag :img, ‘fallback/default_cover.png’ end end end ! #app/views/products/show.html.erb ! <%= cover_image(@product) %>
  10. # app/decorators/product_decorator.rb ! class ProductDecorator < Draper::Decorator def size unless

    object.size.empty? h.content_tag :span, object.size else h.content_tag :span, ‘Nema opcije za velicinu’ end end end Implemetacija
  11. # app/decorators/product_decorator.rb ! class ProductDecorator < Draper::Decorator … def color

    unless object.color.empty? h.content_tag :span, object.color else h.content_tag :span, ‘Nema opcije za boju’ end end end
  12. # app/decorators/product_decorator.rb ! class ProductDecorator < Draper::Decorator … def cover_image

    first_image = object.images.try(:first) if first_image h.image_tag first_image.image_url(:cover) else h.image_tag ‘fallback/cover_image.png’ end end end
  13. # app/decorators/product_decorator.rb ! class ProductDecorator < Draper::Decorator … ! def

    discount if object.retail_price h.content_tag :span, object.retail_price - object.price end end end
  14. <h1> <%= @product.title %> </h1> ! <label> Boja: </label> <%

    unless @product.color.empty? %> <span> <%= @product.color %> </span> <% else %> <span> Nema opcije </span> <% end %> ! <label> Size: </label> <% unless @product.size.empty? %> <span> <%= @product.size %> </span> <% else %> <span> Nema opcije </span> <% end %> ! <p> <%= @product.retail_price %> </p> ! <p> <%= @product.price %> </p> ! <% if @product.retail_price %> <label> Usredite: </label> <p> <%= @product_retail_price - @product.price%> </p> <% end %> ! <% if @product.product_images.count > 0 %> <%= image_tag @product.product_images.first.image_url(:cover) %> <% else %> <%= image_tag ‘fallback/default_cover.png’%> </end>
  15. <h1> <%= @product.title %> </h1> ! <label> Boja: </label> <%=

    product.color %> ! <label> Size: </label> <%= product.size %> ! <p> <%= @product.retail_price %> </p> <p> <%= @product.price %> </p> ! <%= product.discount %> <%= product.cover_image %>