diff options
Diffstat (limited to 'app/models/calendar/period.rb')
| -rw-r--r-- | app/models/calendar/period.rb | 108 | 
1 files changed, 44 insertions, 64 deletions
diff --git a/app/models/calendar/period.rb b/app/models/calendar/period.rb index eb1bb5370..bfde242f3 100644 --- a/app/models/calendar/period.rb +++ b/app/models/calendar/period.rb @@ -1,83 +1,63 @@ -class Calendar::Period -  include ActiveAttr::Model +class Calendar < ActiveRecord::Base +   +  class Period +    include ActiveAttr::Model -  attribute :id, type: Integer -  attribute :begin, type: Date -  attribute :end, type: Date +    attribute :id, type: Integer +    attribute :begin, type: Date +    attribute :end, type: Date -  validate :check_end_greather_than_begin -  validates_presence_of :begin, :end -  validate :validate_dates +    validates_presence_of :begin, :end +    validate :check_end_greather_than_begin -  def initialize(args={}) -    super -    self.begin = Calendar::CalendarDate.from_date(self.begin) if Date === self.begin -    self.end = Calendar::CalendarDate.from_date(self.end) if Date === self.end -  end - -  def check_end_greather_than_begin -    if self.begin and self.end and self.begin > self.end -      errors.add(:end, :invalid) +    def check_end_greather_than_begin +      if self.begin and self.end and self.begin > self.end +        errors.add(:end, :invalid) +      end      end -  end -  def self.from_range(index, range) -    new \ -      id:    index,  -      begin: Calendar::CalendarDate.from_date(range.begin),  -      end:   Calendar::CalendarDate.from_date(range.end) -  end +    def self.from_range(range, index) +      last = range.exclude_end? ? range.end - 1.day : range.end +      Period.new id: index, begin: range.begin, end: last +    end -  def range -    if self.begin and self.end and self.begin <= self.end -      Range.new self.begin, self.end +    def range +      if self.begin and self.end and self.begin <= self.end +        Range.new self.begin, self.end +      end      end -  end -  def intersect?(*other) -    return false if range.nil? +    def intersect?(*other) +      return false if range.nil? -    other = other.flatten -    other = other.delete_if { |o| o.id == id } if id +      other = other.flatten +      other = other.delete_if { |o| o.id == id } if id -    other.any? do |period| -      if other_range = period.range -        (range & other_range).present? +      other.any? do |period| +        if other_range = period.range +          (range & other_range).present? +        end        end      end -  end -  def validate_dates -    validate_begin -    validate_end -  end - -  def validate_begin -    errors.add(:begin, I18n.t('activerecord.errors.models.calendar.attributes.dates.illegal_date', date: self.begin.to_s)) unless self.begin.try( :legal? ) -  end - -  def validate_end -    errors.add(:end, I18n.t('activerecord.errors.models.calendar.attributes.dates.illegal_date', date: self.end.to_s)) unless self.end.try( :legal? ) -  end +    def cover? date +      range.cover? date +    end -  def cover? date -    range.cover? date -  end +    # Stuff required for coocon +    def new_record? +      !persisted? +    end -  # Stuff required for coocon -  def new_record? -    !persisted? -  end +    def persisted? +      id.present? +    end -  def persisted? -    id.present? -  end +    def mark_for_destruction +      self._destroy = true +    end -  def mark_for_destruction -    self._destroy = true +    attribute :_destroy, type: Boolean +    alias_method :marked_for_destruction?, :_destroy    end - -  attribute :_destroy, type: Boolean -  alias_method :marked_for_destruction?, :_destroy  end -  | 
