Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
5de85708
Commit
5de85708
authored
Jan 23, 2018
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use nested attributes for updating multiple variables
parent
ba077841
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
9 additions
and
26 deletions
+9
-26
app/controllers/projects/variables_controller.rb
app/controllers/projects/variables_controller.rb
+3
-21
spec/controllers/projects/variables_controller_spec.rb
spec/controllers/projects/variables_controller_spec.rb
+6
-5
No files found.
app/controllers/projects/variables_controller.rb
View file @
5de85708
class
Projects::VariablesController
<
Projects
::
ApplicationController
before_action
:variable
,
only:
[
:show
,
:update
,
:destroy
]
before_action
:variables
,
only:
[
:save_multiple
]
before_action
:authorize_admin_build!
layout
'project_settings'
...
...
@@ -36,11 +35,9 @@ class Projects::VariablesController < Projects::ApplicationController
def
save_multiple
respond_to
do
|
format
|
format
.
json
do
return
head
:
bad_request
unless
@variables
.
all?
(
&
:valid?
)
return
head
:
ok
if
@project
.
update
(
variables_params
)
@variables
.
each
(
&
:save
)
@variables_destroy
.
each
(
&
:destroy
)
head
:ok
head
:bad_request
end
end
end
...
...
@@ -64,7 +61,7 @@ class Projects::VariablesController < Projects::ApplicationController
end
def
variables_params
params
.
permit
(
variables:
[
*
variable_params_attributes
])
params
.
permit
(
variables
_attributes
:
[
*
variable_params_attributes
])
end
def
variable_params_attributes
...
...
@@ -74,19 +71,4 @@ class Projects::VariablesController < Projects::ApplicationController
def
variable
@variable
||=
project
.
variables
.
find
(
params
[
:id
]).
present
(
current_user:
current_user
)
end
def
variables
destroy
,
edit
=
variables_params
[
:variables
].
partition
{
|
hash
|
hash
[
:_destroy
]
==
'true'
}
@variables
=
initialize_or_update_variables_from_hash
(
edit
)
@variables_destroy
=
initialize_or_update_variables_from_hash
(
destroy
)
end
def
initialize_or_update_variables_from_hash
(
hash
)
hash
.
map
do
|
variable_hash
|
variable
=
project
.
variables
.
where
(
key:
variable_hash
[
:key
])
.
first_or_initialize
(
variable_hash
).
present
(
current_user:
current_user
)
variable
.
assign_attributes
(
variable_hash
.
except
(
:_destroy
))
unless
variable
.
new_record?
variable
end
end
end
spec/controllers/projects/variables_controller_spec.rb
View file @
5de85708
...
...
@@ -67,8 +67,8 @@ describe Projects::VariablesController do
subject
do
post
:save_multiple
,
namespace_id:
project
.
namespace
.
to_param
,
project_id:
project
,
variables
:
[{
key:
variable
.
key
,
value:
'other_value'
},
{
key:
'..?'
,
value:
'dummy_value'
}],
variables
_attributes:
[{
id:
variable
.
id
,
key:
variable
.
key
,
value:
'other_value'
},
{
key:
'..?'
,
value:
'dummy_value'
}],
format: :json
end
...
...
@@ -91,8 +91,8 @@ describe Projects::VariablesController do
subject
do
post
:save_multiple
,
namespace_id:
project
.
namespace
.
to_param
,
project_id:
project
,
variables
:
[{
key:
variable
.
key
,
value:
'other_value'
},
{
key:
'new_key'
,
value:
'dummy_value'
}],
variables
_attributes:
[{
id:
variable
.
id
,
key:
variable
.
key
,
value:
'other_value'
},
{
key:
'new_key'
,
value:
'dummy_value'
}],
format: :json
end
...
...
@@ -115,7 +115,8 @@ describe Projects::VariablesController do
subject
do
post
:save_multiple
,
namespace_id:
project
.
namespace
.
to_param
,
project_id:
project
,
variables:
[{
key:
variable
.
key
,
value:
variable
.
value
,
_destroy:
'true'
}],
variables_attributes:
[{
id:
variable
.
id
,
key:
variable
.
key
,
value:
variable
.
value
,
_destroy:
'true'
}],
format: :json
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment