class RailsCalendar::Simple

Constants

DAYS

Attributes

calendar_day[RW]
callback[RW]
config[R]
view_context[RW]

Public Class Methods

new(calendar_day, view_context = nil, &block) click to toggle source
# File lib/rails_calendar/simple.rb, line 16
def initialize(calendar_day, view_context = nil, &block)
  @config = RailsCalendar.configuration
  @calendar_day = calendar_day
  @view_context = view_context
  @callback = block
end

Public Instance Methods

to_s() click to toggle source
# File lib/rails_calendar/simple.rb, line 23
def to_s
  table
end

Private Instance Methods

body() click to toggle source
# File lib/rails_calendar/simple.rb, line 47
def body
  rows = weeks.map do |week|
    content_tag :tr do
      week.each do |day|
        concat day_cell(day)
      end
    end
  end

  content_tag :tbody, rows.join('').html_safe
end
date_callback(date) click to toggle source
# File lib/rails_calendar/simple.rb, line 65
def date_callback(date)
  return nil unless callback.present?

  if view_context.present?
    view_context.capture(date, &callback)
  else
    capture(date, &callback)
  end
end
day_cell(date) click to toggle source
# File lib/rails_calendar/simple.rb, line 75
def day_cell(date)
  callback_contents = date_callback(date)

  content_tag(:td, class: day_cell_classes(date)) do
    concat content_tag(:span, date.day, class: config.get_class(:day_number))

    if callback_contents.present?
      concat content_tag(:div, callback_contents, class: config.get_class(:day_contents))
    end
  end
end
day_cell_classes(date) click to toggle source
# File lib/rails_calendar/simple.rb, line 87
def day_cell_classes(date)
  classes = []
  classes << config.get_class(:day_cell)
  classes << config.get_class(:today) if date == Date.today
  classes << config.get_class(:another_month) unless date.month == calendar_day.month
  classes.empty? ? nil : classes.join(' ')
end
header() click to toggle source
# File lib/rails_calendar/simple.rb, line 35
def header
  index = DAYS.index(config.start_of_week)

  content_tag :thead do
    content_tag :tr do
      I18n.t(config.i18n_days).rotate(index).each do |day|
        concat content_tag(:th, day.titleize, class: config.get_class(:day_name))
      end
    end
  end
end
table() click to toggle source
# File lib/rails_calendar/simple.rb, line 29
def table
  content_tag :table, class: config.get_class(:table) do
    header + body
  end
end
weeks() click to toggle source
# File lib/rails_calendar/simple.rb, line 59
def weeks
  first = calendar_day.beginning_of_month.beginning_of_week(config.start_of_week)
  last = calendar_day.end_of_month.end_of_week(config.start_of_week)
  (first..last).to_a.in_groups_of(7)
end