css - Calculating the number of weeks in a month for a calendar in Rails -


i have calendar shows public events month. responsive layout, need able calculate week_row height. therefore, need able calculate number of weeks in month, including partial weeks (weeks contain days current month , previous/next month).

the following method calculates number of weeks in month:

def weeks_in_month(date)   @week_count = (0.5 + (date.end_of_month.day + date.beginning_of_month.wday).to_f / 7.0).round end 

and code add appropriate class(es) <div> is:

def week_rows    weeks.map {|week|      content_tag :div, class: week_classes(week)        week.map { |day| day_cell(day) }.join.html_safe      end    }.join.html_safe end  def week_classes(date)   classes = ['week']   classes << "wk_rows_4" if weeks_in_month(date) == 4   classes << "wk_rows_5" if weeks_in_month(date) == 5   classes << "wk_rows_6" if weeks_in_month(date) == 6   classes.empty? ? nil : classes.join(" ") end 

however, have missed something. week_classes aren't populating other default 'week' class.

this based off of railscasts #213, modified per this tutorial on creating calendar without tables, borrows heavily rc#231.

any idea why it's not assigning correct class <div>?

everything else in calendar working. here complete code:

module calendarhelper   def calendar(date = date.today, &block)     calendar.new(self, date, block).calendar_div   end    class calendar < struct.new(:view, :date, :callback)     header = %w[sunday monday tuesday wednesday thursday friday saturday]     start_day = :sunday      delegate :content_tag, to: :view      def calendar_div        content_tag 'div', id: "calendar"           header + week_rows        end     end      def header        content_tag 'div', id: 'weekdays'           header.map { |day| content_tag :div, class: 'weekday'              day           end }.join.html_safe        end     end      def week_rows        weeks.map {|week|           content_tag :div, class: week_classes(week)              week.map { |day| day_cell(day) }.join.html_safe           end        }.join.html_safe     end      def week_classes(date)        classes = ['week']        classes << "wk_rows_4" if weeks_in_month(date) == 4        classes << "wk_rows_5" if weeks_in_month(date) == 5        classes << "wk_rows_6" if weeks_in_month(date) == 6        classes.empty? ? nil : classes.join(" ")     end      def day_cell(day)        content_tag :div, view.capture(day, &callback), class: day_classes(day)     end      def day_classes(day)        classes = ['day']        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      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      def weeks_in_month(date)        @week_count = (0.5 + (date.end_of_month.day + date.beginning_of_month.wday).to_f / 7.0).round     end   end 

ok, fiddling around lead me solution:

  def week_rows      weeks.map {|week|         content_tag :div, class: week_classes            week.map { |day| day_cell(day) }.join.html_safe         end      }.join.html_safe   end    def week_classes      classes = ['week']      classes << "wk_rows_4" if weeks_in_month == 4      classes << "wk_rows_5" if weeks_in_month == 5      classes << "wk_rows_6" if weeks_in_month == 6      classes.empty? ? nil : classes.join(" ")   end    def weeks_in_month      (0.5 + (date.end_of_month.day + date.at_beginning_of_month.wday).to_f / 7.0).round   end 

it wasn't necessary pass in (date) parameter.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -