From 329ec6c18a0d771384bbb6ad82d0db6e48364826 Mon Sep 17 00:00:00 2001 From: Philipp Tessenow Date: Fri, 2 May 2014 13:10:15 +0200 Subject: [PATCH] refactor and document the calendars helper --- app/helpers/calendars_helper.rb | 64 +++++++++++++++------------------ 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/app/helpers/calendars_helper.rb b/app/helpers/calendars_helper.rb index 6d5458e88c..283534ec93 100644 --- a/app/helpers/calendars_helper.rb +++ b/app/helpers/calendars_helper.rb @@ -27,46 +27,38 @@ # See doc/COPYRIGHT.rdoc for more details. #++ +# Provides helper methods for a project's calendar view. module CalendarsHelper - def link_to_previous_month(year, month, options={}) - target_year, target_month = if month == 1 - [year - 1, 12] - else - [year, month - 1] - end - - name = if target_month == 12 - "#{localized_month_name target_month} #{target_year}" - else - "#{localized_month_name target_month}" - end - - link_to_month(name, target_year, target_month, options.merge(:class => 'navigate-left')) + # Generates a html link to a calendar of the previous month. + # @param [Integer] year the current year + # @param [Integer] month the current month + # @param [Hash, nil] options html options passed to the link generation + # @return [String] link to the calendar + def link_to_previous_month(year, month, options = {}) + target_date = Date.new(year, month, 1) - 1.month + link_to_month(target_date, options.merge(class: 'navigate-left', + display_year: target_date.year != year)) end - def link_to_next_month(year, month, options={}) - target_year, target_month = if month == 12 - [year + 1, 1] - else - [year, month + 1] - end - - name = if target_month == 1 - "#{localized_month_name target_month} #{target_year}" - else - "#{localized_month_name target_month}" - end - - link_to_month(name, target_year, target_month, options.merge(:class => 'navigate-right')) + # Generates a html link to a calendar of the next month. + # @param [Integer] year the current year + # @param [Integer] month the current month + # @param [Hash, nil] options html options passed to the link generation + # @return [String] link to the calendar + def link_to_next_month(year, month, options = {}) + target_date = Date.new(year, month, 1) + 1.month + link_to_month(target_date, options.merge(class: 'navigate-right', + display_year: target_date.year != year)) end - def link_to_month(link_name, year, month, options={}) - link_to_content_update(link_name, params.merge(:year => year, :month => month), options) - end - - private - - def localized_month_name(month_index) - ::I18n.t('date.month_names')[month_index] + # Generates a html-link which leads to a calendar displaying the given date. + # @param [Date, Time] date the date which should be displayed + # @param [Hash, nil] options html options passed to the link generation + # @options options [Boolean] :display_year Whether the year should be displayed + # @return [String] link to the calendar + def link_to_month(date_to_show, options = {}) + date = date_to_show.to_date + name = ::I18n.l date, format: options.delete(:display_year) ? '%B %Y' : '%B' + link_to_content_update(name, params.merge(:year => date.year, :month => date.month), options) end end