From 146f72c06202d4f24691a6866f029b0e8db1c85d Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Sun, 14 Oct 2012 16:39:58 +0400 Subject: [PATCH] #672: small refactoring --- .../api/v1/platforms_controller.rb | 21 +++--------- .../api/v1/repositories_controller.rb | 22 +++--------- app/helpers/api/v1/base_helper.rb | 34 +++++++++++++++++++ 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index 19efa1611..007237092 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -33,11 +33,7 @@ class Api::V1::PlatformsController < Api::V1::BaseController platform_params = params[:platform] || {} owner = User.where(:id => platform_params[:owner_id]).first platform_params[:owner] = owner if owner - if @platform.update_attributes(platform_params) - render_json_response @platform, 'Platform has been updated successfully' - else - render_validation_error @platform, 'Platform has not been updated' - end + update_subject @platform end def members @@ -45,19 +41,11 @@ class Api::V1::PlatformsController < Api::V1::BaseController end def add_member - if member.present? && @platform.add_member(member) - render_json_response @platform, "#{member.class.to_s} '#{member.id}' has been added to platform successfully" - else - render_validation_error @platform, 'Member has not been added to platform' - end + add_member_to_subject @platform end def remove_member - if member.present? && @platform.remove_member(member) - render_json_response @platform, "#{member.class.to_s} '#{member.id}' has been removed from platform successfully" - else - render_validation_error @platform, 'Member has not been removed from platform' - end + remove_member_from_subject @platform end def clone @@ -77,8 +65,7 @@ class Api::V1::PlatformsController < Api::V1::BaseController end def destroy - @platform.destroy # later with resque - render_json_response @platform, 'Platform has been destroyed successfully' + destroy_subject @platform end end diff --git a/app/controllers/api/v1/repositories_controller.rb b/app/controllers/api/v1/repositories_controller.rb index 5b53ba3a4..fa9fa6046 100644 --- a/app/controllers/api/v1/repositories_controller.rb +++ b/app/controllers/api/v1/repositories_controller.rb @@ -10,33 +10,19 @@ class Api::V1::RepositoriesController < Api::V1::BaseController end def update - rep_params = params[:repository] || {} - if @repository.update_attributes(rep_params) - render_json_response @repository, 'Repository has been updated successfully' - else - render_validation_error @repository, 'Repository has not been updated' - end + update_subject @repository end def add_member - if member.present? && @repository.add_member(member) - render_json_response @repository, "#{member.class.to_s} '#{member.id}' has been added to repository successfully" - else - render_validation_error @repository, 'Member has not been added to repository' - end + add_member_to_subject @repository end def remove_member - if member.present? && @repository.remove_member(member) - render_json_response @repository, "#{member.class.to_s} '#{member.id}' has been removed from repository successfully" - else - render_validation_error @repository, 'Member has not been removed from repository' - end + remove_member_from_subject @repository end def destroy - @repository.destroy # later with resque - render_json_response @repository, 'Repository has been destroyed successfully' + destroy_subject @repository end end diff --git a/app/helpers/api/v1/base_helper.rb b/app/helpers/api/v1/base_helper.rb index 969a3760c..0599169b8 100644 --- a/app/helpers/api/v1/base_helper.rb +++ b/app/helpers/api/v1/base_helper.rb @@ -2,6 +2,38 @@ module Api::V1::BaseHelper protected + def add_member_to_subject(subject) + class_name = subject.class.name.downcase + if member.present? && subject.add_member(member) + render_json_response subject, "#{member.class.to_s} '#{member.id}' has been added to #{class_name} successfully" + else + render_validation_error subject, "Member has not been added to #{class_name}" + end + end + + def remove_member_from_subject(subject) + class_name = subject.class.name.downcase + if member.present? && subject.remove_member(member) + render_json_response subject, "#{member.class.to_s} '#{member.id}' has been removed from #{class_name} successfully" + else + render_validation_error subject, "Member has not been removed from #{class_name}" + end + end + + def destroy_subject(subject) + subject.destroy # later with resque + render_json_response subject, "#{subject.class.name} has been destroyed successfully" + end + + def update_subject(subject) + class_name = subject.class.name + if subject.update_attributes(params[class_name.downcase.to_sym] || {}) + render_json_response subject, "#{class_name} has been updated successfully" + else + render_validation_error subject, "#{class_name} has not been updated" + end + end + def paginate_params per_page = params[:per_page].to_i per_page = 20 if per_page < 1 @@ -28,6 +60,8 @@ module Api::V1::BaseHelper render_json_response(subject, message, 422) end + private + def member return @member if @member if params[:type] == 'User'