class ActiveSupport::Callbacks::Filters::After

def self.build(callback_sequence, user_callback, user_conditions, chain_config)

def self.build(callback_sequence, user_callback, user_conditions, chain_config)
  if chain_config[:skip_after_callbacks_if_terminated]
    if user_conditions.any?
      halting_and_conditional(callback_sequence, user_callback, user_conditions)
    else
      halting(callback_sequence, user_callback)
    end
  else
    if user_conditions.any?
      conditional callback_sequence, user_callback, user_conditions
    else
      simple callback_sequence, user_callback
    end
  end
end

def self.conditional(callback_sequence, user_callback, user_conditions)

def self.conditional(callback_sequence, user_callback, user_conditions)
  callback_sequence.after do |env|
    target = env.target
    value  = env.value
    if user_conditions.all? { |c| c.call(target, value) }
      user_callback.call target, value
    end
    env
  end
end

def self.halting(callback_sequence, user_callback)

def self.halting(callback_sequence, user_callback)
  callback_sequence.after do |env|
    unless env.halted
      user_callback.call env.target, env.value
    end
    env
  end
end

def self.halting_and_conditional(callback_sequence, user_callback, user_conditions)

def self.halting_and_conditional(callback_sequence, user_callback, user_conditions)
  callback_sequence.after do |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
end

def self.simple(callback_sequence, user_callback)

def self.simple(callback_sequence, user_callback)
  callback_sequence.after do |env|
    user_callback.call env.target, env.value
    env
  end
end