diff options
Diffstat (limited to 'app/models/work_items/parent_link.rb')
-rw-r--r-- | app/models/work_items/parent_link.rb | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/app/models/work_items/parent_link.rb b/app/models/work_items/parent_link.rb index 3c405dbce3b..f5ebbfa59b8 100644 --- a/app/models/work_items/parent_link.rb +++ b/app/models/work_items/parent_link.rb @@ -5,11 +5,13 @@ module WorkItems self.table_name = 'work_item_parent_links' MAX_CHILDREN = 100 + PARENT_TYPES = [:issue, :incident].freeze belongs_to :work_item belongs_to :work_item_parent, class_name: 'WorkItem' - validates :work_item, :work_item_parent, presence: true + validates :work_item_parent, presence: true + validates :work_item, presence: true, uniqueness: true validate :validate_child_type validate :validate_parent_type validate :validate_same_project @@ -21,15 +23,20 @@ module WorkItems return unless work_item unless work_item.task? - errors.add :work_item, _('Only Task can be assigned as a child in hierarchy.') + errors.add :work_item, _('only Task can be assigned as a child in hierarchy.') end end def validate_parent_type return unless work_item_parent - unless work_item_parent.issue? - errors.add :work_item_parent, _('Only Issue can be parent of Task.') + base_type = work_item_parent.work_item_type.base_type.to_sym + unless PARENT_TYPES.include?(base_type) + parent_names = WorkItems::Type::BASE_TYPES.slice(*WorkItems::ParentLink::PARENT_TYPES) + .values.map { |type| type[:name] } + + errors.add :work_item_parent, _('only %{parent_types} can be parent of Task.') % + { parent_types: parent_names.to_sentence } end end @@ -37,7 +44,7 @@ module WorkItems return if work_item.nil? || work_item_parent.nil? if work_item.resource_parent != work_item_parent.resource_parent - errors.add :work_item_parent, _('Parent must be in the same project as child.') + errors.add :work_item_parent, _('parent must be in the same project as child.') end end @@ -46,7 +53,7 @@ module WorkItems max = persisted? ? MAX_CHILDREN : MAX_CHILDREN - 1 if work_item_parent.child_links.count > max - errors.add :work_item_parent, _('Parent already has maximum number of children.') + errors.add :work_item_parent, _('parent already has maximum number of children.') end end end |