1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

polymorphic after filter

This commit is contained in:
Aaron Patterson 2013-05-14 11:37:34 -07:00
parent c5953f7dba
commit e8ddd4f9e5

View file

@ -176,6 +176,75 @@ module ActiveSupport
} }
end end
end end
class After
def self.build(next_callback, user_callback, user_conditions, chain_config)
if chain_config[:skip_after_callbacks_if_terminated]
if chain_config.key?(:terminator) && user_conditions.any?
halting_and_conditional(next_callback, user_callback, user_conditions)
elsif chain_config.key?(:terminator)
halting(next_callback, user_callback)
elsif user_conditions.any?
conditional next_callback, user_callback, user_conditions
else
simple next_callback, user_callback
end
else
if user_conditions.any?
conditional next_callback, user_callback, user_conditions
else
simple next_callback, user_callback
end
end
end
private
def self.halting_and_conditional(next_callback, user_callback, user_conditions)
lambda { |env|
env = next_callback.call env
target = env.target
value = env.value
halted = env.halted
if !halted && user_conditions.all? { |c| c.call(target, value) }
user_callback.call target, value
end
env
}
end
def self.halting(next_callback, user_callback)
lambda { |env|
env = next_callback.call env
if !env.halted
user_callback.call env.target, env.value
end
env
}
end
def self.conditional(next_callback, user_callback, user_conditions)
lambda { |env|
env = next_callback.call env
target = env.target
value = env.value
if user_conditions.all? { |c| c.call(target, value) }
user_callback.call target, value
end
env
}
end
def self.simple(next_callback, user_callback)
lambda { |env|
env = next_callback.call env
user_callback.call env.target, env.value
env
}
end
end
end end
class Callback #:nodoc:# class Callback #:nodoc:#
@ -248,31 +317,7 @@ module ActiveSupport
when :before when :before
Filters::Before.build(next_callback, user_callback, user_conditions, chain_config, @filter) Filters::Before.build(next_callback, user_callback, user_conditions, chain_config, @filter)
when :after when :after
if chain_config[:skip_after_callbacks_if_terminated] Filters::After.build(next_callback, user_callback, user_conditions, chain_config)
lambda { |env|
env = next_callback.call env
target = env.target
value = env.value
halted = env.halted
if !halted && user_conditions.all? { |c| c.call(target, value) }
user_callback.call target, value
end
env
}
else
lambda { |env|
env = next_callback.call env
target = env.target
value = env.value
halted = env.halted
if user_conditions.all? { |c| c.call(target, value) }
user_callback.call target, value
end
env
}
end
when :around when :around
lambda { |env| lambda { |env|
target = env.target target = env.target