Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom CSS classes for date <td> elements #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/table_builder/calendar_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ def day(*args)
options = options_from_hash(args)
day_method = options.delete(:day_method) || :date
id_pattern = options.delete(:id)
css_lambda = options.delete(:class)
tbody do
@calendar.objects_for_days(@objects, day_method).to_a.sort{|a1, a2| a1.first <=> a2.first }.each do |o|
key, array = o
day, objects = array
concat(tag(:tr, options, true)) if(day.wday == @calendar.first_weekday)
concat(tag(:td, td_options(day, id_pattern), true))
concat(tag(:td, td_options(day, id_pattern, css_lambda), true))
yield(day, objects)
concat('</td>')
concat('</tr>') if(day.wday == @calendar.last_weekday)
Expand All @@ -42,13 +43,14 @@ def objects_for_days
@calendar.objects_for_days(@objects)
end

def td_options(day, id_pattern)
def td_options(day, id_pattern, css_lambda)
options = {}
css_classes = []
css_classes << 'today' if day.strftime("%Y-%m-%d") == @today.strftime("%Y-%m-%d")
css_classes << 'notmonth' if day.month != @calendar.month
css_classes << 'weekend' if day.wday == 0 or day.wday == 6
css_classes << 'future' if day > @today.to_date
css_classes << 'today' if day.strftime("%Y-%m-%d") == @today.strftime("%Y-%m-%d")
css_classes << 'notmonth' if day.month != @calendar.month
css_classes << 'weekend' if day.wday == 0 or day.wday == 6
css_classes << 'future' if day > @today.to_date
css_classes << css_lambda.call(day) if !css_lambda.nil?
options[:class] = css_classes.join(' ') unless css_classes.empty?
options[:id] = day.strftime(id_pattern) if id_pattern
options
Expand Down
19 changes: 19 additions & 0 deletions test/calendar_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ def test_calendar_for_sets_css_classes
assert_dom_equal expected, output
end

def test_calendar_for_sets_custom_css_classes
custom_css = lambda { |day| day.day.even? ? "even" : "odd" }
output = calendar_for([], :year=> 2008, :month => 12, :today => Date.civil(2008, 12, 15)) do |c|
c.day(:class => custom_css) do |day, events|
concat(events.collect{|e| e.id}.join)
end
end
expected = %(<table>) <<
%(<tbody>) <<
%(<tr><td class="notmonth weekend even"></td><td class="odd"></td><td class="even"></td><td class="odd"></td><td class="even"></td><td class="odd"></td><td class="weekend even"></td></tr>) <<
%(<tr><td class="weekend odd"></td><td class="even"></td><td class="odd"></td><td class="even"></td><td class="odd"></td><td class="even"></td><td class="weekend odd"></td></tr>) <<
%(<tr><td class="weekend even"></td><td class="today odd"></td><td class="future even"></td><td class="future odd"></td><td class="future even"></td><td class="future odd"></td><td class="weekend future even"></td></tr>) <<
%(<tr><td class="weekend future odd"></td><td class="future even"></td><td class="future odd"></td><td class="future even"></td><td class="future odd"></td><td class="future even"></td><td class="weekend future odd"></td></tr>) <<
%(<tr><td class="weekend future even"></td><td class="future odd"></td><td class="future even"></td><td class="future odd"></td><td class="notmonth future odd"></td><td class="notmonth future even"></td><td class="notmonth weekend future odd"></td></tr>) <<
%(</tbody>) <<
%(</table>)
assert_dom_equal expected, output
end

def test_calendar_for_thirty_days
today = Date.civil(2008, 12, 15)
output = calendar_for([], :today => today, :year=>2008, :month=>12, :first=>today, :last=>:thirty_days) do |c|
Expand Down