Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug with server_error_callbacks being called from subclasses #532

Merged
merged 1 commit into from
Nov 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/jsonapi/acts_as_resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

module JSONAPI
module ActsAsResourceController
extend ActiveSupport::Concern

included do
before_action :ensure_correct_media_type, only: [:create, :update, :create_relationship, :update_relationship]
def self.included(base)
base.extend ClassMethods
base.before_action :ensure_correct_media_type, only: [:create, :update, :create_relationship, :update_relationship]
base.cattr_reader :server_error_callbacks
end

def index
Expand Down Expand Up @@ -191,13 +192,12 @@ def handle_exceptions(e)
# Ignores whitelist exceptions from config

module ClassMethods
attr_reader :server_error_callbacks

def on_server_error(*args, &callback_block)
@server_error_callbacks ||= []
callbacks ||= []

if callback_block
@server_error_callbacks << callback_block
callbacks << callback_block
end

method_callbacks = args.map do |method|
Expand All @@ -209,8 +209,10 @@ def on_server_error(*args, &callback_block)
end
end
end.compact
@server_error_callbacks += method_callbacks
callbacks += method_callbacks
self.class_variable_set :@@server_error_callbacks, callbacks
end

end
end
end
12 changes: 6 additions & 6 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def test_on_server_error_block_callback_with_exception
JSONAPI.configuration.exception_class_whitelist = []

@controller.class.instance_variable_set(:@callback_message, "none")
@controller.class.on_server_error do
BaseController.on_server_error do
@controller.class.instance_variable_set(:@callback_message, "Sent from block")
end

get :index
assert_equal @controller.class.instance_variable_get(:@callback_message), "Sent from block"

Expand All @@ -54,7 +54,7 @@ def test_on_server_error_block_callback_with_exception
def test_on_server_error_method_callback_with_exception
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.operations_processor = :error_raising
JSONAPI.configuration.exception_class_whitelist = []
JSONAPI.configuration.exception_class_whitelist = []

#ignores methods that don't exist
@controller.class.on_server_error :set_callback_message, :a_bogus_method
Expand All @@ -70,7 +70,7 @@ def test_on_server_error_method_callback_with_exception
end

def test_on_server_error_callback_without_exception

callback = Proc.new { @controller.class.instance_variable_set(:@callback_message, "Sent from block") }
@controller.class.on_server_error callback
@controller.class.instance_variable_set(:@callback_message, "none")
Expand Down Expand Up @@ -1222,7 +1222,7 @@ def test_delete_relationship_to_many_with_relationship_url_not_matching_type

#check the relationship was created successfully
assert_equal 1, Post.find(14).special_tags.count
before_tags = Post.find(14).tags.count
before_tags = Post.find(14).tags.count

delete :destroy_relationship, {post_id: 14, relationship: 'special_tags', data: [{type: 'tags', id: 2}]}
assert_equal 0, Post.find(14).special_tags.count, "Relationship that matches URL relationship not destroyed"
Expand Down Expand Up @@ -3235,4 +3235,4 @@ def test_immutable_update_not_supported
}
end
end
end
end
6 changes: 5 additions & 1 deletion test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,12 @@ class AuthorsController < JSONAPI::ResourceController
class PeopleController < JSONAPI::ResourceController
end

class PostsController < ActionController::Base
class BaseController < ActionController::Base
include JSONAPI::ActsAsResourceController
end

class PostsController < BaseController

class SpecialError < StandardError; end
class SubSpecialError < PostsController::SpecialError; end

Expand Down