Sha256: d11f0dca0103810795026586a17c3ac0f0bd6c0939f2938c29aaa05828187e2c

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

module Flyboy
  class Goal < ActiveRecord::Base
    include AASM

    aasm(column: "status", whiny_transitions: false) do
      state :open, initial: true
      state :closed

      event :close, if: :no_undone_tasks? do
        transitions from: [:open], to: :closed
      end

      event :open do
        transitions from: [:closed], to: :open
      end
    end

    has_many :tasks, dependent: :destroy

    validates :title, presence: true
    validates :status, inclusion: {in: Flyboy::Goal.aasm.states.map(&:to_s)}

    def no_undone_tasks?
      tasks.where(done: false).count == 0
    end

    def progress
      return 0 if tasks.count.zero?
      tasks.sum(:progress) / tasks.count
    end

    def revision
      "#{tracking} #{version}"
    end

    before_create :create_tracking

    def create_tracking
      dailycounter  = Goal.where("DATE(created_at) = ?", Date.today).count + 1
      self.tracking = "#{Time.now.strftime("%y%m%d")}-#{dailycounter}"
    end

    before_save :update_version

    def update_version
      self.version = 0 if self.version.nil?
      self.version = self.version + 1
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
flyboy-1.0.1 app/models/flyboy/goal.rb
flyboy-1.0.0 app/models/flyboy/goal.rb
flyboy-0.0.6 app/models/flyboy/goal.rb
flyboy-0.0.5 app/models/flyboy/goal.rb
flyboy-0.0.4 app/models/flyboy/goal.rb
flyboy-0.0.3 app/models/flyboy/goal.rb
flyboy-0.0.2 app/models/flyboy/goal.rb
flyboy-0.0.1 app/models/flyboy/goal.rb