Sha256: 240d7b486963a34d7fe174d627afbf39c82c533b08becb21321ab9e5b8656a13

Contents?: true

Size: 1.81 KB

Versions: 9

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module Timely
  class Season < ActiveRecord::Base
    has_many :date_groups, -> { order(:start_date) }, dependent: :destroy, class_name: 'Timely::DateGroup', inverse_of: :season

    accepts_nested_attributes_for :date_groups,
                                  reject_if: proc { |attributes| attributes['start_date'].blank? },
                                  allow_destroy: true

    validate :validate_dates_specified

    def validate_dates_specified
      errors.add(:base, 'No dates specified') if date_groups.blank?
      errors.empty?
    end

    def includes_date?(date)
      date_groups.any? { |dg| dg.includes_date?(date) }
    end

    def dates
      date_groups.map do |date_group|
        ((date_group.start_date)..(date_group.end_date)).to_a
      end.flatten
    end

    def boundary_range
      boundary_start..boundary_end
    end

    def past?
      boundary_end && boundary_end < ::Date.current
    end

    def boundary_start
      date_groups.map(&:start_date).min
    end

    def boundary_end
      date_groups.map(&:end_date).max
    end

    def within_boundary?(date)
      boundary_start && boundary_end && boundary_start <= date && boundary_end >= date
    end

    def deep_clone
      # Use clone until it is removed in AR 3.1, then dup is the same
      method = ActiveRecord::Base.instance_methods(false).include?(:clone) ? :clone : :dup
      cloned = send(method)
      date_groups.each do |dg|
        cloned.date_groups.build(dg.send(method).attributes)
      end
      cloned
    end

    def to_s
      name.presence || Timely::DateRange.to_s(boundary_start, boundary_end)
    end

    alias audit_name to_s

    def string_of_date_groups
      date_groups.map  do |dg|
        "#{dg.start_date.to_s(:short)} - #{dg.end_date.to_s(:short)}"
      end.to_sentence
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
timely-0.14.0 lib/timely/rails/season.rb
timely-0.13.0 lib/timely/rails/season.rb
timely-0.12.0 lib/timely/rails/season.rb
timely-0.11.0 lib/timely/rails/season.rb
timely-0.10.0 lib/timely/rails/season.rb
timely-0.9.0 lib/timely/rails/season.rb
timely-0.8.0 lib/timely/rails/season.rb
timely-0.7.0 lib/timely/rails/season.rb
timely-0.6.0 lib/timely/rails/season.rb