From 549c4c2202307608773a10d8ce9a7dc978537de4 Mon Sep 17 00:00:00 2001 From: Alex Denisov <1101.debian@gmail.com> Date: Sun, 16 Sep 2012 19:51:04 +0300 Subject: [PATCH 1/2] API attributes refactored --- lib/api/helpers.rb | 8 ++++++++ lib/api/issues.rb | 24 ++++++------------------ lib/api/milestones.rb | 18 ++++-------------- lib/api/projects.rb | 38 ++++++++++++++++++-------------------- 4 files changed, 36 insertions(+), 52 deletions(-) diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 054eb2d3..d0fc8c43 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -28,6 +28,14 @@ module Gitlab end end + def existed_attributes(keys) + attrs = {} + keys.each do |key| + attrs[key] = params[key] if params[key].present? + end + attrs + end + # error helpers def forbidden! diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 659f065e..b177a4a8 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -48,15 +48,10 @@ module Gitlab # Example Request: # POST /projects/:id/issues post ":id/issues" do - @issue = user_project.issues.new( - title: params[:title], - description: params[:description], - assignee_id: params[:assignee_id], - milestone_id: params[:milestone_id], - label_list: params[:labels] - ) + attrs = existed_attributes [:title, :description, :assignee_id, :milestone_id] + attrs[:label_list] = params[:labels] if params[:labels].present? + @issue = user_project.issues.new attrs @issue.author = current_user - if @issue.save present @issue, with: Entities::Issue else @@ -81,16 +76,9 @@ module Gitlab @issue = user_project.issues.find(params[:issue_id]) authorize! :modify_issue, @issue - parameters = { - title: (params[:title] || @issue.title), - description: (params[:description] || @issue.description), - assignee_id: (params[:assignee_id] || @issue.assignee_id), - milestone_id: (params[:milestone_id] || @issue.milestone_id), - label_list: (params[:labels] || @issue.label_list), - closed: (params[:closed] || @issue.closed) - } - - if @issue.update_attributes(parameters) + attrs = existed_attributes [:title, :description, :assignee_id, :milestone_id, :closed] + attrs[:label_list] = params[:labels] if params[:labels].present? + if @issue.update_attributes attrs present @issue, with: Entities::Issue else not_found! diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb index 4b0424ba..d5d3347a 100644 --- a/lib/api/milestones.rb +++ b/lib/api/milestones.rb @@ -36,12 +36,8 @@ module Gitlab # Example Request: # POST /projects/:id/milestones post ":id/milestones" do - @milestone = user_project.milestones.new( - title: params[:title], - description: params[:description], - due_date: params[:due_date] - ) - + attrs = existed_attributes [:title, :description, :due_date] + @milestone = user_project.milestones.new attrs if @milestone.save present @milestone, with: Entities::Milestone else @@ -64,14 +60,8 @@ module Gitlab authorize! :admin_milestone, user_project @milestone = user_project.milestones.find(params[:milestone_id]) - parameters = { - title: (params[:title] || @milestone.title), - description: (params[:description] || @milestone.description), - due_date: (params[:due_date] || @milestone.due_date), - closed: (params[:closed] || @milestone.closed) - } - - if @milestone.update_attributes(parameters) + attrs = existed_attributes [:title, :description, :due_date, :closed] + if @milestone.update_attributes attrs present @milestone, with: Entities::Milestone else not_found! diff --git a/lib/api/projects.rb b/lib/api/projects.rb index cf23dc5f..4a263472 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -40,13 +40,16 @@ module Gitlab post do params[:code] ||= params[:name] params[:path] ||= params[:name] - project_attrs = {} - params.each_pair do |k ,v| - if Project.attribute_names.include? k - project_attrs[k] = v - end - end - @project = Project.create_by_user(project_attrs, current_user) + attrs = existed_attributes [:code, + :path, + :name, + :description, + :default_branch, + :issues_enabled, + :wall_enabled, + :merge_requests_enabled, + :wiki_enabled] + @project = Project.create_by_user(attrs, current_user) if @project.saved? present @project, with: Entities::Project else @@ -204,12 +207,10 @@ module Gitlab # Example Request: # POST /projects/:id/snippets post ":id/snippets" do - @snippet = user_project.snippets.new( - title: params[:title], - file_name: params[:file_name], - expires_at: params[:lifetime], - content: params[:code] - ) + attrs = existed_attributes [:title, :file_name] + attrs[:expires_at] = params[:lifetime] if params[:lifetime].present? + attrs[:content] = params[:code] if params[:code].present? + @snippet = user_project.snippets.new attrs @snippet.author = current_user if @snippet.save @@ -234,14 +235,11 @@ module Gitlab @snippet = user_project.snippets.find(params[:snippet_id]) authorize! :modify_snippet, @snippet - parameters = { - title: (params[:title] || @snippet.title), - file_name: (params[:file_name] || @snippet.file_name), - expires_at: (params[:lifetime] || @snippet.expires_at), - content: (params[:code] || @snippet.content) - } + attrs = existed_attributes [:title, :file_name] + attrs[:expires_at] = params[:lifetime] if params[:lifetime].present? + attrs[:content] = params[:code] if params[:code].present? - if @snippet.update_attributes(parameters) + if @snippet.update_attributes attrs present @snippet, with: Entities::ProjectSnippet else not_found! From b896880eb46c2b8167e2252d3656b5e38e35f6ec Mon Sep 17 00:00:00 2001 From: Alex Denisov <1101.debian@gmail.com> Date: Sun, 16 Sep 2012 20:08:57 +0300 Subject: [PATCH 2/2] Method name changed --- lib/api/helpers.rb | 2 +- lib/api/issues.rb | 4 ++-- lib/api/milestones.rb | 4 ++-- lib/api/projects.rb | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index d0fc8c43..9a08b995 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -28,7 +28,7 @@ module Gitlab end end - def existed_attributes(keys) + def attributes_for_keys(keys) attrs = {} keys.each do |key| attrs[key] = params[key] if params[key].present? diff --git a/lib/api/issues.rb b/lib/api/issues.rb index b177a4a8..4ee2d11f 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -48,7 +48,7 @@ module Gitlab # Example Request: # POST /projects/:id/issues post ":id/issues" do - attrs = existed_attributes [:title, :description, :assignee_id, :milestone_id] + attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id] attrs[:label_list] = params[:labels] if params[:labels].present? @issue = user_project.issues.new attrs @issue.author = current_user @@ -76,7 +76,7 @@ module Gitlab @issue = user_project.issues.find(params[:issue_id]) authorize! :modify_issue, @issue - attrs = existed_attributes [:title, :description, :assignee_id, :milestone_id, :closed] + attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id, :closed] attrs[:label_list] = params[:labels] if params[:labels].present? if @issue.update_attributes attrs present @issue, with: Entities::Issue diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb index d5d3347a..daaff940 100644 --- a/lib/api/milestones.rb +++ b/lib/api/milestones.rb @@ -36,7 +36,7 @@ module Gitlab # Example Request: # POST /projects/:id/milestones post ":id/milestones" do - attrs = existed_attributes [:title, :description, :due_date] + attrs = attributes_for_keys [:title, :description, :due_date] @milestone = user_project.milestones.new attrs if @milestone.save present @milestone, with: Entities::Milestone @@ -60,7 +60,7 @@ module Gitlab authorize! :admin_milestone, user_project @milestone = user_project.milestones.find(params[:milestone_id]) - attrs = existed_attributes [:title, :description, :due_date, :closed] + attrs = attributes_for_keys [:title, :description, :due_date, :closed] if @milestone.update_attributes attrs present @milestone, with: Entities::Milestone else diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 4a263472..1d9004f8 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -40,7 +40,7 @@ module Gitlab post do params[:code] ||= params[:name] params[:path] ||= params[:name] - attrs = existed_attributes [:code, + attrs = attributes_for_keys [:code, :path, :name, :description, @@ -207,7 +207,7 @@ module Gitlab # Example Request: # POST /projects/:id/snippets post ":id/snippets" do - attrs = existed_attributes [:title, :file_name] + attrs = attributes_for_keys [:title, :file_name] attrs[:expires_at] = params[:lifetime] if params[:lifetime].present? attrs[:content] = params[:code] if params[:code].present? @snippet = user_project.snippets.new attrs @@ -235,7 +235,7 @@ module Gitlab @snippet = user_project.snippets.find(params[:snippet_id]) authorize! :modify_snippet, @snippet - attrs = existed_attributes [:title, :file_name] + attrs = attributes_for_keys [:title, :file_name] attrs[:expires_at] = params[:lifetime] if params[:lifetime].present? attrs[:content] = params[:code] if params[:code].present?