From 2e5dabe9b3efbd1bb403f6eaabba6b1e134b34a8 Mon Sep 17 00:00:00 2001 From: Bashir Eghbali Date: Fri, 21 Feb 2014 13:13:33 -0800 Subject: [PATCH] added :if and :unless conditionals to impressionist --- app/controllers/impressionist_controller.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/impressionist_controller.rb b/app/controllers/impressionist_controller.rb index 275ed23..59da2a9 100644 --- a/app/controllers/impressionist_controller.rb +++ b/app/controllers/impressionist_controller.rb @@ -3,7 +3,7 @@ require 'digest/sha2' module ImpressionistController module ClassMethods def impressionist(opts={}) - before_filter { |c| c.impressionist_subapp_filter(opts[:actions], opts[:unique])} + before_filter { |c| c.impressionist_subapp_filter(opts)} end end @@ -13,7 +13,7 @@ module ImpressionistController end def impressionist(obj,message=nil,opts={}) - unless bypass + unless bypass || !should_count_impression?(opts[:if]) || should_count_impression?(opts[:unless]) if obj.respond_to?("impressionable?") if unique_instance?(obj, opts[:unique]) obj.impressions.create(associative_create_statement({:message => message})) @@ -29,10 +29,11 @@ module ImpressionistController @impressionist_hash = Digest::SHA2.hexdigest(Time.now.to_f.to_s+rand(10000).to_s) end - def impressionist_subapp_filter(actions=nil,unique_opts=nil) - unless bypass + def impressionist_subapp_filter(opts = {}) + unless bypass || !should_count_impression?(opts[:if]) || should_count_impression?(opts[:unless]) + actions = opts[:actions] actions.collect!{|a|a.to_s} unless actions.blank? - if (actions.blank? || actions.include?(action_name)) && unique?(unique_opts) + if (actions.blank? || actions.include?(action_name)) && unique?(opts[:unique]) Impression.create(direct_create_statement) end end @@ -59,6 +60,14 @@ module ImpressionistController Impressionist::Bots.bot?(request.user_agent) end + def should_count_impression?(condition) + if condition.present? + condition.is_a?(Symbol) ? self.send(condition) : condition.call + else + true + end + end + def unique_instance?(impressionable, unique_opts) return unique_opts.blank? || !impressionable.impressions.where(unique_query(unique_opts)).exists? end