Class: CalendarHelper::CalendarBuilder

Inherits:
TableHelper::TableBuilder show all
Defined in:
lib/table_builder/calendar_helper.rb

Instance Method Summary collapse

Methods inherited from TableHelper::TableBuilder

#body, #body_r, #d, #h, #head, #head_r, #r

Constructor Details

#initialize(objects, template, calendar, options) ⇒ CalendarBuilder

Returns a new instance of CalendarBuilder.



11
12
13
14
15
# File 'lib/table_builder/calendar_helper.rb', line 11

def initialize objects, template, calendar, options
  @calendar = calendar.new(options)
  @today    = options[:today] || Time.now
  super objects, template, options
end

Instance Method Details

#day(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/table_builder/calendar_helper.rb', line 17

def day options = {}
  raise ArgumentError, "Missing block" unless block_given?
  day_method = options.delete(:day_method) || :date
  id_pattern = options.delete(:id)
  
  tag(:tbody) do
    output = ''
    @calendar.objects_for_days(@objects, &day_method).each_slice(7) do |week|
      output = r do
        week.map do |day, objects|
          d capture{yield day, objects}, td_options(day, objects, id_pattern)
        end
      end
    end
    output
  end
  
end

#td_options(day, objects, id_pattern) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/table_builder/calendar_helper.rb', line 36

def td_options day, objects, id_pattern
  options = {}
  css     = []
  css << 'notmonth' if day.month != @calendar.month
  css << 'today'    if @today == day
  css << 'weekend'  if day.wday == 0 or day.wday == 6
  # css << 'empty'    if objects.empty?

  options[:class] = css.join(' ')
  options[:id]    = day.strftime(id_pattern) if id_pattern
  
  options.delete_if{ |k,v| v.blank? }
  options
end