Skip to content

Commit a4549c8

Browse files
committed
Improve create action to check for existing attributes
1 parent e0d7a70 commit a4549c8

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/api/app/controllers/webui/attribute_controller.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Webui::AttributeController < Webui::WebuiController
22
helper :all
3-
before_action :set_project, only: %i[index new edit]
4-
before_action :set_package, only: %i[index new edit]
5-
before_action :set_container, only: %i[index new edit]
3+
before_action :set_project, only: %i[index new edit create]
4+
before_action :set_package, only: %i[index new edit create]
5+
before_action :set_container, only: %i[index new edit create]
66
before_action :set_attribute, only: %i[update destroy]
77

88
# raise an exception if authorize has not yet been called.
@@ -23,7 +23,7 @@ def new
2323

2424
authorize @attribute, :create?
2525

26-
@attribute_types = AttribType.includes(:attrib_namespace).all.sort_by(&:fullname)
26+
@attribute_types = AttribType.includes(:attrib_namespace).where.not(id: @container.attribs.map(&:attrib_type_id)).sort_by(&:fullname)
2727
end
2828

2929
def edit
@@ -42,8 +42,7 @@ def edit
4242
end
4343

4444
def create
45-
@attribute = Attrib.new(attrib_params)
46-
45+
@attribute = @container.attribs.find_or_initialize_by(attrib_type_id: attrib_params[:attrib_type_id])
4746
authorize @attribute
4847

4948
# build the default values

src/api/app/views/webui/attribute/new.html.haml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
= form_for(@attribute) do |form|
99
.row
1010
= form.hidden_field(:project_id)
11+
= hidden_field_tag(:project_name, @project.name)
1112
- if @attribute.package_id
1213
= form.hidden_field(:package_id)
14+
= hidden_field_tag(:package_name, @package.name)
1315
.col-12.col-md-auto
1416
= form.collection_select(:attrib_type_id, @attribute_types, :id, :fullname, {}, class: 'form-control form-select')
1517
.col-form-label.col-12.col-md

0 commit comments

Comments
 (0)