Sha256: afc21fde556d1854dbe6ba9cf72f5ad8f3c3a08649833f1dc79e42cd7e036f6b

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module TimeWillTell
  module Helpers
    module DateRangeHelper

      def date_range(from_date, to_date, options = {})
        format    = options.fetch(:format, :short)
        scope     = options.fetch(:scope, 'time_will_tell.date_range')
        separator = options.fetch(:separator, '—')

        month_names = format.to_sym == :short ? I18n.t("date.abbr_month_names") : I18n.t("date.month_names")

        from_date, to_date = to_date, from_date if from_date > to_date
        from_day   = from_date.day
        from_month = month_names[from_date.month]
        from_year  = from_date.year
        to_day     = to_date.day

        dates = { from_day: from_day, sep: separator }

        if from_date == to_date
          template = :same_date
          dates.merge!(month: from_month, year: from_year)
        elsif from_date.month == to_date.month && from_date.year == to_date.year
          template = :same_month
          dates.merge!(to_day: to_day, month: from_month, year: from_year)
        else
          to_month = month_names[to_date.month]

          dates.merge!(from_month: from_month, to_month: to_month, to_day: to_day)

          if from_date.year == to_date.year
            template = :different_months_same_year
            dates.merge!(year: from_year)
          else
            to_year = to_date.year

            template = :different_years
            dates.merge!(from_year: from_year, to_year: to_year)
          end
        end

        I18n.t("#{scope}.#{template}", dates)
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
time_will_tell-0.0.3 lib/time_will_tell/helpers/date_range_helper.rb
time_will_tell-0.0.2 lib/time_will_tell/helpers/date_range_helper.rb
time_will_tell-0.0.1 lib/time_will_tell/helpers/date_range_helper.rb