This repository has been archived by the owner on Nov 11, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters