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