Sha256: d18ac1f32d11cf7119274e13a8cf7071961cda3fe899609a5d808377cdaed276

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module Timely
class Season < ActiveRecord::Base
#  acts_as_audited

  has_many :date_groups, :order => :start_date, :dependent => :destroy

 # has_many :fare_bases

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


  def validate
    errors.add_to_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 has_gaps?
    last_date = nil
    date_groups.each do |dg|
      return true if last_date && dg.start_date != last_date + 1
    end
    false
  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 boundary_start
    date_groups.map(&:start_date).sort.first
  end


  def boundary_end
    date_groups.map(&:end_date).sort.last
  end


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


  def deep_clone
    cloned = self.clone
    date_groups.each do |dg|
      cloned.date_groups.build(dg.clone.attributes.except(:id))
    end
    cloned
  end


  def self.build_season_for(dates=[])
    season = Season.new
    date_groups = dates.map do |date|
      DateGroup.new(:start_date => date, :end_date => date)
    end
    season.date_groups = date_groups
    season
  end

  def to_s
    name
  end
  alias_method :audit_name, :to_s

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



# == Schema Information
#
# Table name: seasons
#
#  id         :integer(4)      not null, primary key
#  name       :string(255)
#  created_at :datetime
#  updated_at :datetime
#

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
timely-0.0.2 lib/timely/rails/season.rb