cleaned up the logic. too much double negative

This commit is contained in:
Bashir Eghbali 2014-02-21 15:32:07 -08:00
parent 2e5dabe9b3
commit 78a35925b2
1 changed files with 16 additions and 8 deletions

View File

@ -13,7 +13,7 @@ module ImpressionistController
end end
def impressionist(obj,message=nil,opts={}) def impressionist(obj,message=nil,opts={})
unless bypass || !should_count_impression?(opts[:if]) || should_count_impression?(opts[:unless]) if should_count_impression?(opts)
if obj.respond_to?("impressionable?") if obj.respond_to?("impressionable?")
if unique_instance?(obj, opts[:unique]) if unique_instance?(obj, opts[:unique])
obj.impressions.create(associative_create_statement({:message => message})) obj.impressions.create(associative_create_statement({:message => message}))
@ -30,7 +30,7 @@ module ImpressionistController
end end
def impressionist_subapp_filter(opts = {}) def impressionist_subapp_filter(opts = {})
unless bypass || !should_count_impression?(opts[:if]) || should_count_impression?(opts[:unless]) if should_count_impression?(opts)
actions = opts[:actions] actions = opts[:actions]
actions.collect!{|a|a.to_s} unless actions.blank? actions.collect!{|a|a.to_s} unless actions.blank?
if (actions.blank? || actions.include?(action_name)) && unique?(opts[:unique]) if (actions.blank? || actions.include?(action_name)) && unique?(opts[:unique])
@ -60,12 +60,20 @@ module ImpressionistController
Impressionist::Bots.bot?(request.user_agent) Impressionist::Bots.bot?(request.user_agent)
end end
def should_count_impression?(condition) def should_count_impression?(opts)
if condition.present? !bypass && condition_true?(opts[:if]) && condition_false?(opts[:unless])
condition.is_a?(Symbol) ? self.send(condition) : condition.call
else
true
end end
def condition_true?(condition)
condition.present? ? conditional?(condition) : true
end
def condition_false?(condition)
condition.present? ? !conditional?(condition) : true
end
def conditional?(condition)
condition.is_a?(Symbol) ? self.send(condition) : condition.call
end end
def unique_instance?(impressionable, unique_opts) def unique_instance?(impressionable, unique_opts)