Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added filters test and code

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@308 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
commit 5e2b551b73ce394edfdc3e48d0d10aa8894a2e27 1 parent edbcecf
jcarroll authored
View
34 lib/filters.rb
@@ -0,0 +1,34 @@
+module ActionControllerHook
+ module Filters
+
+ def self.included(klass)
+ class << klass
+
+ filters = %w(before_filter after_filter)
+
+ filters.each do |filter|
+ src = <<-END;
+ def #{filter}_with_conditions (*filters, &block)
+ options = filters.extract_options!
+ if block_given?
+ filters << block
+ end
+ filters.each do |filter|
+ #{filter}_without_conditions do |controller|
+ unless (! options[:if].nil? && ! ActiveRecord::Base.evaluate_condition(options[:if], controller)) ||
+ (! options[:unless].nil? && ActiveRecord::Base.evaluate_condition(options[:unless], controller))
+ controller.send filter
+ end
+ end
+ end
+ end
+ alias_method_chain :#{filter}, :conditions
+ END
+ class_eval src, __FILE__, __LINE__
+ end
+
+ end
+ end
+
+ end
+end
View
4 lib/when.rb
@@ -1,6 +1,8 @@
+require 'callbacks'
require 'validations'
require 'filters'
-require 'callbacks'
ActiveRecord::Base.send :include, ActiveRecordHook::Callbacks
ActiveRecord::Base.send :include, ActiveRecordHook::Validations
+
+ActionController::Base.send :include, ActionControllerHook::Filters
View
41 test/filters_test.rb
@@ -0,0 +1,41 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
+require File.join(File.dirname(__FILE__), 'fixtures', 'companies_controller')
+
+class FiltersTest < ActionController::TestCase
+
+ tests CompaniesController
+
+ def setup
+ @controller = CompaniesController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ ActionController::Routing::Routes.draw do |map|
+ map.connect 'companies', :controller => 'companies'
+ end
+ end
+
+ def test_before_filter_if
+ get :index, :q => 'before_filter_if'
+
+ assert_equal 'before_filter_if', @controller.attribute1
+ end
+
+ def test_before_filter_unless
+ get :index, :q => 'before_filter_unless'
+
+ assert_nil @controller.attribute2
+ end
+
+ def test_after_filter_if
+ get :index, :q => 'after_filter_if'
+
+ assert_equal 'after_filter_if', @controller.attribute3
+ end
+
+ def test_after_filter_unless
+ get :index, :q => 'after_filter_unless'
+
+ assert_nil @controller.attribute4
+ end
+
+end
View
59 test/fixtures/companies_controller.rb
@@ -0,0 +1,59 @@
+class CompaniesController < ActionController::Base
+
+ (1..4).each do |each|
+ attr_accessor :"attribute#{each}"
+ end
+
+ before_filter :before_filter_if,
+ :only => :index,
+ :if => :before_filter_if?
+
+ before_filter :before_filter_unless,
+ :only => :index,
+ :unless => :before_filter_unless?
+
+ after_filter :after_filter_if,
+ :only => :index,
+ :if => :after_filter_if?
+
+ after_filter :after_filter_unless,
+ :only => :index,
+ :unless => :after_filter_unless?
+
+ def index
+ render :nothing => true
+ end
+
+ def before_filter_if?
+ params[:q] == 'before_filter_if'
+ end
+
+ def before_filter_if
+ self.attribute1 = 'before_filter_if'
+ end
+
+ def before_filter_unless?
+ params[:q] == 'before_filter_unless'
+ end
+
+ def before_filter_unless
+ self.attribute2 = 'before_filter_unless'
+ end
+
+ def after_filter_if?
+ params[:q] == 'after_filter_if'
+ end
+
+ def after_filter_if
+ self.attribute3 = 'after_filter_if'
+ end
+
+ def after_filter_unless?
+ params[:q] == 'after_filter_unless'
+ end
+
+ def after_filter_unless
+ self.attribute4 = 'after_filter_unless'
+ end
+
+end
View
4 test/test_helper.rb
@@ -1,7 +1,9 @@
require 'rubygems'
require 'test/unit'
require 'active_record'
-require 'active_record/fixtures'
+require 'action_controller'
+require 'action_controller/test_case'
+require 'action_controller/test_process'
require File.join(File.dirname(__FILE__), '..', 'init')
config = YAML::load File.read(File.join(File.dirname(__FILE__), 'database.yml'))
Please sign in to comment.
Something went wrong with that request. Please try again.