From bfb03eeb1c3cf4100b2ccacca3982d9ab5826002 Mon Sep 17 00:00:00 2001 From: cowboycoded Date: Thu, 17 Feb 2011 09:53:01 -0500 Subject: [PATCH] 2 model methods for unique impression account - based on unique ip_address & request_hash --- app/models/impressionist/impressionable.rb | 4 ++++ test_app/Gemfile.lock | 2 +- test_app/spec/fixtures/impressions.yml | 14 +++++++++++++- test_app/spec/models/model_spec.rb | 22 +++++++++++++--------- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/app/models/impressionist/impressionable.rb b/app/models/impressionist/impressionable.rb index f4029f4..6efc533 100644 --- a/app/models/impressionist/impressionable.rb +++ b/app/models/impressionist/impressionable.rb @@ -15,6 +15,10 @@ module Impressionist end def unique_impression_count(start_date=nil,end_date=Time.now) + start_date.blank? ? impressions.group(:request_hash).all.size : impressions.where("created_at>=? and created_at<=?",start_date,end_date).group(:request_hash).all.size + end + + def unique_impression_count_ip(start_date=nil,end_date=nil) start_date.blank? ? impressions.group(:ip_address).all.size : impressions.where("created_at>=? and created_at<=?",start_date,end_date).group(:ip_address).all.size end end diff --git a/test_app/Gemfile.lock b/test_app/Gemfile.lock index 31b0da6..8d0d14c 100644 --- a/test_app/Gemfile.lock +++ b/test_app/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: /rails_plugins/mine/impressionist specs: - impressionist (0.2.3) + impressionist (0.2.4) GEM remote: http://rubygems.org/ diff --git a/test_app/spec/fixtures/impressions.yml b/test_app/spec/fixtures/impressions.yml index 191e8b0..b397089 100644 --- a/test_app/spec/fixtures/impressions.yml +++ b/test_app/spec/fixtures/impressions.yml @@ -2,6 +2,7 @@ impression<%= i %>: impressionable_type: Article impressionable_id: 1 + request_hash: a<%=i%> ip_address: 127.0.0.<%=i%> created_at: 2011-01-01 <% end %> @@ -10,17 +11,28 @@ impression<%= i %>: impression8: impressionable_type: Article impressionable_id: 1 + request_hash: a1 ip_address: 127.0.0.1 created_at: 2010-01-01 impression9: impressionable_type: Article impressionable_id: 1 + request_hash: a1 ip_address: 127.0.0.1 created_at: 2011-01-03 impression10: impressionable_type: Article impressionable_id: 1 + request_hash: a9 + ip_address: 127.0.0.8 + created_at: 2010-01-01 + +impression11: + impressionable_type: Article + impressionable_id: 1 + request_hash: a10 ip_address: 127.0.0.1 - created_at: 2010-01-01 \ No newline at end of file + created_at: 2010-01-01 + \ No newline at end of file diff --git a/test_app/spec/models/model_spec.rb b/test_app/spec/models/model_spec.rb index e17fc46..439ebaf 100644 --- a/test_app/spec/models/model_spec.rb +++ b/test_app/spec/models/model_spec.rb @@ -9,7 +9,7 @@ describe Impression do it "should save a blank impression for an Article that has 10 impressions" do @article.impressions.create - @article.impressions.size.should eq 11 + @article.impressions.size.should eq 12 end it "should save an impression with a message" do @@ -17,27 +17,31 @@ describe Impression do @article.impressions.last.message.should eq "test message" end - it "should return the view count with no date range specified" do - @article.impression_count.should eq 10 + it "should return the impression count with no date range specified" do + @article.impression_count.should eq 11 end - it "should return unique view count with no date range specified" do - @article.unique_impression_count.should eq 7 + it "should return unique impression count with no date range specified" do + @article.unique_impression_count.should eq 9 end - it "should return view count with only start date specified" do + it "should return impression count with only start date specified" do @article.impression_count("2011-01-01").should eq 8 end - it "should return view count with whole date range specified" do + it "should return impression count with whole date range specified" do @article.impression_count("2011-01-01","2011-01-02").should eq 7 end - it "should return unique view count with only start date specified" do + it "should return unique impression count with only start date specified" do @article.unique_impression_count("2011-01-01").should eq 7 end - it "should return unique view count with date range specified" do + it "should return unique impression count with date range specified" do @article.unique_impression_count("2011-01-01","2011-01-02").should eq 7 end + + it "should return unique impression count using ip address (which in turn eliminates duplicate request_hashes)" do + @article.unique_impression_count_ip.should eq 8 + end end \ No newline at end of file