This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

removed filters and support for #after_find and #after_initialize

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@320 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent 69ca9a7 commit e3230a4f0a51bceafadfa4cff0d04e3d16616d94 jcarroll committed Feb 13, 2008
Showing with 1 addition and 366 deletions.
  1. +1 −1 lib/callbacks.rb
  2. +0 −51 lib/filters.rb
  3. +0 −2 lib/when.rb
  4. +0 −59 test/callbacks_test.rb
  5. +0 −95 test/filters_test.rb
  6. +0 −155 test/fixtures/companies_controller.rb
  7. +0 −3 test/test_helper.rb
View
@@ -4,7 +4,7 @@ module Callbacks
def self.included(klass)
class << klass
- callbacks = ActiveRecord::Callbacks::CALLBACKS
+ callbacks = ActiveRecord::Callbacks::CALLBACKS - %w(after_find after_initialize)
callbacks.each do |callback|
src = <<-END;
View
@@ -1,51 +0,0 @@
-module ActionControllerHook
- module Filters
-
- def self.included(klass)
- class << klass
-
- filters = %w(append_before_filter prepend_before_filter before_filter
- append_after_filter prepend_after_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))
- execute_filter filter, controller, :#{filter}
- end
- end
- end
- end
- alias_method_chain :#{filter}, :conditions
- END
- class_eval src, __FILE__, __LINE__
- end
-
- def execute_filter(filter, controller, method)
- if filter.class == Symbol
- controller.send filter
-# elsif callback.class == Proc || callback.class == Method
-# callback.call controller
-# else
-# if callback.respond_to?(method)
-# callback.send method, controller
-# else
-# raise ActionControllerError, 'A filter must be a Symbol, Proc, Method, or object
-# responding to filter, after or before.'
-# end
-
- end
- end
-
- end
- end
-
- end
-end
View
@@ -1,7 +1,5 @@
require File.join(File.dirname(__FILE__), 'callbacks')
require File.join(File.dirname(__FILE__), 'validations')
-require File.join(File.dirname(__FILE__), 'filters')
ActiveRecord::Base.send :include, ActiveRecordHook::Callbacks
ActiveRecord::Base.send :include, ActiveRecordHook::Validations
-ActionController::Base.send :include, ActionControllerHook::Filters
View
@@ -13,7 +13,6 @@ def setup
update_callbacks = ['before_update', 'after_update',
'before_validation_on_update', 'after_validation_on_update']
destroy_callbacks = ['before_destroy', 'after_destroy']
- unique_callbacks = ['after_find', 'after_initialize']
conditions = [ lambda {|company| company.callback_flag == true}, :flag? ]
@@ -111,64 +110,6 @@ def setup
assert_equal 'new name', company.name unless company.frozen?
end
end
-
- define_method "test_after_find_with_if_condition_which_returns_true_should_change_company_name" do
- Company.send(:after_find, :change_name, { :if => condition })
- company = Company.new :name => 'thoughtbot', :callback_flag => true
- assert company.save
- company = Company.find company.id
- assert_equal 'new name', company.name
- end
-
- define_method "test_after_find_with_if_condition_which_returns_false_should_not_change_company_name" do
- Company.send(:after_find, :change_name, { :if => condition })
- company = Company.new :name => 'thoughtbot', :callback_flag => false
- assert company.save
- company = Company.find company.id
- assert_equal 'thoughtbot', company.name
- end
-
- define_method "test_after_find_with_unless_condition_which_returns_true_should_not_change_company_name" do
- Company.send(:after_find, :change_name, { :unless => condition })
- company = Company.new :name => 'thoughtbot', :callback_flag => true
- assert company.save
- company = Company.find company.id
- assert_equal 'thoughtbot', company.name
- end
-
- define_method "test_after_find_with_unless_condition_which_returns_false_should_change_company_name" do
- Company.send(:after_find, :change_name, { :unless => condition })
- company = Company.new :name => 'thoughtbot', :callback_flag => false
- assert company.save
- company = Company.find company.id
- assert_equal 'new name', company.name
- end
-
- define_method "test_after_initialize_with_if_condition_which_returns_true_should_change_company_name" do
- Company.send(:after_initialize, :change_name, { :if => condition })
- company = Company.new :name => 'thoughtbot', :callback_flag => true
- assert company.save
- company = Company.find company.id
- assert_equal 'new name', company.name
- end
-
- define_method "test_after_initialize_with_if_condition_which_returns_false_should_not_change_company_name" do
- Company.send(:after_initialize, :change_name, { :if => condition })
- company = Company.new :name => 'thoughtbot', :callback_flag => false
- assert_equal 'thoughtbot', company.name
- end
-
- define_method "test_after_initialize_with_unless_condition_which_returns_true_should_not_change_company_name" do
- Company.send(:after_initialize, :change_name, { :unless => condition })
- company = Company.new :name => 'thoughtbot', :callback_flag => true
- assert_equal 'thoughtbot', company.name
- end
-
- define_method "test_after_initialize_with_unless_condition_which_returns_false_should_change_company_name" do
- Company.send(:after_initialize, :change_name, { :unless => condition })
- company = Company.new :name => 'thoughtbot', :callback_flag => false
- assert_equal 'new name', company.name
- end
end
def teardown
View
@@ -1,95 +0,0 @@
-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
- assert_equal 'before_filter_unless', @controller.attribute2
- 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
- assert_equal 'after_filter_unless', @controller.attribute4
- end
-
- def test_after_filter_unless
- get :index, :q => '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
@@ -1,155 +0,0 @@
-class CompaniesController < ActionController::Base
-
- (1..14).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?
-
- 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
-
- 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
-
- 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
Oops, something went wrong.

0 comments on commit e3230a4

Please sign in to comment.