Skip to content

Commit

Permalink
Add current-week class (#178)
Browse files Browse the repository at this point in the history
* add current week method

* test current-week class

* make sure other weeks don't have current-week

* combine rspec files
  • Loading branch information
Alex Graves authored and excid3 committed Aug 29, 2017
1 parent e3831c4 commit 314101a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/views/simple_calendar/_calendar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<tbody>
<% date_range.each_slice(7) do |week| %>
<tr>
<%= content_tag :tr, class: calendar.tr_classes_for(week) do %>
<% week.each do |day| %>
<%= content_tag :td, class: calendar.td_classes_for(day) do %>
<% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
Expand All @@ -26,7 +26,7 @@
<% end %>
<% end %>
<% end %>
</tr>
<% end %>
<% end %>
</tbody>
</table>
Expand Down
8 changes: 8 additions & 0 deletions lib/simple_calendar/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def td_classes_for(day)
td_class
end

def tr_classes_for(week)
today = Date.current
tr_class = ['week']
tr_class << 'current-week' if week.include?(today)

tr_class
end

def url_for_next_view
view_context.url_for(@params.merge(start_date_param => date_range.last + 1.day))
end
Expand Down
14 changes: 14 additions & 0 deletions spec/calendar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ def params
end
end

describe 'current week class' do
it 'should have the current week' do
calendar = SimpleCalendar::Calendar.new(ViewContext.new)
week = calendar.send(:date_range).each_slice(7).to_a[0]
expect(calendar.send(:tr_classes_for, week)).to include('current-week')
end

it 'should not have the current week if it does not contain today' do
calendar = SimpleCalendar::MonthCalendar.new(ViewContext.new)
week = calendar.send(:date_range).each_slice(7).to_a[0]
expect(calendar.send(:tr_classes_for, week)).to_not include('current-week')
end
end

it 'has a param that determines the start date of the calendar'
it 'generates a default date if no start date is present'
it 'has a range of dates'
Expand Down

0 comments on commit 314101a

Please sign in to comment.