module CalendarsHelper # Builds a calendar for the given date # # * date: Determines the month to be displayed. Given date will have the "today" css class # * opts: # - headers: Array of weekday headers (optional) # def spud_calendar_for_date(date, opts = {}, &block) events = SpudCalendarEvent.in_month_of(date) spud_calendar_for_events(events, date, opts, &block) end # Builds a calendar for the given date and identifier # # * identifier: Symbol for the SpudCalendar you wish to display # * date: Determines the month to be displayed. Given date will have the "today" css class # * opts: # - headers: Array of weekday headers (optional) # def spud_calendar_for_identifier(identifier, date, opts = {}, &block) calendar = SpudCalendar.with_identifier(identifier) if calendar.present? events = calendar.spud_calendar_events.in_month_of(date) else logger.warn "Failed to load calendar by identifer '#{identifier}'. Does it exist?" events = [] end spud_calendar_for_events(events, date, opts, &block) end # Builds a calendar for the given date, with events # # * events: An array or relation of SpudCalendarEvent records # * date: Determines the month to be displayed. Given date will have the "today" css class # * opts: # - headers: Array of weekday headers (optional) # def spud_calendar_for_events(events, date, opts = {}, &block) events_array = events.to_a cache(['spud-calendar', date.to_s, events]) do concat( SpudEvents::CalendarBuilder.new(self, date, opts).table do |day| events = extract_events_for_date(events_array, day) if block block.call(day, events) else concat content_tag(:span, day.day.to_s, class: 'calendar-day') if events.length > 0 list_items = events.collect do |event| '