acts_as_footprintable 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +1 -0
- data/Gemfile +18 -0
- data/LICENSE.txt +22 -0
- data/README.md +104 -0
- data/Rakefile +6 -0
- data/acts_as_footprintable.gemspec +27 -0
- data/lib/acts_as_footprintable.rb +12 -0
- data/lib/acts_as_footprintable/extenders/footprintable.rb +28 -0
- data/lib/acts_as_footprintable/extenders/footprinter.rb +21 -0
- data/lib/acts_as_footprintable/footprint.rb +19 -0
- data/lib/acts_as_footprintable/footprintable.rb +32 -0
- data/lib/acts_as_footprintable/footprinter.rb +42 -0
- data/lib/acts_as_footprintable/version.rb +3 -0
- data/lib/generators/acts_as_footprintable/migration/migration_generator.rb +32 -0
- data/lib/generators/acts_as_footprintable/migration/templates/active_record/migration.rb +19 -0
- data/spec/footprintable_spec.rb +97 -0
- data/spec/footprinter_spec.rb +53 -0
- data/spec/spec_helper.rb +73 -0
- metadata +149 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 3a50bee3e3dc9f9a3197091c284ecc247eb92370
|
|
4
|
+
data.tar.gz: fd5f7194fd1dd3fa9c1add735c1a238ce08f3853
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b42916a04487a30db4d5e2e8ec098b5104b012a5c4b4ddbf97ad4b916b8cce455c012f3261daafaa282237ccfc7703859a6ed53f638afee40cb8417a5e6d3787
|
|
7
|
+
data.tar.gz: 38432d7cbc3aa5fefa9eb3f4a605d1421250b44ba02d9b6aff90030a12ba0b76af9e815b07f475e670d11412568c1cc5eeefeb38a137ede8dfab5ca96ca596dd
|
data/.gitignore
ADDED
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--drb --colour --format documentation
|
data/Gemfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
source 'https://rubygems.org'
|
|
2
|
+
|
|
3
|
+
# Specify your gem's dependencies in acts_as_footprintable.gemspec
|
|
4
|
+
gemspec
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
rails_version = ENV['RAILS_VERSION'] || 'default'
|
|
8
|
+
|
|
9
|
+
rails = case rails_version
|
|
10
|
+
when 'master'
|
|
11
|
+
{ :github => 'rails/rails' }
|
|
12
|
+
when 'default'
|
|
13
|
+
'~> 3.2.0'
|
|
14
|
+
else
|
|
15
|
+
"~> #{rails_version}"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
gem 'rails', rails
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 patorash
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# ActsAsFootprint
|
|
2
|
+
|
|
3
|
+
Acts As Footprintable is a Ruby Gem specifically written for Rails/ActiveRecord models.
|
|
4
|
+
The main goals of this gem are:
|
|
5
|
+
|
|
6
|
+
- Allow any model to leave footprints
|
|
7
|
+
- Get access ranking for footprintable model
|
|
8
|
+
- Get access histories by footprinter model
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
### Rails 3.2 and 4.0+
|
|
14
|
+
|
|
15
|
+
Add this line to your application's Gemfile:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
gem 'acts_as_footprintable', '~> 0.1.0'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
And then execute:
|
|
22
|
+
|
|
23
|
+
$ bundle install
|
|
24
|
+
|
|
25
|
+
Or install it yourself as:
|
|
26
|
+
|
|
27
|
+
$ gem install acts_as_footprintable
|
|
28
|
+
|
|
29
|
+
### Database Migrations
|
|
30
|
+
|
|
31
|
+
Acts As Footprintable uses a footprints table to store all footprints information.
|
|
32
|
+
To generate and run the migration just use.
|
|
33
|
+
|
|
34
|
+
$ rails generate acts_as_footprintable:migration
|
|
35
|
+
$ rake db:migrate
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
### Footprintable models
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
class Post < ActiveRecord::Base
|
|
43
|
+
acts_as_footprintable
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
@post = Post.create(:name => 'my post!')
|
|
47
|
+
|
|
48
|
+
@post.leave_footprints @user
|
|
49
|
+
@post.footprints.size # => 1
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Footprintable model access ranking
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
# Total access ranking
|
|
56
|
+
total_ranking = Post.access_ranking
|
|
57
|
+
|
|
58
|
+
# Span access ranking
|
|
59
|
+
monthly_ranking = Post.access_ranking(1.month.ago.beginning_of_month..1.month.ago.end_of_month)
|
|
60
|
+
|
|
61
|
+
# Limit access ranking
|
|
62
|
+
monthly_top10_ranking = Post.access_ranking(1.month.ago.beginning_of_month..1.month.ago.end_of_month, 10)
|
|
63
|
+
# => {footprintable_id => count, ...}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Footprinter models
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
class User < ActiveRecord::Base
|
|
70
|
+
acts_as_footprinter
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
@user.leave_footprints @post
|
|
74
|
+
@post.footprints.size # => 1
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Footprinter model access histories
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
# Total access histories
|
|
81
|
+
total_access_histories = @user.access_histories
|
|
82
|
+
# Limited total access histories
|
|
83
|
+
total_access_histories = @user.access_histories(10)
|
|
84
|
+
|
|
85
|
+
# Post access histories
|
|
86
|
+
post_access_histories = @user.access_histories_for Post
|
|
87
|
+
# Limited Post access histories
|
|
88
|
+
post_access_histories = @user.access_histories_for(Post, 10)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Testing
|
|
92
|
+
|
|
93
|
+
All tests follow the RSpec format and located in the spec directory.
|
|
94
|
+
They can be run with:
|
|
95
|
+
|
|
96
|
+
$ rake spec
|
|
97
|
+
|
|
98
|
+
## Contributing
|
|
99
|
+
|
|
100
|
+
1. Fork it
|
|
101
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
102
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
103
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
104
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'acts_as_footprintable/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "acts_as_footprintable"
|
|
8
|
+
spec.version = ActsAsFootprintable::VERSION
|
|
9
|
+
spec.author = "Toyoaki Oko"
|
|
10
|
+
spec.email = "chariderpato@gmail.com"
|
|
11
|
+
spec.description = %q{Rails gem to allowing records to leave footprints}
|
|
12
|
+
spec.summary = %q{Rails gem to allowing records to leave footprints}
|
|
13
|
+
spec.homepage = "https://github.com/patorash/acts_as_footprintable"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($/)
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
|
22
|
+
spec.add_development_dependency 'rake'
|
|
23
|
+
spec.add_development_dependency 'rspec'
|
|
24
|
+
spec.add_development_dependency 'sqlite3'
|
|
25
|
+
spec.add_development_dependency 'timecop'
|
|
26
|
+
spec.add_development_dependency 'database_cleaner', "~> 1.0.1"
|
|
27
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require 'active_record'
|
|
2
|
+
require 'active_support/inflector'
|
|
3
|
+
|
|
4
|
+
module ActsAsFootprintable
|
|
5
|
+
if defined?(ActiveRecord::Base)
|
|
6
|
+
require 'acts_as_footprintable/extenders/footprintable'
|
|
7
|
+
require 'acts_as_footprintable/extenders/footprinter'
|
|
8
|
+
require 'acts_as_footprintable/footprint'
|
|
9
|
+
ActiveRecord::Base.extend ActsAsFootprintable::Extenders::Footprintable
|
|
10
|
+
ActiveRecord::Base.extend ActsAsFootprintable::Extenders::Footprinter
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
module ActsAsFootprintable
|
|
3
|
+
module Extenders
|
|
4
|
+
module Footprintable
|
|
5
|
+
def footprintable?
|
|
6
|
+
false
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def acts_as_footprintable
|
|
10
|
+
require 'acts_as_footprintable/footprintable'
|
|
11
|
+
include ActsAsFootprintable::Footprintable
|
|
12
|
+
|
|
13
|
+
class_eval do
|
|
14
|
+
def self.footprintable?
|
|
15
|
+
true
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.access_ranking(range=nil, limit=nil)
|
|
19
|
+
records = Footprint.for_type(self)
|
|
20
|
+
records = records.where(:created_at => range) unless range.nil?
|
|
21
|
+
records = records.limit(limit) unless limit.nil?
|
|
22
|
+
records.group(:footprintable_id).order('count_footprintable_id desc').count(:footprintable_id)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
module ActsAsFootprintable
|
|
3
|
+
module Extenders
|
|
4
|
+
module Footprinter
|
|
5
|
+
def footprinter?
|
|
6
|
+
false
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def acts_as_footprinter(*args)
|
|
10
|
+
require 'acts_as_footprintable/footprinter'
|
|
11
|
+
include ActsAsFootprintable::Footprinter
|
|
12
|
+
|
|
13
|
+
class_eval do
|
|
14
|
+
def self.footprinter?
|
|
15
|
+
true
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
module ActsAsFootprintable
|
|
3
|
+
class Footprint < ::ActiveRecord::Base
|
|
4
|
+
if ::ActiveRecord::VERSION::MAJOR < 4
|
|
5
|
+
attr_accessible :footprintable_id, :footprintable_type,
|
|
6
|
+
:footprinter_id, :footprinter_type,
|
|
7
|
+
:footprintable, :footprinter
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
belongs_to :footprintable, :polymorphic => true
|
|
11
|
+
belongs_to :footprinter, :polymorphic => true
|
|
12
|
+
|
|
13
|
+
scope :for_type, lambda{|klass| where(:footprintable_type => klass)}
|
|
14
|
+
scope :by_type, lambda{|klass| where(:footprinter_type => klass)}
|
|
15
|
+
|
|
16
|
+
validates :footprintable_id, :presence => true
|
|
17
|
+
validates :footprinter_id, :presence => true
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
module ActsAsFootprintable
|
|
3
|
+
module Footprintable
|
|
4
|
+
|
|
5
|
+
def self.included(base)
|
|
6
|
+
base.class_eval do
|
|
7
|
+
has_many :footprints, :class_name => 'ActsAsFootprintable::Footprint', :as => :footprintable, :dependent => :destroy do
|
|
8
|
+
def footprinters
|
|
9
|
+
includes(:footprinter).map(&:footprinter)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def leave_footprints(footprinter)
|
|
16
|
+
footprint = ActsAsFootprintable::Footprint.new(:footprintable => self, :footprinter => footprinter)
|
|
17
|
+
if footprint.save
|
|
18
|
+
true
|
|
19
|
+
else
|
|
20
|
+
false
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def footprint_count
|
|
25
|
+
footprints.count
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def footprint_count_between(range)
|
|
29
|
+
footprints.where(:created_at => range).count
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
module ActsAsFootprintable
|
|
3
|
+
module Footprinter
|
|
4
|
+
def self.included(base)
|
|
5
|
+
base.class_eval do
|
|
6
|
+
has_many :footprints, :class_name => 'ActsAsFootprintable::Footprint', :as => :footprinter, :dependent => :destroy do
|
|
7
|
+
def footprintable
|
|
8
|
+
includes(:footprintable).map(&:footprintable)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def leave_footprints(footprintable)
|
|
15
|
+
footprint = ActsAsFootprintable::Footprint.new(:footprintable => footprintable, :footprinter => self)
|
|
16
|
+
if footprint.save
|
|
17
|
+
true
|
|
18
|
+
else
|
|
19
|
+
false
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def access_histories_for(klass, limit=nil)
|
|
24
|
+
get_access_history_records(limit) do
|
|
25
|
+
footprints.for_type(klass).group('footprintable_id').having('MAX(created_at)').pluck(:id)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def access_histories(limit=nil)
|
|
30
|
+
get_access_history_records(limit) do
|
|
31
|
+
footprints.group('footprintable_id, footprintable_type').having('MAX(created_at)').pluck(:id)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
def get_access_history_records(limit=nil)
|
|
37
|
+
records = footprints.where(:id => yield).order("footprints.created_at desc")
|
|
38
|
+
records = records.limit(limit) unless limit.nil?
|
|
39
|
+
records.map{|footprint| footprint.footprintable}
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
require 'rails/generators/migration'
|
|
3
|
+
|
|
4
|
+
module ActsAsFootprintable
|
|
5
|
+
class MigrationGenerator < Rails::Generators::Base
|
|
6
|
+
include Rails::Generators::Migration
|
|
7
|
+
|
|
8
|
+
desc "Generators migration for footprintable(footprints table)"
|
|
9
|
+
|
|
10
|
+
def self.orm
|
|
11
|
+
Rails::Generators.options[:rails][:orm]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.source_root
|
|
15
|
+
File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.orm_has_migration?
|
|
19
|
+
[:active_record].include? orm
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.next_migration_number(path)
|
|
23
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def create_migration_file
|
|
27
|
+
if self.class.orm_has_migration?
|
|
28
|
+
migration_template 'migration.rb', 'db/migrate/acts_as_footprintable_migration'
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
class ActsAsFootprintableMigration < ActiveRecord::Migration
|
|
3
|
+
def self.up
|
|
4
|
+
create_table :footprints do |t|
|
|
5
|
+
t.references :footprintable, :polymorphic => true
|
|
6
|
+
t.references :footprinter, :polymorphic => true
|
|
7
|
+
t.timestamps
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
if ActiveRecord::VERSION::MAJOR < 4
|
|
11
|
+
add_index :footprints, [:footprintable_id, :footprintable_type]
|
|
12
|
+
add_index :footprints, [:footprinter_id, :footprinter_type]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.down
|
|
17
|
+
drop_table :footprints
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
require 'acts_as_footprintable'
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe ActsAsFootprintable::Footprintable do
|
|
6
|
+
|
|
7
|
+
it "should not be a footprintable" do
|
|
8
|
+
NotFootprintable.should_not be_footprintable
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "should be a footprintable" do
|
|
12
|
+
Footprintable.should be_footprintable
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
describe 'leave footprints by footprinter' do
|
|
16
|
+
|
|
17
|
+
before do
|
|
18
|
+
@user = User.create!(:name => 'i can footprint!')
|
|
19
|
+
@user2 = User.create!(:name => 'a new person')
|
|
20
|
+
|
|
21
|
+
@footprintable = Footprintable.create!(:name => 'a footprinting model')
|
|
22
|
+
@footprintable2 = Footprintable.create!(:name => 'a 2nd footprinting model')
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "should be leave footprints" do
|
|
26
|
+
@footprintable.leave_footprints(@user).should be_true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "足跡の数が増えていること" do
|
|
30
|
+
expect {
|
|
31
|
+
@footprintable.leave_footprints @user
|
|
32
|
+
}.to change{ @footprintable.footprint_count }.from(0).to(1)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "10回アクセスしたら10になること" do
|
|
36
|
+
expect {
|
|
37
|
+
10.times { @footprintable.leave_footprints @user }
|
|
38
|
+
}.to change{ @footprintable.footprint_count }.from(0).to(10)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "複数人でアクセスしたら合計されること" do
|
|
42
|
+
expect {
|
|
43
|
+
5.times { @footprintable.leave_footprints @user }
|
|
44
|
+
5.times { @footprintable.leave_footprints @user2 }
|
|
45
|
+
}.to change{ @footprintable.footprint_count }.from(0).to(10)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
describe "期間指定をする" do
|
|
49
|
+
before do
|
|
50
|
+
(1..30).each do |day|
|
|
51
|
+
Timecop.travel(Time.parse("2013/9/#{day}")) do
|
|
52
|
+
5.times {@footprintable.leave_footprints @user}
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
context "1週間の場合" do
|
|
58
|
+
it "35の足跡があること" do
|
|
59
|
+
Timecop.travel(Time.parse("2013/9/30 10:00:00")) do
|
|
60
|
+
@footprintable.footprint_count_between(1.week.ago..Time.now).should == 35
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
context "1ヶ月の場合" do
|
|
66
|
+
it "150の足跡があること" do
|
|
67
|
+
Timecop.travel(Time.parse("2013/9/30 10:00:00")) do
|
|
68
|
+
@footprintable.footprint_count_between(1.month.ago..Time.now).should == 150
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
describe "アクセスランキングを作成" do
|
|
76
|
+
before do
|
|
77
|
+
@user = User.create!(:name => 'i can footprint!')
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
context "件数と期間を制限" do
|
|
81
|
+
before do
|
|
82
|
+
(1..30).each do |index|
|
|
83
|
+
Timecop.travel(Time.parse("2013/9/#{index}")) do
|
|
84
|
+
footprintable = Footprintable.create!(:name => "Footprintable#{index}")
|
|
85
|
+
index.times {footprintable.leave_footprints @user}
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
subject do
|
|
90
|
+
month = Time.new(2013,9,1)
|
|
91
|
+
Footprintable.access_ranking(month.beginning_of_month...1.week.since(month), 5)
|
|
92
|
+
end
|
|
93
|
+
it { should == {7 => 7, 6 => 6, 5 => 5, 4 => 4, 3 => 3 }}
|
|
94
|
+
it { should have(5).items }
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require 'acts_as_footprintable'
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
|
|
4
|
+
describe ActsAsFootprintable::Footprinter do
|
|
5
|
+
|
|
6
|
+
it "should not be a footprinter" do
|
|
7
|
+
NotUser.should_not be_footprinter
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "should be a footprinter" do
|
|
11
|
+
User.should be_footprinter
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "ユーザーのアクセス履歴を" do
|
|
15
|
+
before do
|
|
16
|
+
@user = User.create!(:name => "user")
|
|
17
|
+
(1..5).each do |index|
|
|
18
|
+
footprintable = Footprintable.create!(:name => "footprintable#{index}")
|
|
19
|
+
second_footprintable = SecondFootprintable.create!(:name => "second_footprintable#{index}")
|
|
20
|
+
3.times do
|
|
21
|
+
footprintable.leave_footprints @user
|
|
22
|
+
second_footprintable.leave_footprints @user
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
context "対象のモデル毎に" do
|
|
28
|
+
it "取得できること" do
|
|
29
|
+
@user.access_histories_for(Footprintable).should have(5).items
|
|
30
|
+
@user.access_histories_for(Footprintable).map{|footprintable| footprintable.name}.should == (1..5).to_a.reverse.map{|index| "footprintable#{index}"}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "件数を絞り込めること" do
|
|
34
|
+
@user.access_histories_for(Footprintable, 3).should have(3).items
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
context "全てのモデルを通じて" do
|
|
39
|
+
it "取得できること" do
|
|
40
|
+
@user.access_histories.should have(10).items
|
|
41
|
+
@user.access_histories.map{|footprintable| footprintable.name}.should == (1..5).to_a.reverse.inject([]) do |results, index|
|
|
42
|
+
results.push "second_footprintable#{index}"
|
|
43
|
+
results.push "footprintable#{index}"
|
|
44
|
+
results
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it "件数を絞り込める事" do
|
|
49
|
+
@user.access_histories(3).should have(3).items
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
|
3
|
+
require 'sqlite3'
|
|
4
|
+
require 'timecop'
|
|
5
|
+
require 'database_cleaner'
|
|
6
|
+
require 'acts_as_footprintable'
|
|
7
|
+
|
|
8
|
+
RSpec.configure do |config|
|
|
9
|
+
config.before :suite do
|
|
10
|
+
DatabaseCleaner.strategy = :truncation
|
|
11
|
+
end
|
|
12
|
+
config.before :each do
|
|
13
|
+
DatabaseCleaner.start
|
|
14
|
+
end
|
|
15
|
+
config.after :each do
|
|
16
|
+
DatabaseCleaner.clean
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
|
21
|
+
|
|
22
|
+
ActiveRecord::Schema.define(:version => 1) do
|
|
23
|
+
create_table :footprints do |t|
|
|
24
|
+
t.references :footprintable, :polymorphic => true
|
|
25
|
+
t.references :footprinter, :polymorphic => true
|
|
26
|
+
t.timestamps
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
add_index :footprints, [:footprintable_id, :footprintable_type]
|
|
30
|
+
add_index :footprints, [:footprinter_id, :footprinter_type]
|
|
31
|
+
|
|
32
|
+
create_table :users do |t|
|
|
33
|
+
t.string :name
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
create_table :not_users do |t|
|
|
37
|
+
t.string :name
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
create_table :footprintables do |t|
|
|
41
|
+
t.string :name
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
create_table :second_footprintables do |t|
|
|
45
|
+
t.string :name
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
create_table :not_footprintables do |t|
|
|
49
|
+
t.string :name
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
class User < ActiveRecord::Base
|
|
54
|
+
acts_as_footprinter
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class NotUser < ActiveRecord::Base
|
|
58
|
+
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
class Footprintable < ActiveRecord::Base
|
|
62
|
+
acts_as_footprintable
|
|
63
|
+
validates :name, :presence => true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
class SecondFootprintable < ActiveRecord::Base
|
|
67
|
+
acts_as_footprintable
|
|
68
|
+
validates :name, :presence => true
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
class NotFootprintable < ActiveRecord::Base
|
|
72
|
+
validates :name, :presence => true
|
|
73
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: acts_as_footprintable
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Toyoaki Oko
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-10-03 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.3'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.3'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - '>='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - '>='
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: sqlite3
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - '>='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - '>='
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: timecop
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - '>='
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - '>='
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: database_cleaner
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ~>
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 1.0.1
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ~>
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 1.0.1
|
|
97
|
+
description: Rails gem to allowing records to leave footprints
|
|
98
|
+
email: chariderpato@gmail.com
|
|
99
|
+
executables: []
|
|
100
|
+
extensions: []
|
|
101
|
+
extra_rdoc_files: []
|
|
102
|
+
files:
|
|
103
|
+
- .gitignore
|
|
104
|
+
- .rspec
|
|
105
|
+
- Gemfile
|
|
106
|
+
- LICENSE.txt
|
|
107
|
+
- README.md
|
|
108
|
+
- Rakefile
|
|
109
|
+
- acts_as_footprintable.gemspec
|
|
110
|
+
- lib/acts_as_footprintable.rb
|
|
111
|
+
- lib/acts_as_footprintable/extenders/footprintable.rb
|
|
112
|
+
- lib/acts_as_footprintable/extenders/footprinter.rb
|
|
113
|
+
- lib/acts_as_footprintable/footprint.rb
|
|
114
|
+
- lib/acts_as_footprintable/footprintable.rb
|
|
115
|
+
- lib/acts_as_footprintable/footprinter.rb
|
|
116
|
+
- lib/acts_as_footprintable/version.rb
|
|
117
|
+
- lib/generators/acts_as_footprintable/migration/migration_generator.rb
|
|
118
|
+
- lib/generators/acts_as_footprintable/migration/templates/active_record/migration.rb
|
|
119
|
+
- spec/footprintable_spec.rb
|
|
120
|
+
- spec/footprinter_spec.rb
|
|
121
|
+
- spec/spec_helper.rb
|
|
122
|
+
homepage: https://github.com/patorash/acts_as_footprintable
|
|
123
|
+
licenses:
|
|
124
|
+
- MIT
|
|
125
|
+
metadata: {}
|
|
126
|
+
post_install_message:
|
|
127
|
+
rdoc_options: []
|
|
128
|
+
require_paths:
|
|
129
|
+
- lib
|
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
131
|
+
requirements:
|
|
132
|
+
- - '>='
|
|
133
|
+
- !ruby/object:Gem::Version
|
|
134
|
+
version: '0'
|
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
|
+
requirements:
|
|
137
|
+
- - '>='
|
|
138
|
+
- !ruby/object:Gem::Version
|
|
139
|
+
version: '0'
|
|
140
|
+
requirements: []
|
|
141
|
+
rubyforge_project:
|
|
142
|
+
rubygems_version: 2.0.5
|
|
143
|
+
signing_key:
|
|
144
|
+
specification_version: 4
|
|
145
|
+
summary: Rails gem to allowing records to leave footprints
|
|
146
|
+
test_files:
|
|
147
|
+
- spec/footprintable_spec.rb
|
|
148
|
+
- spec/footprinter_spec.rb
|
|
149
|
+
- spec/spec_helper.rb
|