2016-09-20 16:07:56 -04:00
|
|
|
class ProjectLabel < Label
|
2016-10-18 07:46:11 -04:00
|
|
|
MAX_NUMBER_OF_PRIORITIES = 1
|
2016-10-14 17:51:34 -04:00
|
|
|
|
2016-09-20 16:07:56 -04:00
|
|
|
belongs_to :project
|
|
|
|
|
|
|
|
validates :project, presence: true
|
2016-09-21 16:47:58 -04:00
|
|
|
|
2016-10-14 17:51:34 -04:00
|
|
|
validate :permitted_numbers_of_priorities
|
2016-09-21 16:47:58 -04:00
|
|
|
validate :title_must_not_exist_at_group_level
|
|
|
|
|
|
|
|
delegate :group, to: :project, allow_nil: true
|
|
|
|
|
2016-10-17 15:48:46 -04:00
|
|
|
alias_attribute :subject, :project
|
|
|
|
|
2016-10-27 22:43:31 -04:00
|
|
|
def subject_foreign_key
|
|
|
|
'project_id'
|
|
|
|
end
|
|
|
|
|
2016-10-11 15:51:11 -04:00
|
|
|
def to_reference(target_project = nil, format: :id)
|
|
|
|
super(project, target_project, format: format)
|
2016-09-28 23:21:47 -04:00
|
|
|
end
|
|
|
|
|
2016-09-21 16:47:58 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def title_must_not_exist_at_group_level
|
2016-10-13 16:45:39 -04:00
|
|
|
return unless group.present? && title_changed?
|
2016-09-21 16:47:58 -04:00
|
|
|
|
|
|
|
if group.labels.with_title(self.title).exists?
|
|
|
|
errors.add(:title, :label_already_exists_at_group_level, group: group.name)
|
|
|
|
end
|
|
|
|
end
|
2016-10-14 17:51:34 -04:00
|
|
|
|
|
|
|
def permitted_numbers_of_priorities
|
2016-10-18 07:46:11 -04:00
|
|
|
if priorities && priorities.size > MAX_NUMBER_OF_PRIORITIES
|
2016-10-14 17:51:34 -04:00
|
|
|
errors.add(:priorities, 'Number of permitted priorities exceeded')
|
|
|
|
end
|
|
|
|
end
|
2016-09-20 16:07:56 -04:00
|
|
|
end
|