提交 da18193d 编写于 作者: J Jamis Buck

More consistent implementation of filter replacement (thanks Martin! closes #5949)


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5331 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 8949ce09
...@@ -248,10 +248,7 @@ module ClassMethods ...@@ -248,10 +248,7 @@ module ClassMethods
# The passed <tt>filters</tt> will be appended to the filter_chain and # The passed <tt>filters</tt> will be appended to the filter_chain and
# will execute before the action on this controller is performed. # will execute before the action on this controller is performed.
def append_before_filter(*filters, &block) def append_before_filter(*filters, &block)
new_filters, existing_filters = look_for_existing_filters(filters, :before) append_filter_to_chain(filters, :before, &block)
append_filter_to_chain(new_filters, :before, &block)
skip_before_filter(existing_filters) unless existing_filters.empty?
end end
# The passed <tt>filters</tt> will be prepended to the filter_chain and # The passed <tt>filters</tt> will be prepended to the filter_chain and
...@@ -266,10 +263,7 @@ def prepend_before_filter(*filters, &block) ...@@ -266,10 +263,7 @@ def prepend_before_filter(*filters, &block)
# The passed <tt>filters</tt> will be appended to the array of filters # The passed <tt>filters</tt> will be appended to the array of filters
# that run _after_ actions on this controller are performed. # that run _after_ actions on this controller are performed.
def append_after_filter(*filters, &block) def append_after_filter(*filters, &block)
new_filters, existing_filters = look_for_existing_filters(filters, :after) prepend_filter_to_chain(filters, :after, &block)
prepend_filter_to_chain(new_filters, :after, &block)
skip_after_filter(existing_filters) unless existing_filters.empty?
end end
# The passed <tt>filters</tt> will be prepended to the array of filters # The passed <tt>filters</tt> will be prepended to the array of filters
...@@ -411,6 +405,10 @@ def after? ...@@ -411,6 +405,10 @@ def after?
false false
end end
def around?
true
end
def call(controller, &block) def call(controller, &block)
raise(ActionControllerError, 'No filter type: Nothing to do here.') raise(ActionControllerError, 'No filter type: Nothing to do here.')
end end
...@@ -422,6 +420,10 @@ class FilterProxy < Filter #:nodoc: ...@@ -422,6 +420,10 @@ class FilterProxy < Filter #:nodoc:
def filter def filter
@filter.filter @filter.filter
end end
def around?
false
end
end end
class BeforeFilterProxy < FilterProxy #:nodoc: class BeforeFilterProxy < FilterProxy #:nodoc:
...@@ -494,27 +496,26 @@ def prepend_filter_to_chain(filters, position = :around, &block) ...@@ -494,27 +496,26 @@ def prepend_filter_to_chain(filters, position = :around, &block)
def create_filters(filters, position, &block) #:nodoc: def create_filters(filters, position, &block) #:nodoc:
filters, conditions = extract_conditions(filters, &block) filters, conditions = extract_conditions(filters, &block)
filters.map! { |filter| find_or_create_filter(filter,position) }
update_conditions(filters, conditions)
filters
end
filters.map! do |filter| def find_or_create_filter(filter,position)
# change all the filters into instances of Filter derived classes if found_filter = find_filter(filter) { |f| f.send("#{position}?") }
class_for_filter(filter).new(filter) found_filter
end else
f = class_for_filter(filter).new(filter)
filters.map! do |filter|
# apply proxy to filter if necessary # apply proxy to filter if necessary
case position case position
when :before when :before
BeforeFilterProxy.new(filter) BeforeFilterProxy.new(f)
when :after when :after
AfterFilterProxy.new(filter) AfterFilterProxy.new(f)
else else
filter f
end end
end end
update_conditions(filters, conditions)
filters
end end
# The determination of the filter type was once done at run time. # The determination of the filter type was once done at run time.
...@@ -603,27 +604,6 @@ def proxy_before_and_after_filter(filter) #:nodoc: ...@@ -603,27 +604,6 @@ def proxy_before_and_after_filter(filter) #:nodoc:
end end
end end
end end
def look_for_existing_filters(filters, which)
filters, options = extract_conditions(filters)
old_filters = []
filter_chain.select(&"#{which}?".to_sym).each do |f|
old_filters << f.filter if filters.include?(f.filter)
end
new_filters = filters - old_filters + [options]
if options[:except]
old_filters << { :only => options[:except] }
elsif options[:only]
old_filters << { :except => options[:only] }
else
old_filters = []
end
[new_filters, old_filters]
end
end end
module InstanceMethods # :nodoc: module InstanceMethods # :nodoc:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册