Class: Twitter::Bootstrap::Calendar::BootstrapCalendar

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter/bootstrap/calendar/bootstrap_calendar.rb

Constant Summary collapse

DAY_NAMES =
%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
MOBILE_DAY_NAMES =
%w[Sun Mon Tues Wed Thur Fri Sat]
START_DAY =
:sunday

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, date, options = {}, callback) ⇒ BootstrapCalendar

Returns a new instance of BootstrapCalendar.



16
17
18
19
20
21
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 16

def initialize(view, date, options={}, callback)
  @view = view
  @date = date
  @options = options.symbolize_keys
  @callback = callback
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



10
11
12
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 10

def callback
  @callback
end

#dateObject (readonly)

Returns the value of attribute date.



10
11
12
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 10

def date
  @date
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 10

def options
  @options
end

#viewObject (readonly)

Returns the value of attribute view.



10
11
12
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 10

def view
  @view
end

Instance Method Details

#calendar_divObject



28
29
30
31
32
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 28

def calendar_div
   'div', class: "calendar_grid" do
    header + week_rows
  end
end

#content_tag(*args, &block) ⇒ Object



12
13
14
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 12

def  *args, &block
  view.(*args, &block)
end

#day_cell(day) ⇒ Object



73
74
75
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 73

def day_cell(day)
   :div, view.capture(day, &callback), class: day_classes(day)
end

#day_classes(day) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 77

def day_classes(day)
  classes = ['span1']
  classes << "today" if day == Date.today
  classes << "notmonth" if day.month != date.month
  classes << "month" if day.month == date.month
  classes.empty? ? nil : classes.join(" ")
end

#day_namesObject



38
39
40
41
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 38

def day_names
  return DAY_NAMES if start_day_index.zero?
  DAY_NAMES[start_day_index, DAY_NAMES.size - start_day_index] + DAY_NAMES[0, start_day_index]
end

#headerObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 48

def header
   'div', class: 'month_header row-fluid' do
    standard = day_names.map { |day|
       :div, class: 'span1 visible-desktop' do
        I18n.t(day.downcase)
      end
    }.join.html_safe
    mobile = mobile_day_names.map { |day|
       :div, class: 'span1 hidden-desktop', style: 'width: 14.1%' do
        I18n.t(day.downcase)
      end
    }.join.html_safe

    standard + mobile
  end
end

#mobile_day_namesObject



43
44
45
46
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 43

def mobile_day_names
  return MOBILE_DAY_NAMES if start_day_index.zero?
  MOBILE_DAY_NAMES[start_day_index, MOBILE_DAY_NAMES.size - start_day_index] + MOBILE_DAY_NAMES[0, start_day_index]
end

#start_dayObject



23
24
25
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 23

def start_day
  @start_day ||= (options[:start_day] && DAY_NAMES.include?(options[:start_day].to_s.humanize)) ? options[:start_day].to_s.downcase.to_sym : :sunday
end

#start_day_indexObject



34
35
36
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 34

def start_day_index
  @start_day_index ||= DAY_NAMES.index {|n| n.downcase == start_day.to_s} || 0
end

#week_rowsObject



65
66
67
68
69
70
71
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 65

def week_rows
  weeks.map {|week|
     :div, class: 'row-fluid week' do
      week.map { |day| day_cell(day) }.join.html_safe
    end
  }.join.html_safe
end

#weeksObject



85
86
87
88
89
# File 'lib/twitter/bootstrap/calendar/bootstrap_calendar.rb', line 85

def weeks
  first = date.beginning_of_month.beginning_of_week(start_day)
  last = date.end_of_month.end_of_week(start_day)
  (first..last).to_a.in_groups_of(7)
end