iorum-acts_as_viewable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/CHANGELOG +2 -0
- data/MIT-LICENSE +21 -0
- data/README.markdown +41 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/generators/acts_as_viewable/USAGE +8 -0
- data/generators/acts_as_viewable/acts_as_viewable_generator.rb +10 -0
- data/generators/acts_as_viewable/templates/acts_as_viewable.rake +6 -0
- data/generators/acts_as_viewable/templates/acts_as_viewable_migration.rb +33 -0
- data/init.rb +1 -0
- data/lib/acts_as_viewable.rb +7 -0
- data/lib/acts_as_viewable/acts_as_viewable.rb +36 -0
- data/lib/acts_as_viewable/total_viewings.rb +7 -0
- data/lib/acts_as_viewable/viewing.rb +19 -0
- data/rails/init.rb +3 -0
- data/spec/acts_as_viewable/acts_as_viewable_spec.rb +155 -0
- data/spec/acts_as_viewable/total_viewings_spec.rb +68 -0
- data/spec/acts_as_viewable/viewing_spec.rb +74 -0
- data/spec/schema.rb +26 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +23 -0
- metadata +168 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2010 iorum design & technology consultants.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
INGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# acts_as_viewable
|
2
|
+
|
3
|
+
A rather application specific gem for rails 2 which adds the ability to count views of your records.
|
4
|
+
|
5
|
+
When a model `acts_as_viewable` its instances are endowed with a `view!` method, which takes a single argument - an IP address. Invoking `view!` on a record records the viewing and increments a counter *unless* the IP in question has viewed this record within the last *n* minutes, where *n* can be specified in minutes via the `:ttl` option to `acts_as_viewable` (defaults to 60).
|
6
|
+
|
7
|
+
A rake task is provided for pruning view recordings.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
config.gem 'acts_as_viewable'
|
12
|
+
|
13
|
+
$ rake gems:install
|
14
|
+
$ ./script/generate acts_as_viewable
|
15
|
+
$ rake db:migrate
|
16
|
+
|
17
|
+
## Example
|
18
|
+
|
19
|
+
class Foo < ActiveRecord::Base
|
20
|
+
acts_as_viewable :ttl => 60 * 24
|
21
|
+
end
|
22
|
+
|
23
|
+
class FoosController < ActionController::Base
|
24
|
+
def show
|
25
|
+
@foo = Foo.find(params[:id])
|
26
|
+
@foo.view!(request.remote_ip)
|
27
|
+
@total_viewings_for_all_time = @foo.views
|
28
|
+
@records_of_viewings = @foo.viewings
|
29
|
+
@twelve_most_viewed_foos_of_all_time = Foo.most_viewed(12)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
To delete viewing records greater than 10 days old.
|
34
|
+
|
35
|
+
$ rake acts_as_viewable:delete_old_records[10]
|
36
|
+
|
37
|
+
This will not effect the total viewings of any record.
|
38
|
+
|
39
|
+
## Copyright
|
40
|
+
|
41
|
+
Copyright (c) 2010 iorum design & technology consultants. See MIT-LICENSE for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gem|
|
6
|
+
gem.name = 'iorum-acts_as_viewable'
|
7
|
+
gem.homepage = 'http://github.com/iorum/acts_as_viewable'
|
8
|
+
gem.license = 'MIT'
|
9
|
+
gem.summary = %Q{Record IP/timeframe unique viewings of your models}
|
10
|
+
gem.description = %Q{Adds models and methods to facilitate tracking of views of your models. Models which act_as_viewable have an instance method view! which will record a viewing of a record by the supplied IP address if and only if the IP has not viewed the record in the last n minutes.}
|
11
|
+
gem.email = 'alan.larkin@gmail.com'
|
12
|
+
gem.authors = ['Alan Larkin']
|
13
|
+
gem.add_development_dependency 'sqlite3-ruby', '>= 1.2.5'
|
14
|
+
gem.add_development_dependency 'rspec', '~> 1.3'
|
15
|
+
gem.add_development_dependency 'rspec-rails', '~> 1.3'
|
16
|
+
gem.add_development_dependency 'timecop'
|
17
|
+
gem.add_development_dependency 'jeweler', '~> 1.5.1'
|
18
|
+
end
|
19
|
+
Jeweler::RubygemsDotOrgTasks.new
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
desc 'Default: run specs'
|
23
|
+
task :default => :spec
|
24
|
+
Spec::Rake::SpecTask.new do |t|
|
25
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
require 'rake/rdoctask'
|
29
|
+
Rake::RDocTask.new do |rdoc|
|
30
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
31
|
+
|
32
|
+
rdoc.rdoc_dir = 'rdoc'
|
33
|
+
rdoc.title = "acts_as_viewable #{version}"
|
34
|
+
rdoc.rdoc_files.include('README*')
|
35
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
36
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module ActsAsViewable
|
2
|
+
class ActsAsViewableGenerator < Rails::Generator::Base
|
3
|
+
def manifest
|
4
|
+
record do |m|
|
5
|
+
m.migration_template 'acts_as_viewable_migration.rb', 'db/migrate', :migration_file_name => 'acts_as_viewable_migration'
|
6
|
+
m.file 'acts_as_viewable.rake', 'lib/tasks/acts_as_viewable.rake', :collision => :ask
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
namespace :acts_as_viewable do
|
2
|
+
desc 'Delete any Viewing records that are more than the specified number of days old (default 30)'
|
3
|
+
task :delete_old_viewing_records, :days_old, :needs => [:environment] do |task, args|
|
4
|
+
ActsAsViewable::Viewing.delete_all(['created_at < ?', (args[:days_old] || 30).to_i.days.ago])
|
5
|
+
end
|
6
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class ActsAsViewableMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :viewings do |t|
|
4
|
+
t.integer :viewable_id
|
5
|
+
t.string :viewable_type
|
6
|
+
t.string :ip
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :viewings, [:viewable_id, :viewable_type, :ip]
|
11
|
+
add_index :viewings, [:viewable_type]
|
12
|
+
|
13
|
+
create_table :total_viewings do |t|
|
14
|
+
t.integer :viewable_id
|
15
|
+
t.string :viewable_type
|
16
|
+
t.integer :viewings
|
17
|
+
t.timestamps
|
18
|
+
end
|
19
|
+
|
20
|
+
add_index :total_viewings, [:viewable_type, :viewable_id], :unique => true
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.down
|
24
|
+
remove_index :total_viewings, [:viewable_type, :viewable_id]
|
25
|
+
|
26
|
+
drop_table :total_viewings
|
27
|
+
|
28
|
+
add_index :viewings, [:viewable_type]
|
29
|
+
add_index :viewings, [:viewable_id, :viewable_type, :ip]
|
30
|
+
|
31
|
+
drop_table :viewings
|
32
|
+
end
|
33
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/rails/init'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActsAsViewable
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
# Options:
|
8
|
+
#
|
9
|
+
# :ttl The minimum number of minutes between viewings of a specific viewable by a specific IP in order for the viewings to be considered distinct
|
10
|
+
#
|
11
|
+
def acts_as_viewable(options = {})
|
12
|
+
include InstanceMethods
|
13
|
+
options = { :ttl => 60 }.merge(options)
|
14
|
+
const_set('VIEWING_TIME_TO_LIVE', options[:ttl])
|
15
|
+
has_many :viewings, :as => :viewable, :class_name => 'ActsAsViewable::Viewing'
|
16
|
+
has_one :total_viewings, :conditions => { :viewable_type => self.name }, :as => :viewable, :class_name => 'ActsAsViewable::TotalViewings'
|
17
|
+
named_scope :most_viewed, lambda { |*args| { :include => :total_viewings, :order => '`total_viewings`.`viewings` DESC', :limit => args.first || 10 } }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module InstanceMethods
|
22
|
+
def views
|
23
|
+
total_viewings && total_viewings.viewings || 0
|
24
|
+
end
|
25
|
+
|
26
|
+
def view!(ip)
|
27
|
+
unless Viewing.create(:viewable_id => id, :viewable_type => self.class.name, :ip => ip).new_record?
|
28
|
+
if total_viewings
|
29
|
+
total_viewings.increment!(:viewings)
|
30
|
+
else
|
31
|
+
create_total_viewings(:viewable_type => self.class.name, :viewings => 1)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActsAsViewable
|
2
|
+
class Viewing < ::ActiveRecord::Base
|
3
|
+
belongs_to :viewable, :polymorphic => true
|
4
|
+
|
5
|
+
validates_presence_of :viewable_type, :viewable_id, :ip
|
6
|
+
validate :ip_has_not_viewed_viewable_in_last_n_minutes, :if => :viewable_type
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def ip_has_not_viewed_viewable_in_last_n_minutes
|
11
|
+
errors.add(:ip, 'has viewed this viewable too recently') unless Viewing.count(:conditions => {
|
12
|
+
:ip => ip,
|
13
|
+
:viewable_id => viewable_id,
|
14
|
+
:viewable_type => viewable_type,
|
15
|
+
:created_at => (viewable_type.camelcase.constantize::VIEWING_TIME_TO_LIVE || 0).minutes.ago..Time.now
|
16
|
+
}).zero?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe 'ActsAsViewable' do
|
4
|
+
before(:each) do
|
5
|
+
ActsAsViewable::Viewing.delete_all
|
6
|
+
ActsAsViewable::TotalViewings.delete_all
|
7
|
+
@viewable = ViewableModelWithoutTtl.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'when a ttl is supplied' do
|
11
|
+
it 'should define a VIEWING_TIME_TO_LIVE constant in the model to be the supplied value' do
|
12
|
+
ViewableModelWithTtl::VIEWING_TIME_TO_LIVE.should == 30
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'when no ttl is supplied' do
|
17
|
+
it 'should define a VIEWING_TIME_TO_LIVE constant in the model to be 60' do
|
18
|
+
ViewableModelWithoutTtl::VIEWING_TIME_TO_LIVE.should == 60
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should generate an association for viewings' do
|
23
|
+
@viewable.should respond_to(:viewings)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should generate an association for total_viewings' do
|
27
|
+
@viewable.should respond_to(:total_viewings)
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'most_viewed' do
|
31
|
+
before :each do
|
32
|
+
@viewable_1 = ViewableModelWithoutTtl.new; @viewable_1.save(:validate => false); @viewable_1.build_total_viewings(:viewings => 1).save(:validate => false)
|
33
|
+
@viewable_2 = ViewableModelWithoutTtl.new; @viewable_2.save(:validate => false); @viewable_2.build_total_viewings(:viewings => 2).save(:validate => false)
|
34
|
+
@viewable_3 = ViewableModelWithoutTtl.new; @viewable_3.save(:validate => false); @viewable_3.build_total_viewings(:viewings => 3).save(:validate => false)
|
35
|
+
@viewable_4 = ViewableModelWithoutTtl.new; @viewable_4.save(:validate => false); @viewable_4.build_total_viewings(:viewings => 4).save(:validate => false)
|
36
|
+
@viewable_5 = ViewableModelWithoutTtl.new; @viewable_5.save(:validate => false); @viewable_5.build_total_viewings(:viewings => 5).save(:validate => false)
|
37
|
+
@viewable_6 = ViewableModelWithoutTtl.new; @viewable_6.save(:validate => false); @viewable_6.build_total_viewings(:viewings => 6).save(:validate => false)
|
38
|
+
@viewable_7 = ViewableModelWithoutTtl.new; @viewable_7.save(:validate => false); @viewable_7.build_total_viewings(:viewings => 7).save(:validate => false)
|
39
|
+
@viewable_8 = ViewableModelWithoutTtl.new; @viewable_8.save(:validate => false); @viewable_8.build_total_viewings(:viewings => 8).save(:validate => false)
|
40
|
+
@viewable_9 = ViewableModelWithoutTtl.new; @viewable_9.save(:validate => false); @viewable_9.build_total_viewings(:viewings => 9).save(:validate => false)
|
41
|
+
@viewable_10 = ViewableModelWithoutTtl.new; @viewable_10.save(:validate => false); @viewable_10.build_total_viewings(:viewings => 10).save(:validate => false)
|
42
|
+
@viewable_11 = ViewableModelWithoutTtl.new; @viewable_11.save(:validate => false); @viewable_11.build_total_viewings(:viewings => 11).save(:validate => false)
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'when no argument is supplied' do
|
46
|
+
it 'should return the 10 viewables with the highest number of total viewings (in descending order)' do
|
47
|
+
ViewableModelWithoutTtl.most_viewed.should == [
|
48
|
+
@viewable_11, @viewable_10, @viewable_9, @viewable_8, @viewable_7,
|
49
|
+
@viewable_6, @viewable_5, @viewable_4, @viewable_3, @viewable_2
|
50
|
+
]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'when an argument is supplied' do
|
55
|
+
it 'should return the specified number of viewables with the highest number of total viewings (in descending order)' do
|
56
|
+
ViewableModelWithoutTtl.most_viewed(3).should == [@viewable_11, @viewable_10, @viewable_9]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'views' do
|
62
|
+
before :each do
|
63
|
+
@viewable = ViewableModelWithoutTtl.new; @viewable.save(:validate => false);
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'when there is an associated total viewings' do
|
67
|
+
before :each do
|
68
|
+
@viewable.build_total_viewings(:viewings => 2431267).save(:validate => false)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should return the viewings of the associated total viewings' do
|
72
|
+
@viewable.views.should == 2431267
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'when there is no associated total viewings' do
|
77
|
+
it 'should return 0' do
|
78
|
+
@viewable.views.should be_zero
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'view!' do
|
84
|
+
before :each do
|
85
|
+
@ip = 'ip'
|
86
|
+
@viewable = ViewableModelWithoutTtl.new; @viewable.save(:validate => false);
|
87
|
+
ActsAsViewable::Viewing.stub!(:create).and_return(mock('ActsAsViewable::Viewing', :new_record? => true))
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should attempt to create a viewing of the viewable by the supplied IP' do
|
91
|
+
ActsAsViewable::Viewing.should_receive(:create).with(:viewable_id => @viewable.id, :viewable_type => 'ViewableModelWithoutTtl', :ip => @ip)
|
92
|
+
@viewable.view!(@ip)
|
93
|
+
end
|
94
|
+
|
95
|
+
describe 'when a viewing of the viewable could be created for the specified IP' do
|
96
|
+
before :each do
|
97
|
+
ActsAsViewable::Viewing.stub!(:create).and_return(mock('ActsAsViewable::Viewing', :new_record? => false))
|
98
|
+
end
|
99
|
+
|
100
|
+
describe 'and a total viewings record already exists for this viewable' do
|
101
|
+
before :each do
|
102
|
+
@total_viewings = mock('ActsAsViewable::TotalViewings', :increment! => true)
|
103
|
+
@viewable.stub!(:total_viewings).and_return(@total_viewings)
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should increment the number of viewings of the total viewings' do
|
107
|
+
@total_viewings.should_receive(:increment!).with(:viewings)
|
108
|
+
@viewable.view!(@ip)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe 'and a total viewings record does not already exist for this viewable' do
|
113
|
+
before :each do
|
114
|
+
@viewable.stub!(:total_viewings).and_return(nil)
|
115
|
+
@viewable.stub!(:create_total_viewings)
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should initialise an associated total viewings to a value of 1' do
|
119
|
+
@viewable.should_receive(:create_total_viewings).with(:viewable_type => 'ViewableModelWithoutTtl', :viewings => 1)
|
120
|
+
@viewable.view!(@ip)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe 'when a viewing of the viewable could not be created for the specified IP' do
|
126
|
+
before :each do
|
127
|
+
ActsAsViewable::Viewing.stub!(:create).and_return(mock('ActsAsViewable::Viewing', :new_record? => true))
|
128
|
+
end
|
129
|
+
|
130
|
+
describe 'and a total viewings record already exists for this viewable' do
|
131
|
+
before :each do
|
132
|
+
@total_viewings = mock('ActsAsViewable::TotalViewings', :increment! => true)
|
133
|
+
@viewable.stub!(:total_viewings).and_return(@total_viewings)
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'should not increment the number of viewings of the total viewings' do
|
137
|
+
@total_viewings.should_not_receive(:increment!)
|
138
|
+
@viewable.view!(@ip)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe 'and a total viewings record does not already exist for this viewable' do
|
143
|
+
before :each do
|
144
|
+
@viewable.stub!(:total_viewings).and_return(nil)
|
145
|
+
@viewable.stub!(:create_total_viewings)
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'should not initialise an associated total viewings' do
|
149
|
+
@total_viewings.should_not_receive(:create_total_viewings)
|
150
|
+
@viewable.view!(@ip)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
+
|
3
|
+
shared_examples_for 'all invalid total viewings' do
|
4
|
+
it 'should not be valid' do
|
5
|
+
@gig.should_not be_valid
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should have an error message' do
|
9
|
+
@gig.valid?
|
10
|
+
@gig.errors[@attr.to_sym].should_not be_nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ActsAsViewable::TotalViewings do
|
15
|
+
before(:each) do
|
16
|
+
ActsAsViewable::Viewing.delete_all
|
17
|
+
ActsAsViewable::TotalViewings.delete_all
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid_attributes
|
21
|
+
{
|
22
|
+
:viewable_id => 1,
|
23
|
+
:viewable_type => 'viewable type',
|
24
|
+
:viewings => 12378
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'creation' do
|
29
|
+
describe 'with valid arguments' do
|
30
|
+
it 'should be valid' do
|
31
|
+
ActsAsViewable::TotalViewings.new(valid_attributes).should be_valid
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'with invalid arguments' do
|
36
|
+
describe '(no viewable_id)' do
|
37
|
+
before :each do
|
38
|
+
@attr = :viewable_id
|
39
|
+
@gig = ActsAsViewable::TotalViewings.new(valid_attributes.except(@attr))
|
40
|
+
end
|
41
|
+
|
42
|
+
it_should_behave_like 'all invalid total viewings'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'with invalid arguments' do
|
47
|
+
describe '(no viewable_type)' do
|
48
|
+
before :each do
|
49
|
+
@attr = :viewable_type
|
50
|
+
@gig = ActsAsViewable::TotalViewings.new(valid_attributes.except(@attr))
|
51
|
+
end
|
52
|
+
|
53
|
+
it_should_behave_like 'all invalid total viewings'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'with invalid arguments' do
|
58
|
+
describe '(no viewings)' do
|
59
|
+
before :each do
|
60
|
+
@attr = :viewings
|
61
|
+
@gig = ActsAsViewable::TotalViewings.new(valid_attributes.except(@attr))
|
62
|
+
end
|
63
|
+
|
64
|
+
it_should_behave_like 'all invalid total viewings'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
+
|
3
|
+
shared_examples_for 'all invalid viewings' do
|
4
|
+
it 'should not be valid' do
|
5
|
+
@viewing.should_not be_valid
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should have an error message' do
|
9
|
+
@viewing.valid?
|
10
|
+
@viewing.errors[@attr.to_sym].should_not be_nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ActsAsViewable::Viewing do
|
15
|
+
before(:each) do
|
16
|
+
ActsAsViewable::Viewing.delete_all
|
17
|
+
ActsAsViewable::TotalViewings.delete_all
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid_attributes
|
21
|
+
{
|
22
|
+
:viewable_type => 'viewable_model_with_ttl',
|
23
|
+
:viewable_id => 1,
|
24
|
+
:ip => 'ip'
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'creation' do
|
29
|
+
describe 'with valid arguments' do
|
30
|
+
it 'should be valid' do
|
31
|
+
ActsAsViewable::Viewing.new(valid_attributes).should be_valid
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'with invalid arguments' do
|
36
|
+
describe '(no viewable_type)' do
|
37
|
+
before :each do
|
38
|
+
@attr = :viewable_type
|
39
|
+
@viewing = ActsAsViewable::Viewing.new(valid_attributes.except(@attr))
|
40
|
+
end
|
41
|
+
|
42
|
+
it_should_behave_like 'all invalid viewings'
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '(no viewable_id)' do
|
46
|
+
before :each do
|
47
|
+
@attr = :viewable_id
|
48
|
+
@viewing = ActsAsViewable::Viewing.new(valid_attributes.except(@attr))
|
49
|
+
end
|
50
|
+
|
51
|
+
it_should_behave_like 'all invalid viewings'
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '(no ip)' do
|
55
|
+
before :each do
|
56
|
+
@attr = :ip
|
57
|
+
@viewing = ActsAsViewable::Viewing.new(valid_attributes.except(@attr))
|
58
|
+
end
|
59
|
+
|
60
|
+
it_should_behave_like 'all invalid viewings'
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '(it is too soon to create the viewable)' do
|
64
|
+
before :each do
|
65
|
+
@attr = :ip
|
66
|
+
ActsAsViewable::Viewing.new(valid_attributes.merge(:created_at => 29.minutes.ago)).save(:validate => false)
|
67
|
+
@viewing = ActsAsViewable::Viewing.new(valid_attributes)
|
68
|
+
end
|
69
|
+
|
70
|
+
it_should_behave_like 'all invalid viewings'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
ActiveRecord::Schema.define :version => 0 do
|
2
|
+
create_table :viewings do |t|
|
3
|
+
t.integer :viewable_id
|
4
|
+
t.string :viewable_type
|
5
|
+
t.string :ip
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
|
9
|
+
add_index :viewings, [:viewable_id, :viewable_type, :ip]
|
10
|
+
add_index :viewings, [:viewable_type]
|
11
|
+
|
12
|
+
create_table :total_viewings do |t|
|
13
|
+
t.integer :viewable_id
|
14
|
+
t.string :viewable_type
|
15
|
+
t.integer :viewings
|
16
|
+
t.timestamps
|
17
|
+
end
|
18
|
+
|
19
|
+
add_index :total_viewings, [:viewable_type, :viewable_id], :unique => true
|
20
|
+
|
21
|
+
create_table :viewable_model_without_ttls, :force => true do |t|
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table :viewable_model_with_ttls, :force => true do |t|
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_record'
|
3
|
+
require 'spec'
|
4
|
+
|
5
|
+
TEST_DATABASE_FILE = File.join(File.dirname(__FILE__), '..', 'test.sqlite3')
|
6
|
+
|
7
|
+
File.unlink(TEST_DATABASE_FILE) if File.exist?(TEST_DATABASE_FILE)
|
8
|
+
ActiveRecord::Base.establish_connection(
|
9
|
+
'adapter' => 'sqlite3', 'database' => TEST_DATABASE_FILE
|
10
|
+
)
|
11
|
+
|
12
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
13
|
+
|
14
|
+
$: << File.join(File.dirname(__FILE__), '..', 'lib')
|
15
|
+
require File.join(File.dirname(__FILE__), '..', 'init')
|
16
|
+
|
17
|
+
class ViewableModelWithoutTtl < ActiveRecord::Base
|
18
|
+
acts_as_viewable
|
19
|
+
end
|
20
|
+
|
21
|
+
class ViewableModelWithTtl < ActiveRecord::Base
|
22
|
+
acts_as_viewable :ttl => 30
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iorum-acts_as_viewable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Alan Larkin
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-02 00:00:00 +00:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: sqlite3-ruby
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 21
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 5
|
34
|
+
version: 1.2.5
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 9
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 3
|
49
|
+
version: "1.3"
|
50
|
+
type: :development
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: rspec-rails
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 9
|
61
|
+
segments:
|
62
|
+
- 1
|
63
|
+
- 3
|
64
|
+
version: "1.3"
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: timecop
|
69
|
+
prerelease: false
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: jeweler
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 1
|
90
|
+
segments:
|
91
|
+
- 1
|
92
|
+
- 5
|
93
|
+
- 1
|
94
|
+
version: 1.5.1
|
95
|
+
type: :development
|
96
|
+
version_requirements: *id005
|
97
|
+
description: Adds models and methods to facilitate tracking of views of your models. Models which act_as_viewable have an instance method view! which will record a viewing of a record by the supplied IP address if and only if the IP has not viewed the record in the last n minutes.
|
98
|
+
email: alan.larkin@gmail.com
|
99
|
+
executables: []
|
100
|
+
|
101
|
+
extensions: []
|
102
|
+
|
103
|
+
extra_rdoc_files:
|
104
|
+
- README.markdown
|
105
|
+
files:
|
106
|
+
- .document
|
107
|
+
- .rspec
|
108
|
+
- CHANGELOG
|
109
|
+
- MIT-LICENSE
|
110
|
+
- README.markdown
|
111
|
+
- Rakefile
|
112
|
+
- VERSION
|
113
|
+
- generators/acts_as_viewable/USAGE
|
114
|
+
- generators/acts_as_viewable/acts_as_viewable_generator.rb
|
115
|
+
- generators/acts_as_viewable/templates/acts_as_viewable.rake
|
116
|
+
- generators/acts_as_viewable/templates/acts_as_viewable_migration.rb
|
117
|
+
- init.rb
|
118
|
+
- lib/acts_as_viewable.rb
|
119
|
+
- lib/acts_as_viewable/acts_as_viewable.rb
|
120
|
+
- lib/acts_as_viewable/total_viewings.rb
|
121
|
+
- lib/acts_as_viewable/viewing.rb
|
122
|
+
- rails/init.rb
|
123
|
+
- spec/acts_as_viewable/acts_as_viewable_spec.rb
|
124
|
+
- spec/acts_as_viewable/total_viewings_spec.rb
|
125
|
+
- spec/acts_as_viewable/viewing_spec.rb
|
126
|
+
- spec/schema.rb
|
127
|
+
- spec/spec.opts
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
has_rdoc: true
|
130
|
+
homepage: http://github.com/iorum/acts_as_viewable
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
hash: 3
|
144
|
+
segments:
|
145
|
+
- 0
|
146
|
+
version: "0"
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
hash: 3
|
153
|
+
segments:
|
154
|
+
- 0
|
155
|
+
version: "0"
|
156
|
+
requirements: []
|
157
|
+
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 1.3.7
|
160
|
+
signing_key:
|
161
|
+
specification_version: 3
|
162
|
+
summary: Record IP/timeframe unique viewings of your models
|
163
|
+
test_files:
|
164
|
+
- spec/acts_as_viewable/acts_as_viewable_spec.rb
|
165
|
+
- spec/acts_as_viewable/total_viewings_spec.rb
|
166
|
+
- spec/acts_as_viewable/viewing_spec.rb
|
167
|
+
- spec/schema.rb
|
168
|
+
- spec/spec_helper.rb
|