#672: small refactoring

This commit is contained in:
Vokhmin Alexey V 2012-10-14 16:39:58 +04:00
parent be8ed0984a
commit 146f72c062
3 changed files with 42 additions and 35 deletions

View File

@ -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

View File

@ -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

View File

@ -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'