Commit eed1b52f18b5cced7d91d94df2c5d2f3e6b16149
Exists in
master
and in
4 other branches
Merge pull request #1480 from AlexDenisov/api_attrs_refactoring
API attributes refactoring
Showing
4 changed files
with
36 additions
and
52 deletions
Show diff stats
lib/api/helpers.rb
lib/api/issues.rb
| ... | ... | @@ -48,15 +48,10 @@ module Gitlab |
| 48 | 48 | # Example Request: |
| 49 | 49 | # POST /projects/:id/issues |
| 50 | 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 = attributes_for_keys [:title, :description, :assignee_id, :milestone_id] | |
| 52 | + attrs[:label_list] = params[:labels] if params[:labels].present? | |
| 53 | + @issue = user_project.issues.new attrs | |
| 58 | 54 | @issue.author = current_user |
| 59 | - | |
| 60 | 55 | if @issue.save |
| 61 | 56 | present @issue, with: Entities::Issue |
| 62 | 57 | else |
| ... | ... | @@ -81,16 +76,9 @@ module Gitlab |
| 81 | 76 | @issue = user_project.issues.find(params[:issue_id]) |
| 82 | 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 = attributes_for_keys [:title, :description, :assignee_id, :milestone_id, :closed] | |
| 80 | + attrs[:label_list] = params[:labels] if params[:labels].present? | |
| 81 | + if @issue.update_attributes attrs | |
| 94 | 82 | present @issue, with: Entities::Issue |
| 95 | 83 | else |
| 96 | 84 | not_found! | ... | ... |
lib/api/milestones.rb
| ... | ... | @@ -36,12 +36,8 @@ module Gitlab |
| 36 | 36 | # Example Request: |
| 37 | 37 | # POST /projects/:id/milestones |
| 38 | 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 = attributes_for_keys [:title, :description, :due_date] | |
| 40 | + @milestone = user_project.milestones.new attrs | |
| 45 | 41 | if @milestone.save |
| 46 | 42 | present @milestone, with: Entities::Milestone |
| 47 | 43 | else |
| ... | ... | @@ -64,14 +60,8 @@ module Gitlab |
| 64 | 60 | authorize! :admin_milestone, user_project |
| 65 | 61 | |
| 66 | 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 = attributes_for_keys [:title, :description, :due_date, :closed] | |
| 64 | + if @milestone.update_attributes attrs | |
| 75 | 65 | present @milestone, with: Entities::Milestone |
| 76 | 66 | else |
| 77 | 67 | not_found! | ... | ... |
lib/api/projects.rb
| ... | ... | @@ -40,13 +40,16 @@ module Gitlab |
| 40 | 40 | post do |
| 41 | 41 | params[:code] ||= params[:name] |
| 42 | 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 = attributes_for_keys [: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 | 53 | if @project.saved? |
| 51 | 54 | present @project, with: Entities::Project |
| 52 | 55 | else |
| ... | ... | @@ -204,12 +207,10 @@ module Gitlab |
| 204 | 207 | # Example Request: |
| 205 | 208 | # POST /projects/:id/snippets |
| 206 | 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 = attributes_for_keys [: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 | 214 | @snippet.author = current_user |
| 214 | 215 | |
| 215 | 216 | if @snippet.save |
| ... | ... | @@ -234,14 +235,11 @@ module Gitlab |
| 234 | 235 | @snippet = user_project.snippets.find(params[:snippet_id]) |
| 235 | 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 = attributes_for_keys [: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 | 243 | present @snippet, with: Entities::ProjectSnippet |
| 246 | 244 | else |
| 247 | 245 | not_found! | ... | ... |