Sha256: 5b60641bf3b939ae00e165a8bc8b1455f1420b4e2592b115232dcb36986be04b

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require "dorsale/search"

module Flyboy
  class Goal < ActiveRecord::Base
    include AASM
    include Dorsale::Search

    paginates_per 50

    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: proc { Flyboy::Goal.aasm.states.map(&:to_s) }
    }

    def initialize(*args)
      super
      self.progress = 0 if progress.nil?
    end

    def no_undone_tasks?
      tasks.where(done: false).count == 0
    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

    def update_progress
      if tasks.count.zero?
        self.progress = 0
      else
        self.progress = tasks.sum(:progress) / tasks.count
      end
    end

    def update_progress!
      update_progress
      save
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flyboy-1.0.7 app/models/flyboy/goal.rb
flyboy-1.0.6 app/models/flyboy/goal.rb
flyboy-1.0.5 app/models/flyboy/goal.rb