Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added filter tests for #prepend_<filter> and #append_<filter>

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@315 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
commit f53f323abdcf997d0ed2c2e7896285cf642d03e1 1 parent b9547f1
jcarroll authored
21 lib/filters.rb
View
@@ -4,7 +4,8 @@ module Filters
def self.included(klass)
class << klass
- filters = %w(before_filter after_filter)
+ filters = %w(append_before_filter prepend_before_filter before_filter
+ append_after_filter prepend_after_filter after_filter)
filters.each do |filter|
src = <<-END;
@@ -27,6 +28,24 @@ def #{filter}_with_conditions (*filters, &block)
class_eval src, __FILE__, __LINE__
end
+ def skip_filter_with_conditions(*filters, &block)
+ options = filters.extract_options!
+ if block_given?
+ filters << block
+ end
+ filters.each do |filter|
+ skip_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))
+raise
+ skip_filter_in_chain filter
+ end
+ end
+ end
+ end
+ alias_method_chain :skip_filter, :conditions
+
+
end
end
54 test/filters_test.rb
View
@@ -18,6 +18,7 @@ def test_before_filter_if
get :index, :q => 'before_filter_if'
assert_equal 'before_filter_if', @controller.attribute1
+ assert_equal 'before_filter_unless', @controller.attribute2
end
def test_before_filter_unless
@@ -30,6 +31,7 @@ def test_after_filter_if
get :index, :q => 'after_filter_if'
assert_equal 'after_filter_if', @controller.attribute3
+ assert_equal 'after_filter_unless', @controller.attribute4
end
def test_after_filter_unless
@@ -38,4 +40,56 @@ def test_after_filter_unless
assert_nil @controller.attribute4
end
+ def test_append_before_filter_if
+ get :index, :q => 'append_before_filter_if'
+
+ assert_equal 'append_before_filter_if', @controller.attribute5
+ assert_equal 'append_before_filter_unless', @controller.attribute6
+ end
+
+ def test_append_before_filter_unless
+ get :index, :q => 'append_before_filter_unless'
+
+ assert_nil @controller.attribute6
+ end
+
+ def test_append_after_filter_if
+ get :index, :q => 'append_after_filter_if'
+
+ assert_equal 'append_after_filter_if', @controller.attribute7
+ assert_equal 'append_after_filter_unless', @controller.attribute8
+ end
+
+ def test_append_after_filter_unless
+ get :index, :q => 'append_after_filter_unless'
+
+ assert_nil @controller.attribute8
+ end
+
+ def test_prepend_before_filter_if
+ get :index, :q => 'prepend_before_filter_if'
+
+ assert_equal 'prepend_before_filter_if', @controller.attribute9
+ assert_equal 'prepend_before_filter_unless', @controller.attribute10
+ end
+
+ def test_prepend_before_filter_unless
+ get :index, :q => 'prepend_before_filter_unless'
+
+ assert_nil @controller.attribute10
+ end
+
+ def test_prepend_after_filter_if
+ get :index, :q => 'prepend_after_filter_if'
+
+ assert_equal 'prepend_after_filter_if', @controller.attribute11
+ assert_equal 'prepend_after_filter_unless', @controller.attribute12
+ end
+
+ def test_prepend_after_filter_unless
+ get :index, :q => 'prepend_after_filter_unless'
+
+ assert_nil @controller.attribute12
+ end
+
end
98 test/fixtures/companies_controller.rb
View
@@ -1,6 +1,6 @@
class CompaniesController < ActionController::Base
- (1..4).each do |each|
+ (1..14).each do |each|
attr_accessor :"attribute#{each}"
end
@@ -20,6 +20,38 @@ class CompaniesController < ActionController::Base
:only => :index,
:unless => :after_filter_unless?
+ append_before_filter :append_before_filter_if,
+ :only => :index,
+ :if => :append_before_filter_if?
+
+ append_before_filter :append_before_filter_unless,
+ :only => :index,
+ :unless => :append_before_filter_unless?
+
+ append_after_filter :append_after_filter_if,
+ :only => :index,
+ :if => :append_after_filter_if?
+
+ append_after_filter :append_after_filter_unless,
+ :only => :index,
+ :unless => :append_after_filter_unless?
+
+ prepend_before_filter :prepend_before_filter_if,
+ :only => :index,
+ :if => :prepend_before_filter_if?
+
+ prepend_before_filter :prepend_before_filter_unless,
+ :only => :index,
+ :unless => :prepend_before_filter_unless?
+
+ prepend_after_filter :prepend_after_filter_if,
+ :only => :index,
+ :if => :prepend_after_filter_if?
+
+ prepend_after_filter :prepend_after_filter_unless,
+ :only => :index,
+ :unless => :prepend_after_filter_unless?
+
def index
render :nothing => true
end
@@ -56,4 +88,68 @@ def after_filter_unless
self.attribute4 = 'after_filter_unless'
end
+ def append_before_filter_if?
+ params[:q] == 'append_before_filter_if'
+ end
+
+ def append_before_filter_if
+ self.attribute5 = 'append_before_filter_if'
+ end
+
+ def append_before_filter_unless?
+ params[:q] == 'append_before_filter_unless'
+ end
+
+ def append_before_filter_unless
+ self.attribute6 = 'append_before_filter_unless'
+ end
+
+ def append_after_filter_if?
+ params[:q] == 'append_after_filter_if'
+ end
+
+ def append_after_filter_if
+ self.attribute7 = 'append_after_filter_if'
+ end
+
+ def append_after_filter_unless?
+ params[:q] == 'append_after_filter_unless'
+ end
+
+ def append_after_filter_unless
+ self.attribute8 = 'append_after_filter_unless'
+ end
+
+ def prepend_before_filter_if?
+ params[:q] == 'prepend_before_filter_if'
+ end
+
+ def prepend_before_filter_if
+ self.attribute9 = 'prepend_before_filter_if'
+ end
+
+ def prepend_before_filter_unless?
+ params[:q] == 'prepend_before_filter_unless'
+ end
+
+ def prepend_before_filter_unless
+ self.attribute10 = 'prepend_before_filter_unless'
+ end
+
+ def prepend_after_filter_if?
+ params[:q] == 'prepend_after_filter_if'
+ end
+
+ def prepend_after_filter_if
+ self.attribute11 = 'prepend_after_filter_if'
+ end
+
+ def prepend_after_filter_unless?
+ params[:q] == 'prepend_after_filter_unless'
+ end
+
+ def prepend_after_filter_unless
+ self.attribute12 = 'prepend_after_filter_unless'
+ end
+
end
4 test/fixtures/company.rb
View
@@ -5,11 +5,11 @@ class Company < ActiveRecord::Base
protected
def change_name
- self.name = 'new name' unless self.frozen?
+ self.name = 'new name' unless frozen?
end
def flag?
- self.callback_flag == true
+ callback_flag == true
end
# (1..114).each do |each|
4 test/when_test.rb
View
@@ -3,6 +3,10 @@
class WhenTest < Test::Unit::TestCase
+ def test_truth
+ assert true
+ end
+
# def test_should_set_its_attribute_1_if_its_before_create_if_with_proc_condition_when_sent_save_on_initial_creation
# company = Company.new :attribute_1 => 'before_create_if_with_proc_condition'
#
Please sign in to comment.
Something went wrong with that request. Please try again.