Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Commit

Permalink
added filters test and code
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@308 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information
jcarroll committed Feb 12, 2008
1 parent edbcecf commit 5e2b551
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
34 changes: 34 additions & 0 deletions 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
4 changes: 3 additions & 1 deletion 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
41 changes: 41 additions & 0 deletions 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
59 changes: 59 additions & 0 deletions 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
4 changes: 3 additions & 1 deletion 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'))
Expand Down

0 comments on commit 5e2b551

Please sign in to comment.