Sha256: c749e7f26e497107ba4587126ef40c841344b38f1f40edf9f4fd80b27b535c47

Contents?: true

Size: 1.8 KB

Versions: 5

Compression:

Stored size: 1.8 KB

Contents

#encoding: utf-8

module Flyboy
  class Task < ActiveRecord::Base
    belongs_to :goal
    has_many :comments, class_name: Flyboy::TaskComment, inverse_of: :task, dependent: :destroy

    validates :goal,     presence: true
    validates :title,    presence: true
    validates :term,     presence: true
    validates :reminder, presence: true

    validate :validates_reminder_and_term

    def validates_reminder_and_term
      if reminder && term && reminder > term
        # errors.add(:reminder, "La date de relance doit être antérieure ou égale à la date d'échéance")
        errors.add(:reminder, :less_than, count: term)
      end
    end

    default_scope -> { order("term ASC") }

    def initialize(*args)
      super
      self.done = false                                    if done.nil?
      self.reminder = Date.today + snooze_default_reminder if reminder.nil?
      self.term     = Date.today + snooze_default_term     if term.nil?
    end

    def snooze
      if term_not_passed_yet
        if self.reminder + snooze_default_reminder > Date.today
          self.reminder += snooze_default_reminder
          self.term     += snooze_default_term
        else
          self.reminder = Date.today + 1
        end
      else
        self.reminder = Date.today + snooze_default_reminder
        self.term     = Date.today + snooze_default_term
      end
    end

    def snooze_default_reminder
      7
    end

    def snooze_default_term
      30
    end

    def self.to_csv(options = {})
      CSV.generate(options) do |csv|
        csv << ['Action', 'Titre', 'Echéance']

        all.each do |task|
          csv << [
            task.goal.title,
            task.title,
            I18n.l(task.term),
          ]
        end
      end
    end

    def term_not_passed_yet
      self.term > Date.today
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
flyboy-0.0.6 app/models/flyboy/task.rb
flyboy-0.0.5 app/models/flyboy/task.rb
flyboy-0.0.4 app/models/flyboy/task.rb
flyboy-0.0.3 app/models/flyboy/task.rb
flyboy-0.0.2 app/models/flyboy/task.rb