E635ccff7389d9070f5e7e9fe8b36beb

I'm building a golf application to track my golf game. Tonight I finished up the functionality to add a course. I had no idea how complex a golf scorecard was until I tried to model it in a DB.

This is all working, I'm just wondering if there's a better way. It feels clean to me, but a little too separated? And I know I should do some sort of loop in the tee's partial, since it's duplicating each row and only the tee color is different. Anyway, any advice is greatly appreciated... thanks in advance.

BTW: I don't expect any _real_ refactorings, just because it's sort of a lot. But just a brief explanation in words might go a long way. Thanks.

courses_controller.rb

1
2
3
4
5
6
def new
  @course  = Course.new
  18.times { @course.holes.build }
  3.times  { @course.tees.build  }
end

courses/new.rhtml (only the scorecard part)

1
2
3
4
5
6
7
<table>
  <%= render :partial => 'yardages',           :locals => { :side => 'front' } -%>
  <%= render :partial => 'yardages',           :locals => { :side => 'back'  } -%>
  <%= render :partial => 'handicaps_and_pars', :locals => { :side => 'front' } -%>
  <%= render :partial => 'handicaps_and_pars', :locals => { :side => 'back'  } -%>
</table>

courses/_yardages.rhtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<tr>
  <td>Holes</td>
  <%= provide_hole_count_for(side) -%>
</tr>
<tr>
  <td>Blue Tee's</td>
  <%= render :partial => 'yards', :collection => @course.holes, :locals => { :color => 'blue', :which => side } -%>
</tr>
<tr>
  <td>White Tee's</td>
  <%= render :partial => 'yards', :collection => @course.holes, :locals => { :color => 'white', :which => side } -%>
</tr>
<tr>
  <td>Red Tee's</td>
  <%= render :partial => 'yards', :collection => @course.holes, :locals => { :color => 'red', :which => side } -%>
</tr>

courses/_handicaps_and_pars.rhtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<tbody id="holes-<%= side %>">	
  <tr>
    <td>Holes</td>
    <%= provide_hole_count_for(side) -%>
  </tr>
  <tr>
    <td>Men's Handicap</td>
    <%= render :partial => 'handicap', :collection => @course.holes, :locals => { :mens => true, :side => side } -%>
  </tr>
  <tr>
    <td>Men's Par</td>
    <%= render :partial => 'hole', :collection => @course.holes, :locals => { :mens => true, :side => side } -%>
  </tr>
  <tr>
    <td>Ladie's Handicap</td>
    <%= render :partial => 'handicap', :collection => @course.holes, :locals => { :mens => false, :side => side } -%>
  </tr>
  <tr>
    <td>Ladie's Par</td>
    <%= render :partial => 'hole', :collection => @course.holes, :locals => { :mens => false, :side => side } -%>
  </tr>
</tbody>

courses/_yards.rhtml

1
2
3
4
5
6
7
8
<% fields_for "course[course_holes][]", yards do |f| %>
  <% setup_fields_for(which, yards_counter) do -%>	
    <td>
      <%= f.text_field "#{color}_yardage".to_sym, :index => nil, :style => "width: 90%;" %>
    </td>
  <% end -%>
<% end %>

courses/_handicap.rhtml

1
2
3
4
5
6
7
8
9
10
11
12
<% fields_for "course[course_holes][]", handicap do |f| %>
  <% setup_fields_for(side, handicap_counter) do -%>
  <td>
    <% if mens -%>
      <%= f.text_field :mens_handicap, :index => nil, :style => "width: 90%;" %>
    <% else %>
      <%= f.text_field :ladies_handicap, :index => nil, :style => "width: 90%;" %>
    <% end -%>
  </td>
  <% end -%>
<% end %>

courses/_hole.rhtml

1
2
3
4
5
6
7
8
9
10
11
12
<% fields_for "course[course_holes][]", hole do |f| %>
  <% setup_fields_for(side, hole_counter) do -%>
  <td>
    <% if mens -%>
      <%= f.text_field :mens_par, :index => nil, :style => "width: 90%;" %>
    <% else %>
      <%= f.text_field :ladies_par, :index => nil, :style => "width: 90%;" %>
    <% end -%>
  </td>
  <% end -%>
<% end %>

courses_helper.rb

1
2
3
4
5
6
7
8
9
def setup_fields_for(side, iteration)
  yield if (9 / (iteration + 1)).to_f (side.to_sym == :front ? >= : <) 1.0  
end
	
def provide_hole_count_for(side)
  returning td_collection = String.new do
    9.times { |h| td_collection << "<td>#{h + (side.to_sym == :front ? 1 : 10)}</td>" }
  end
end

Refactorings

No refactoring yet !

Avatar

Ryan Bates

October 24, 2007, October 24, 2007 16:46, permalink

2 ratings. Login to rate!

You can refactor the yardages partial by doing this. You may be able to apply this same concept to other areas as well.

_yardages.rhtml

1
2
3
4
5
6
<% %w[blue white red].each do |color| %>
<tr>
  <td><%= color.humanize %> Tee's</td>
  <%= render :partial => 'yards', :collection => @course.holes, :locals => { :color => color, :which => side } -%>
</tr>
<% end %>
E635ccff7389d9070f5e7e9fe8b36beb

rpheath

October 24, 2007, October 24, 2007 22:13, permalink

No rating. Login to rate!

Thanks, I ended up using a block helper, but it's basically the same thing...

_yardages.rhtml

1
2
3
4
5
6
<% yardage_rows do |color| %>
  <tr>
    <td><%= color.humanize -%> Tee's</td>
    <%= render :partial => 'yards', :collection => @course.holes, :locals => { :color => color, :which => side } -%>
  </tr>
<% end -%>

courses_helper.rb

1
2
3
4
5
def yardage_rows(&block)
  %w(blue white red).each do |color|
    yield color
  end
end

Your refactoring





Format Copy from initial code

or Cancel