Last active
September 12, 2017 20:33
-
-
Save jbmyid/6682830 to your computer and use it in GitHub Desktop.
Display dynamic calendar in view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CalendarHelper | |
def calendar(date = Date.today,options={}, &block) | |
Calendar.new(self, date,options, block).table | |
end | |
class Calendar < Struct.new(:view, :date,:options, :callback) | |
HEADER = %w[SUN MON TUES WED THU FRI SAT] | |
START_DAY = :sunday | |
delegate :content_tag, to: :view | |
def table | |
cal_table = content_tag :table, class: "calendar" do | |
header + week_rows | |
end | |
[month_row,cal_table].join.html_safe | |
end | |
def month_row | |
options.present? ? (content_tag :div, options[:prev_link] + content_tag(:div, "#{Date::MONTHNAMES[date.month].upcase} #{date.year}", class: "monthYear") + options[:next_link]).html_safe : "" | |
end | |
def header | |
content_tag :tr do | |
HEADER.map { |day| content_tag :th, day }.join.html_safe | |
end | |
end | |
def week_rows | |
weeks.map do |week| | |
content_tag :tr do | |
week.map { |day| day_cell(day) }.join.html_safe | |
end | |
end.join.html_safe | |
end | |
def day_cell(day) | |
content_tag :td, (date.month==day.month ? view.capture(day, &callback) : ""), class: day_classes(day) | |
end | |
def day_classes(day) | |
classes = [] | |
classes << "blackout slctdDate" if options[:blackout_dates] && options[:blackout_dates].include?(day) | |
classes << "today" if day == Date.today | |
classes << "notmonth" if day.month != date.month | |
classes.empty? ? nil : classes.join(" ") | |
end | |
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 | |
end | |
end | |
#####in haml | |
.bookingCalendar | |
- current_date = current_date || Date.today | |
= calendar(current_date, prev_link: link_to("", calendar_user_parking_lot_profile_path(profile, date: (current_date - 1.month)),remote: true, class: "monthDir leftArrow"), next_link: link_to("", calendar_user_parking_lot_profile_path(profile, date: (current_date + 1.month)),remote: true, class: "monthDir rgtArrow"), blackout_dates: profile.blackout_dates.map{|d| d.date}) do |date| | |
= link_to booking_on_date_user_parking_lot_profile_path(profile, date: date), remote: true do | |
%div.dayBlock | |
.date= date.day | |
= "extra's" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment