Skip to content

Commit

Permalink
added :if and :unless conditionals to impressionist
Browse files Browse the repository at this point in the history
  • Loading branch information
beghbali committed Feb 21, 2014
1 parent 1c3ac14 commit 2e5dabe
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions app/controllers/impressionist_controller.rb
Expand Up @@ -3,7 +3,7 @@
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

Expand All @@ -13,7 +13,7 @@ def self.included(base)
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}))
Expand All @@ -29,10 +29,11 @@ def impressionist_app_filter
@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
Expand All @@ -59,6 +60,14 @@ def bypass
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
Expand Down

0 comments on commit 2e5dabe

Please sign in to comment.