Manish Shrivastava
Manish Shrivastava

Reputation: 32050

which code I should put into a partal in rails

Ideally the code which is reusable, that we can put into a septate partial and can call it from different actions.

Now, if my parial _list.html.haml file has code

 #betting_list
   %table.table
    %tr  
    %th{:align => "left"}="Start time"
    %th{:align => "left"}="Fixture"
    %th{:align => "left"}="Won"
    %th{:align => "left"}="Draw"
    %th{:align => "left"}="Lost"
    %th{:align => "left"}="Amount"
  - @upcoming_event.each do |event|
    %tr{:class => cycle('even', 'odd')}
      %td=event.start_at.strftime("%b %d %H:%M")
      %td=link_to event.name, event
      - event.outcomes.each do |outcome|
        - if outcome.label =="Won"
          %td=outcome.odds 
        - if outcome.label =="Draw"
          %td=outcome.odds 
        - if outcome.label =="Lost"
          %td=outcome.odds 
      %td=event.bets.count
= paginate(@upcoming_event)

Should we always keep a a data row or loop in different partial or ?. I am in delimma. I don't need to re-use that loop part to reuse.

Is is possible to refactor such code ?

Upvotes: 0

Views: 58

Answers (2)

juicedM3
juicedM3

Reputation: 499

As for different partials, it all depends on reuse or readability. A properly named partial could also help explain what's going on for maintenance reasons especially if there are no comments.

Case statement makes it a little more readable. But that's just an opinion.

case outcome.label
when "Won", "Lost", "Draw"
  %td=outcome.odds
end

Upvotes: 1

Abibullah Rahamathulah
Abibullah Rahamathulah

Reputation: 2891

You can keep the looping better in a new partial.

Also I see the multiple IF's, a bit confusing.

You can do this

- if (["Won","Lost","Draw"].include? outcome.label)
    %td=outcome.odds 

Upvotes: 1

Related Questions