more devise fixes and start of spec suite

This commit is contained in:
John Bintz 2011-05-27 09:41:35 -04:00
parent 0ea172d5e3
commit 47691f38be
4 changed files with 81 additions and 15 deletions

View File

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

View File

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

View File

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

11
spec/spec_helper.rb Normal file
View File

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