From bad17466ee348a908464fa47c874751b6537bf00 Mon Sep 17 00:00:00 2001 From: Hannes Staffler Date: Fri, 13 Dec 2013 10:20:20 +0100 Subject: [PATCH 1/2] added functionality to filter by message --- app/models/impressionist/impressionable.rb | 4 ++++ tests/test_app/spec/models/model_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/app/models/impressionist/impressionable.rb b/app/models/impressionist/impressionable.rb index c9159bc..ef0b6c8 100644 --- a/app/models/impressionist/impressionable.rb +++ b/app/models/impressionist/impressionable.rb @@ -31,6 +31,10 @@ module Impressionist # If a start_date is provided, finds impressions between then and the end_date. Otherwise returns all impressions imps = options[:start_date].blank? ? impressions : impressions.where("created_at >= ? and created_at <= ?", options[:start_date], options[:end_date]) + if options[:message] + imps = imps.where("impressions.message = ?", options[:message]) + end + # Count all distinct impressions unless the :all filter is provided. distinct = options[:filter] != :all if Rails::VERSION::MAJOR == 4 diff --git a/tests/test_app/spec/models/model_spec.rb b/tests/test_app/spec/models/model_spec.rb index d0c21e2..40a9e7f 100644 --- a/tests/test_app/spec/models/model_spec.rb +++ b/tests/test_app/spec/models/model_spec.rb @@ -17,6 +17,14 @@ describe Impression do @article.impressions.last.message.should eq "test message" end + it "should return the impression count for the message specified" do + @article.impressions.create(:message => "pageview") + @article.impressions.create(:message => "pageview") + @article.impressions.create(:message => "visit") + + @article.impressionist_count(:message => "pageview", :filter => :all).should eq 2 + end + it "should return the impression count for all with no date range specified" do @article.impressionist_count(:filter=>:all).should eq 11 end From 3494a040a28530f609c734a61c9035a0a9946c10 Mon Sep 17 00:00:00 2001 From: Hannes Staffler Date: Fri, 13 Dec 2013 10:39:14 +0100 Subject: [PATCH 2/2] updated readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 1f2b451..13a56cc 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,10 @@ Usage @widget.impressionist_count(:filter=>:all) +9. Get impression count by message. This only counts impressions of the given message. + + @widget.impressionist_count(:message=>"pageview", :filter=>:all) + Logging impressions for authenticated users happens automatically. If you have a current_user helper or use @current_user in your before_filter to set your authenticated user, current_user.id will be written to the user_id field in the