Sha256: 07bef80130452c2401a54784c41eb34fe6aa9a81b420af0f49377edf9da45c7b

Contents?: true

Size: 1.67 KB

Versions: 29

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module PagesCore
  module Admin
    module DateRangeHelper
      def page_date_range(page)
        if page.all_day?
          date_range(page.starts_at.to_date, page.ends_at.to_date)
        else
          date_range(page.starts_at, page.ends_at)
        end
      end

      def date_range(starts_at, ends_at)
        dates = different_year_date_range(starts_at, ends_at) ||
                different_month_date_range(starts_at, ends_at) ||
                different_day_date_range(starts_at, ends_at) ||
                same_day_date_range(starts_at, ends_at)
        safe_join(dates.map(&:strip), "–".html_safe)
      end

      private

      def different_year_date_range(starts_at, ends_at)
        return if starts_at.year == ends_at.year

        [l(starts_at, format: :pages_full), l(ends_at, format: :pages_full)]
      end

      def different_month_date_range(starts_at, ends_at)
        return if starts_at.month == ends_at.month

        [l(starts_at, format: :pages_date), l(ends_at, format: :pages_full)]
      end

      def different_day_date_range(starts_at, ends_at)
        return if starts_at.day == ends_at.day

        if starts_at.is_a?(Date) && ends_at.is_a?(Date)
          [l(starts_at, format: :pages_day), l(ends_at, format: :pages_full)]
        else
          [l(starts_at, format: :pages_date), l(ends_at, format: :pages_full)]
        end
      end

      def same_day_date_range(starts_at, ends_at)
        if starts_at.is_a?(Date) && ends_at.is_a?(Date)
          [l(starts_at, format: :pages_full)]
        else
          [l(starts_at, format: :pages_full), l(ends_at, format: :pages_time)]
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
pages_core-3.15.5 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.15.4 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.15.3 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.15.2 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.15.1 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.14.0 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.13.0 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.12.7 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.12.6 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.12.5 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.12.4 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.12.3 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.12.2 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.12.1 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.12.0 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.11.3 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.11.2 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.11.1 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.11.0 app/helpers/pages_core/admin/date_range_helper.rb
pages_core-3.10.2 app/helpers/pages_core/admin/date_range_helper.rb