Skip to content

Commit 7180eaa

Browse files
authored
Merge pull request #18859 from rubhanazeem/attributes-rewrite
Check for existing attributes before creating new ones
2 parents 1b7880d + 958de54 commit 7180eaa

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
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

src/api/spec/controllers/webui/attribute_controller_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107

108108
context 'with editable values' do
109109
before do
110-
post :create, params: { attrib: { project_id: user.home_project.id, attrib_type_id: attribute_type1.id } }
110+
post :create, params: { attrib: { project_id: user.home_project.id, attrib_type_id: attribute_type1.id }, project_name: user.home_project.name }
111111
end
112112

113113
it { expect(response).to redirect_to(edit_attribs_path(project: user.home_project_name, package: '', attribute: attribute_type1_name)) }
@@ -116,7 +116,7 @@
116116

117117
context 'with non editable values' do
118118
before do
119-
post :create, params: { attrib: { project_id: user.home_project.id, attrib_type_id: attribute_type0.id } }
119+
post :create, params: { attrib: { project_id: user.home_project.id, attrib_type_id: attribute_type0.id }, project_name: user.home_project.name }
120120
end
121121

122122
it { expect(response).to redirect_to(index_attribs_path(project: user.home_project_name, package: '')) }
@@ -126,7 +126,7 @@
126126
context 'fails at save' do
127127
before do
128128
allow_any_instance_of(Attrib).to receive(:save).and_return(false)
129-
post :create, params: { attrib: { project_id: user.home_project.id, attrib_type_id: attribute_type1.id } }
129+
post :create, params: { attrib: { project_id: user.home_project.id, attrib_type_id: attribute_type1.id }, project_name: user.home_project.name }
130130
end
131131

132132
it { expect(response).to redirect_to(root_path) }

0 commit comments

Comments
 (0)