From 6b3bf41c7ac421af98ed619d958e84d44e7a93c0 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 11 Oct 2012 19:36:14 +0400 Subject: [PATCH] #671: removed before_filter --- app/controllers/api/v1/base_controller.rb | 12 ++++---- .../api/v1/platforms_controller.rb | 29 ++++++++----------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index c99ce0692..6b4baa981 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -17,23 +17,23 @@ class Api::V1::BaseController < ApplicationController {:page => params[:page], :per_page => per_page} end - def render_json_response(message, status = 200) - id = status != 200 ? nil : @subject.id + def render_json_response(subject, message, status = 200) + id = status != 200 ? nil : subject.id render :json => { - @subject.class.name.downcase.to_sym => { + subject.class.name.downcase.to_sym => { :id => id, :message => message } }.to_json, :status => status end - def render_validation_error(message) - errors = @subject.errors.full_messages.join('. ') + def render_validation_error(subject, message) + errors = subject.errors.full_messages.join('. ') if errors.present? message << '. ' << errors end - render_json_response(message, 422) + render_json_response(subject, message, 422) end end diff --git a/app/controllers/api/v1/platforms_controller.rb b/app/controllers/api/v1/platforms_controller.rb index c1ad76ece..ff37d009a 100644 --- a/app/controllers/api/v1/platforms_controller.rb +++ b/app/controllers/api/v1/platforms_controller.rb @@ -4,7 +4,6 @@ class Api::V1::PlatformsController < Api::V1::BaseController skip_before_filter :authenticate_user!, :only => [:show, :platforms_for_build, :members] if APP_CONFIG['anonymous_access'] load_and_authorize_resource - before_filter :set_subject, :except => [:index, :platforms_for_build, :show, :members] def index @platforms = @platforms.accessible_by(current_ability, :related). @@ -24,9 +23,9 @@ class Api::V1::PlatformsController < Api::V1::BaseController owner = User.where(:id => platform_params[:owner_id]).first @platform.owner = owner || get_owner if @platform.save - render_json_response 'Platform has been created successfully' + render_json_response @platform, 'Platform has been created successfully' else - render_validation_error 'Platform has not been created' + render_validation_error @platform, 'Platform has not been created' end end @@ -35,9 +34,9 @@ class Api::V1::PlatformsController < Api::V1::BaseController 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 has been updated successfully' + render_json_response @platform, 'Platform has been updated successfully' else - render_validation_error 'Platform has not been updated' + render_validation_error @platform, 'Platform has not been updated' end end @@ -47,17 +46,17 @@ class Api::V1::PlatformsController < Api::V1::BaseController def add_member if member.present? && @platform.add_member(member) - render_json_response "#{member.class.to_s} '#{member.id}' has been added to platform successfully" + render_json_response @platform, "#{member.class.to_s} '#{member.id}' has been added to platform successfully" else - render_validation_error 'Member has not been added to platform' + render_validation_error @platform, 'Member has not been added to platform' end end def remove_member if member.present? && @platform.remove_member(member) - render_json_response "#{member.class.to_s} '#{member.id}' has been removed from platform successfully" + render_json_response @platform, "#{member.class.to_s} '#{member.id}' has been removed from platform successfully" else - render_validation_error 'Member has not been removed from platform' + render_validation_error @platform, 'Member has not been removed from platform' end end @@ -66,20 +65,20 @@ class Api::V1::PlatformsController < Api::V1::BaseController platform_params[:owner] = current_user @cloned = @platform.full_clone(platform_params) if @cloned.persisted? - render_json_response 'Platform has been cloned successfully' + render_json_response @platform, 'Platform has been cloned successfully' else - render_validation_error 'Platform has not been cloned' + render_validation_error @platform, 'Platform has not been cloned' end end def clear @platform.clear - render_json_response 'Platform has been cleared successfully' + render_json_response @platform, 'Platform has been cleared successfully' end def destroy @platform.destroy # later with resque - render_json_response 'Platform has been destroyed successfully' + render_json_response @platform, 'Platform has been destroyed successfully' end private @@ -95,8 +94,4 @@ class Api::V1::PlatformsController < Api::V1::BaseController @member ||= '' end - def set_subject - @subject = @platform - end - end