rosa-build/app/models/issue.rb

107 lines
3.4 KiB
Ruby
Raw Normal View History

class Issue < ActiveRecord::Base
include Feed::Issue
STATUSES = ['open', 'closed']
belongs_to :project
belongs_to :user
2014-01-21 04:51:49 +00:00
belongs_to :assignee, class_name: 'User', foreign_key: 'assignee_id'
belongs_to :closer, class_name: 'User', foreign_key: 'closed_by'
2014-01-21 04:51:49 +00:00
has_many :comments, as: :commentable, dependent: :destroy
has_many :subscribes, as: :subscribeable, dependent: :destroy
has_many :labelings, dependent: :destroy
has_many :labels, -> { uniq }, through: :labelings
has_one :pull_request#, dependent: :destroy
2014-01-21 04:51:49 +00:00
validates :title, :body, :project_id, presence: true
after_create :set_serial_id
after_create :subscribe_users
after_update :subscribe_issue_assigned_user
2012-04-13 20:44:04 +01:00
attr_accessible :labelings_attributes, :title, :body, :assignee_id
2014-01-21 04:51:49 +00:00
accepts_nested_attributes_for :labelings, allow_destroy: true
scope :opened, -> { where(status: 'open') }
scope :closed, -> { where(status: 'closed') }
2012-02-28 14:28:11 +00:00
scope :needed_checking, -> { where(issues: {status: ['open', 'blocked', 'ready', 'already']}) }
scope :not_closed_or_merged, -> { needed_checking }
scope :closed_or_merged, -> { where(issues: {status: ['closed', 'merged']}) }
# Using mb_chars for correct transform to lowercase ('Русский Текст'.downcase => "Русский Текст")
2014-03-11 11:47:03 +00:00
scope :search, ->(q) { where("#{table_name}.title ILIKE ?", "%#{q.mb_chars.downcase}%") if q.present? }
scope :without_pull_requests, -> {
where('NOT EXISTS (select null from pull_requests as pr where pr.issue_id = issues.id)')
}
2012-06-04 18:00:19 +01:00
def assign_uname
2012-04-13 20:44:04 +01:00
assignee.uname if assignee
end
def to_param
serial_id.to_s
end
def subscribe_creator(creator_id)
2014-01-21 04:51:49 +00:00
if !self.subscribes.exists?(user_id: creator_id)
self.subscribes.create(user_id: creator_id)
end
end
2012-02-28 14:05:18 +00:00
def closed?
closed_by && closed_at && status == 'closed'
2012-02-28 14:05:18 +00:00
end
2012-06-28 11:44:55 +01:00
def set_close(closed_by)
2012-06-13 18:52:11 +01:00
self.closed_at = Time.now.utc
2012-02-28 14:05:18 +00:00
self.closer = closed_by
2012-06-28 11:44:55 +01:00
self.status = 'closed'
2012-02-28 14:05:18 +00:00
end
def set_open
self.closed_at = self.closed_by = nil
self.status = 'open'
2012-02-28 14:05:18 +00:00
end
def collect_recipients
recipients = self.project.admins
recipients = recipients | [self.assignee] if self.assignee
recipients
end
2013-04-09 21:21:02 +01:00
def self.find_by_hash_tag hash_tag, current_ability, project
hash_tag =~ /([a-zA-Z0-9\-_]*\/)?([a-zA-Z0-9\-_]*)?#([0-9]+)/
2013-03-29 18:06:23 +00:00
owner_uname = Regexp.last_match[1].presence || Regexp.last_match[2].presence || project.owner.uname
project_name = Regexp.last_match[1] ? Regexp.last_match[2] : project.name
serial_id = Regexp.last_match[3]
project = Project.find_by_owner_and_name(owner_uname.chomp('/'), project_name)
return nil unless project
return nil unless current_ability.can? :show, project
2014-01-21 04:51:49 +00:00
project.issues.where(serial_id: serial_id).first
2013-03-29 18:06:23 +00:00
end
protected
def set_serial_id
self.serial_id = self.project.issues.count
self.save!
end
def subscribe_users
collect_recipients.each do |recipient|
2014-01-21 04:51:49 +00:00
if recipient.notifier.new_comment && !self.subscribes.exists?(user_id: recipient.id)
ss = self.subscribes.create(user_id: recipient.id)
2012-03-13 13:56:58 +00:00
end
end
end
def subscribe_issue_assigned_user
2012-04-13 20:44:04 +01:00
if self.assignee_id && self.assignee_id_changed?
2014-01-21 04:51:49 +00:00
self.subscribes.where(user_id: self.assignee_id_was).first.destroy unless self.assignee_id_was.blank?
if self.assignee.notifier.issue_assign && !self.subscribes.exists?(user_id: self.assignee_id)
self.subscribes.create(user_id: self.assignee_id)
end
end
end
end