class Ticket < ActiveRecord::Base include AASM include PublicActivity::Common # include Tire::Model::Search # include Tire::Model::Callbacks has_ancestry #touch: true acts_as_list scope: [:ancestry] attr_accessor :old_ancestor_ids class InvalidTypeError < StandardError; end class UnknownEventError < StandardError; end ALLOWED_TYPES = %w(story task bug) belongs_to :creator, class_name: 'User' validates_presence_of :creator validates_presence_of :title has_many :comments has_many :assignments has_many :attachments, as: :attachable, dependent: :destroy after_save :touch_ancestry ACTIVE_STATES = [:open, :current, :current, :completed] scope :active, -> {where(state: ACTIVE_STATES)} ARCHIVED_STATES = [:rejected, :verified] scope :archived, -> {where(state: ARCHIVED_STATES)} scope :with_state, ->(state) {where(state: state.to_s)} aasm column: :state do state :open, initial: true state :current state :completed state :rejected state :verified event :reopen do transitions to: :open, from: [:current, :completed, :rejected, :verified] end event :start do transitions to: :current, from: :open end event :restart do transitions to: :current, from: [:verified, :completed] end event :complete do transitions to: :completed, from: [:open, :current] end event :reject do transitions to: :rejected, from: [:open, :current] end event :verify do transitions to: :verified, from: [:current, :completed] end end def self.filtered_type_class(type) raise InvalidTypeError unless ALLOWED_TYPES.include?(type) type.classify.constantize end def self.ordered order("position ASC") end def short_title(num_words = 2) words = title.split short = words[0..num_words].join(" ") short += "..." if words.count > num_words short end def assignment_users_hash assignments.each_with_object([]) do |assignment, array| array << { id: assignment.user.id, name: assignment.user.display_name, } end end def assignment_users assignments.map{|assignment| [assignment.user.display_name, assignment.user.id]} end def tags "" end def trigger_event!(event) raise UnknownEventError unless self.aasm.events.include?(event.to_sym) self.send("#{event}!") end def reorder!(options) if options[:parent_id] self.old_ancestor_ids = self.ancestor_ids self.parent_id = options[:parent_id] self.save end self.insert_at options[:position].to_i self.save end def touch_ancestry self.old_ancestor_ids ||= [] Ticket.where(id: (ancestor_ids + old_ancestor_ids).uniq).update_all(updated_at: Time.now) end end