diff --git a/Gemfile b/Gemfile index c010d3d..93191f6 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,7 @@ source "http://rubygems.org" # Specify your gem's dependencies in controller_filter_logging.gemspec gemspec + +gem 'rspec' +gem 'mocha' +gem 'rails' diff --git a/lib/controller_filter_logging.rb b/lib/controller_filter_logging.rb index 0beab4c..c06615a 100644 --- a/lib/controller_filter_logging.rb +++ b/lib/controller_filter_logging.rb @@ -1,12 +1,8 @@ +require 'abstract_controller' + module AbstractController::Callbacks::ClassMethods def before_filter_with_logging(*args, &block) - if block_given? - Rails.logger.debug("Can't log filters with blocks: #{caller[0..3].join("\n")}") - before_filter_without_logging *args, &block - else - filter_name = args.shift - before_filter_without_logging create_logging_filter(filter_name), *args - end + handle_filter(:before_filter, *args, &block) end alias_method_chain :before_filter, :logging @@ -15,17 +11,23 @@ module AbstractController::Callbacks::ClassMethods end alias_method_chain :skip_before_filter, :logging - def prepend_before_filter_with_logging(filter_name, *args, &block) - if block_given? - Rails.logger.debug("Can't log filters with blocks: #{caller[0..3].join("\n")}") - prepend_before_filter_without_logging filter_name, *args, &block - else - create_logging_filter(filter_name) - prepend_before_filter_without_logging("#{filter_name}_with_logging".to_sym, *args) - end + def prepend_before_filter_with_logging(*args, &block) + handle_filter(:prepend_before_filter, *args, &block) end alias_method_chain :prepend_before_filter, :logging + private + def handle_filter(type, *args, &block) + method = "#{type}_without_logging" + if block_given? + Rails.logger.debug("Can't log filters with blocks: #{caller[0..3].join("\n")}") + send(method, *args, &block) + else + filter_name = args.shift + send(method, create_logging_filter(filter_name), *args) + end + end + def create_logging_filter(filter_name) name = "#{filter_name.to_s.gsub(%r{[?!]}, '')}_with_logging" define_method(name) do @@ -40,4 +42,5 @@ module AbstractController::Callbacks::ClassMethods end name.to_sym end + end diff --git a/spec/lib/controller_filter_logging_spec.rb b/spec/lib/controller_filter_logging_spec.rb new file mode 100644 index 0000000..acfa1fb --- /dev/null +++ b/spec/lib/controller_filter_logging_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' +require 'action_controller' + +require 'controller_filter_logging' + +describe 'controller_filter_logging' do + describe '#before_filter_with_logging' do + context 'with block' do + it "should warn and pass through to the block" do + Rails.logger.expects(:debug).once + + controller = Class.new(ActionController::Base) do + attr_reader :called + + before_filter do + @called = true + end + end.new + + controller.run_callbacks(:process_action) + controller.called.should be_true + end + + it "should run the block and log the results" do + result = 12345 + + Rails.logger.expects(:debug).with("Entering before_filter: test") + Rails.logger.expects(:debug).with(" result: #{result}") + + controller = Class.new(ActionController::Base) do + attr_reader :called + + before_filter :test + + private + def test + @called = 12345 + end + end.new + + controller.run_callbacks(:process_action) + controller.called.should == result + controller.methods.should include(:test_with_logging) + end + end + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..cd9ab95 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,11 @@ +require 'rails' + +RSpec.configure do |c| + c.mock_with :mocha + + c.before do + @logs = StringIO.new + Rails.logger = (@logger = Logger.new(@logs)) + end +end +