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

Commit

Permalink
filter refactoring
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@317 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information
jcarroll committed Feb 12, 2008
1 parent a228c1a commit e73a708
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
26 changes: 13 additions & 13 deletions lib/callbacks.rb
Expand Up @@ -30,19 +30,19 @@ def #{callback}_with_conditions (*callbacks, &block)
def execute_callback(callback, record, method) def execute_callback(callback, record, method)
if callback.class == Symbol if callback.class == Symbol
record.send callback record.send callback
elsif callback.class == String # elsif callback.class == String
eval callback, record.send(:binding) # eval callback, record.send(:binding)
elsif callback.class == Proc || callback.class == Method # elsif callback.class == Proc || callback.class == Method
callback.call record # callback.call record
else # else
if callback.respond_to?(method) # if callback.respond_to?(method)
callback.send method, record # callback.send method, record
else # else
raise ActiveRecordError, 'Callbacks must be a symbol denoting the message to send, # raise ActiveRecordError, 'Callbacks must be a symbol denoting the message to send,
a string to be evaluated, a block to be invoked or a # a string to be evaluated, a block to be invoked or a
class object responding to the callback message # class object responding to the callback message
(#before_create, #after_create, etc).' # (#before_create, #after_create, etc).'
end # end
end end
end end


Expand Down
18 changes: 17 additions & 1 deletion lib/filters.rb
Expand Up @@ -18,7 +18,7 @@ def #{filter}_with_conditions (*filters, &block)
#{filter}_without_conditions do |controller| #{filter}_without_conditions do |controller|
unless (! options[:if].nil? && ! ActiveRecord::Base.evaluate_condition(options[:if], controller)) || unless (! options[:if].nil? && ! ActiveRecord::Base.evaluate_condition(options[:if], controller)) ||
(! options[:unless].nil? && ActiveRecord::Base.evaluate_condition(options[:unless], controller)) (! options[:unless].nil? && ActiveRecord::Base.evaluate_condition(options[:unless], controller))
controller.send filter execute_filter filter, controller, :#{filter}
end end
end end
end end
Expand All @@ -28,6 +28,22 @@ def #{filter}_with_conditions (*filters, &block)
class_eval src, __FILE__, __LINE__ class_eval src, __FILE__, __LINE__
end 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


Expand Down
16 changes: 15 additions & 1 deletion lib/validations.rb
Expand Up @@ -4,7 +4,7 @@ module Validations
def self.included(klass) def self.included(klass)
class << klass class << klass


validations = ['validate', 'validate_on_create', 'validate_on_update'] validations = ActiveRecord::Validations::VALIDATIONS


validations.each do |validation| validations.each do |validation|
src = <<-END; src = <<-END;
Expand All @@ -30,6 +30,20 @@ def #{validation}_with_conditions (*validations, &block)
def execute_validation(validation, record, method) def execute_validation(validation, record, method)
if validation.class == Symbol if validation.class == Symbol
record.send validation record.send validation
# elsif callback.class == String
# eval callback, record.send(:binding)
# elsif callback.class == Proc || callback.class == Method
# callback.call record
# else
# if callback.respond_to?(method)
# callback.send method, record
# else
# raise ActiveRecordError, 'Callbacks must be a symbol denoting the message to send,
# a string to be evaluated, a block to be invoked or a
# class object responding to the callback message
# (#before_create, #after_create, etc).'
# end

end end
end end


Expand Down
2 changes: 1 addition & 1 deletion lib/when.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), 'callbacks')
require File.join(File.dirname(__FILE__), 'validations') require File.join(File.dirname(__FILE__), 'validations')
require File.join(File.dirname(__FILE__), 'filters') require File.join(File.dirname(__FILE__), 'filters')
require File.join(File.dirname(__FILE__), 'callbacks')


ActiveRecord::Base.send :include, ActiveRecordHook::Callbacks ActiveRecord::Base.send :include, ActiveRecordHook::Callbacks
ActiveRecord::Base.send :include, ActiveRecordHook::Validations ActiveRecord::Base.send :include, ActiveRecordHook::Validations
Expand Down

0 comments on commit e73a708

Please sign in to comment.