Commit 549c4c2202307608773a10d8ce9a7dc978537de4
1 parent
caef9ed1
Exists in
master
and in
4 other branches
API attributes refactored
Showing
4 changed files
with
36 additions
and
52 deletions
Show diff stats
lib/api/helpers.rb
@@ -28,6 +28,14 @@ module Gitlab | @@ -28,6 +28,14 @@ module Gitlab | ||
28 | end | 28 | end |
29 | end | 29 | end |
30 | 30 | ||
31 | + def existed_attributes(keys) | ||
32 | + attrs = {} | ||
33 | + keys.each do |key| | ||
34 | + attrs[key] = params[key] if params[key].present? | ||
35 | + end | ||
36 | + attrs | ||
37 | + end | ||
38 | + | ||
31 | # error helpers | 39 | # error helpers |
32 | 40 | ||
33 | def forbidden! | 41 | def forbidden! |
lib/api/issues.rb
@@ -48,15 +48,10 @@ module Gitlab | @@ -48,15 +48,10 @@ module Gitlab | ||
48 | # Example Request: | 48 | # Example Request: |
49 | # POST /projects/:id/issues | 49 | # POST /projects/:id/issues |
50 | post ":id/issues" do | 50 | post ":id/issues" do |
51 | - @issue = user_project.issues.new( | ||
52 | - title: params[:title], | ||
53 | - description: params[:description], | ||
54 | - assignee_id: params[:assignee_id], | ||
55 | - milestone_id: params[:milestone_id], | ||
56 | - label_list: params[:labels] | ||
57 | - ) | 51 | + attrs = existed_attributes [:title, :description, :assignee_id, :milestone_id] |
52 | + attrs[:label_list] = params[:labels] if params[:labels].present? | ||
53 | + @issue = user_project.issues.new attrs | ||
58 | @issue.author = current_user | 54 | @issue.author = current_user |
59 | - | ||
60 | if @issue.save | 55 | if @issue.save |
61 | present @issue, with: Entities::Issue | 56 | present @issue, with: Entities::Issue |
62 | else | 57 | else |
@@ -81,16 +76,9 @@ module Gitlab | @@ -81,16 +76,9 @@ module Gitlab | ||
81 | @issue = user_project.issues.find(params[:issue_id]) | 76 | @issue = user_project.issues.find(params[:issue_id]) |
82 | authorize! :modify_issue, @issue | 77 | authorize! :modify_issue, @issue |
83 | 78 | ||
84 | - parameters = { | ||
85 | - title: (params[:title] || @issue.title), | ||
86 | - description: (params[:description] || @issue.description), | ||
87 | - assignee_id: (params[:assignee_id] || @issue.assignee_id), | ||
88 | - milestone_id: (params[:milestone_id] || @issue.milestone_id), | ||
89 | - label_list: (params[:labels] || @issue.label_list), | ||
90 | - closed: (params[:closed] || @issue.closed) | ||
91 | - } | ||
92 | - | ||
93 | - if @issue.update_attributes(parameters) | 79 | + attrs = existed_attributes [:title, :description, :assignee_id, :milestone_id, :closed] |
80 | + attrs[:label_list] = params[:labels] if params[:labels].present? | ||
81 | + if @issue.update_attributes attrs | ||
94 | present @issue, with: Entities::Issue | 82 | present @issue, with: Entities::Issue |
95 | else | 83 | else |
96 | not_found! | 84 | not_found! |
lib/api/milestones.rb
@@ -36,12 +36,8 @@ module Gitlab | @@ -36,12 +36,8 @@ module Gitlab | ||
36 | # Example Request: | 36 | # Example Request: |
37 | # POST /projects/:id/milestones | 37 | # POST /projects/:id/milestones |
38 | post ":id/milestones" do | 38 | post ":id/milestones" do |
39 | - @milestone = user_project.milestones.new( | ||
40 | - title: params[:title], | ||
41 | - description: params[:description], | ||
42 | - due_date: params[:due_date] | ||
43 | - ) | ||
44 | - | 39 | + attrs = existed_attributes [:title, :description, :due_date] |
40 | + @milestone = user_project.milestones.new attrs | ||
45 | if @milestone.save | 41 | if @milestone.save |
46 | present @milestone, with: Entities::Milestone | 42 | present @milestone, with: Entities::Milestone |
47 | else | 43 | else |
@@ -64,14 +60,8 @@ module Gitlab | @@ -64,14 +60,8 @@ module Gitlab | ||
64 | authorize! :admin_milestone, user_project | 60 | authorize! :admin_milestone, user_project |
65 | 61 | ||
66 | @milestone = user_project.milestones.find(params[:milestone_id]) | 62 | @milestone = user_project.milestones.find(params[:milestone_id]) |
67 | - parameters = { | ||
68 | - title: (params[:title] || @milestone.title), | ||
69 | - description: (params[:description] || @milestone.description), | ||
70 | - due_date: (params[:due_date] || @milestone.due_date), | ||
71 | - closed: (params[:closed] || @milestone.closed) | ||
72 | - } | ||
73 | - | ||
74 | - if @milestone.update_attributes(parameters) | 63 | + attrs = existed_attributes [:title, :description, :due_date, :closed] |
64 | + if @milestone.update_attributes attrs | ||
75 | present @milestone, with: Entities::Milestone | 65 | present @milestone, with: Entities::Milestone |
76 | else | 66 | else |
77 | not_found! | 67 | not_found! |
lib/api/projects.rb
@@ -40,13 +40,16 @@ module Gitlab | @@ -40,13 +40,16 @@ module Gitlab | ||
40 | post do | 40 | post do |
41 | params[:code] ||= params[:name] | 41 | params[:code] ||= params[:name] |
42 | params[:path] ||= params[:name] | 42 | params[:path] ||= params[:name] |
43 | - project_attrs = {} | ||
44 | - params.each_pair do |k ,v| | ||
45 | - if Project.attribute_names.include? k | ||
46 | - project_attrs[k] = v | ||
47 | - end | ||
48 | - end | ||
49 | - @project = Project.create_by_user(project_attrs, current_user) | 43 | + attrs = existed_attributes [:code, |
44 | + :path, | ||
45 | + :name, | ||
46 | + :description, | ||
47 | + :default_branch, | ||
48 | + :issues_enabled, | ||
49 | + :wall_enabled, | ||
50 | + :merge_requests_enabled, | ||
51 | + :wiki_enabled] | ||
52 | + @project = Project.create_by_user(attrs, current_user) | ||
50 | if @project.saved? | 53 | if @project.saved? |
51 | present @project, with: Entities::Project | 54 | present @project, with: Entities::Project |
52 | else | 55 | else |
@@ -204,12 +207,10 @@ module Gitlab | @@ -204,12 +207,10 @@ module Gitlab | ||
204 | # Example Request: | 207 | # Example Request: |
205 | # POST /projects/:id/snippets | 208 | # POST /projects/:id/snippets |
206 | post ":id/snippets" do | 209 | post ":id/snippets" do |
207 | - @snippet = user_project.snippets.new( | ||
208 | - title: params[:title], | ||
209 | - file_name: params[:file_name], | ||
210 | - expires_at: params[:lifetime], | ||
211 | - content: params[:code] | ||
212 | - ) | 210 | + attrs = existed_attributes [:title, :file_name] |
211 | + attrs[:expires_at] = params[:lifetime] if params[:lifetime].present? | ||
212 | + attrs[:content] = params[:code] if params[:code].present? | ||
213 | + @snippet = user_project.snippets.new attrs | ||
213 | @snippet.author = current_user | 214 | @snippet.author = current_user |
214 | 215 | ||
215 | if @snippet.save | 216 | if @snippet.save |
@@ -234,14 +235,11 @@ module Gitlab | @@ -234,14 +235,11 @@ module Gitlab | ||
234 | @snippet = user_project.snippets.find(params[:snippet_id]) | 235 | @snippet = user_project.snippets.find(params[:snippet_id]) |
235 | authorize! :modify_snippet, @snippet | 236 | authorize! :modify_snippet, @snippet |
236 | 237 | ||
237 | - parameters = { | ||
238 | - title: (params[:title] || @snippet.title), | ||
239 | - file_name: (params[:file_name] || @snippet.file_name), | ||
240 | - expires_at: (params[:lifetime] || @snippet.expires_at), | ||
241 | - content: (params[:code] || @snippet.content) | ||
242 | - } | 238 | + attrs = existed_attributes [:title, :file_name] |
239 | + attrs[:expires_at] = params[:lifetime] if params[:lifetime].present? | ||
240 | + attrs[:content] = params[:code] if params[:code].present? | ||
243 | 241 | ||
244 | - if @snippet.update_attributes(parameters) | 242 | + if @snippet.update_attributes attrs |
245 | present @snippet, with: Entities::ProjectSnippet | 243 | present @snippet, with: Entities::ProjectSnippet |
246 | else | 244 | else |
247 | not_found! | 245 | not_found! |