1
0

Changed Unit uniqueness condtions to shortname only

This commit is contained in:
cryptogopher 2019-08-22 22:10:51 +02:00
parent b0a02b7474
commit b1be5e571a
3 changed files with 6 additions and 4 deletions

View File

@ -27,9 +27,10 @@ class UnitsController < ApplicationController
end
def import
available = Unit.where(project: @project).pluck(:shortname)
defaults = Unit.where(project: nil).pluck(:name, :shortname)
missing = defaults - Unit.where(project: @project).pluck(:name, :shortname)
@project.units.create(missing.map { |n, s| {name: n, shortname: s} })
defaults.delete_if { |n, s| available.include?(s) }
@project.units.create(defaults.map { |n, s| {name: n, shortname: s} })
redirect_to project_units_url(@project)
end

View File

@ -2,5 +2,6 @@ class Unit < ActiveRecord::Base
belongs_to :project
validates :project, associated: true
validates :name, :shortname, presence: true, uniqueness: true
validates :name, presence: true
validates :shortname, presence: true, uniqueness: {scope: :project_id}
end

View File

@ -15,4 +15,4 @@ en:
index:
heading: 'Units'
heading_new_unit: 'New unit'
heading_import: 'Import'
heading_import: 'Import defaults'