browsing_history 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +89 -0
  5. data/.rubocop_todo.yml +28 -0
  6. data/.ruby-version +1 -0
  7. data/Gemfile +7 -0
  8. data/Gemfile.lock +180 -0
  9. data/LICENSE +21 -0
  10. data/MIT-LICENSE +20 -0
  11. data/README.md +2 -0
  12. data/README.rdoc +3 -0
  13. data/Rakefile +28 -0
  14. data/browsing_history.gemspec +34 -0
  15. data/dump.rdb +1 -0
  16. data/lib/browsing_history/browser.rb +66 -0
  17. data/lib/browsing_history/configuration.rb +38 -0
  18. data/lib/browsing_history/error.rb +2 -0
  19. data/lib/browsing_history/historizable.rb +26 -0
  20. data/lib/browsing_history/history.rb +85 -0
  21. data/lib/browsing_history/storage.rb +44 -0
  22. data/lib/browsing_history/storages/active_record.rb +12 -0
  23. data/lib/browsing_history/storages/base.rb +40 -0
  24. data/lib/browsing_history/storages/redis.rb +113 -0
  25. data/lib/browsing_history/version.rb +3 -0
  26. data/lib/browsing_history.rb +12 -0
  27. data/lib/generators/active_record/migration_generator.rb +12 -0
  28. data/lib/generators/active_record/templates/migration.rb +15 -0
  29. data/lib/tasks/browsing_history_tasks.rake +4 -0
  30. data/spec/browsing_history/browser_spec.rb +156 -0
  31. data/spec/browsing_history/configration_spec.rb +16 -0
  32. data/spec/browsing_history/fixtures/article.rb +10 -0
  33. data/spec/browsing_history/fixtures/user.rb +10 -0
  34. data/spec/browsing_history/historizable_spec.rb +27 -0
  35. data/spec/browsing_history/history_spec.rb +273 -0
  36. data/spec/browsing_history/storage_spec.rb +45 -0
  37. data/spec/browsing_history/storages/base_spec.rb +35 -0
  38. data/spec/browsing_history/storages/redis_spec.rb +141 -0
  39. data/spec/browsing_history_spec.rb +4 -0
  40. data/spec/generator_spec.rb +34 -0
  41. data/spec/spec_helper.rb +40 -0
  42. data/spec/test_app/README.rdoc +28 -0
  43. data/spec/test_app/Rakefile +6 -0
  44. data/spec/test_app/app/assets/images/.keep +0 -0
  45. data/spec/test_app/app/assets/javascripts/application.js +13 -0
  46. data/spec/test_app/app/assets/stylesheets/application.css +15 -0
  47. data/spec/test_app/app/controllers/application_controller.rb +10 -0
  48. data/spec/test_app/app/controllers/articles_controller.rb +17 -0
  49. data/spec/test_app/app/controllers/concerns/.keep +0 -0
  50. data/spec/test_app/app/helpers/application_helper.rb +2 -0
  51. data/spec/test_app/app/mailers/.keep +0 -0
  52. data/spec/test_app/app/models/.keep +0 -0
  53. data/spec/test_app/app/models/concerns/.keep +0 -0
  54. data/spec/test_app/app/views/layouts/application.html.erb +14 -0
  55. data/spec/test_app/bin/bundle +3 -0
  56. data/spec/test_app/bin/rails +4 -0
  57. data/spec/test_app/bin/rake +4 -0
  58. data/spec/test_app/bin/setup +29 -0
  59. data/spec/test_app/config/application.rb +19 -0
  60. data/spec/test_app/config/boot.rb +5 -0
  61. data/spec/test_app/config/database.yml +12 -0
  62. data/spec/test_app/config/environment.rb +5 -0
  63. data/spec/test_app/config/environments/development.rb +18 -0
  64. data/spec/test_app/config/environments/test.rb +21 -0
  65. data/spec/test_app/config/initializers/assets.rb +11 -0
  66. data/spec/test_app/config/initializers/backtrace_silencers.rb +7 -0
  67. data/spec/test_app/config/initializers/cookies_serializer.rb +3 -0
  68. data/spec/test_app/config/initializers/filter_parameter_logging.rb +4 -0
  69. data/spec/test_app/config/initializers/inflections.rb +16 -0
  70. data/spec/test_app/config/initializers/mime_types.rb +4 -0
  71. data/spec/test_app/config/initializers/session_store.rb +3 -0
  72. data/spec/test_app/config/initializers/wrap_parameters.rb +14 -0
  73. data/spec/test_app/config/locales/en.yml +23 -0
  74. data/spec/test_app/config/routes.rb +2 -0
  75. data/spec/test_app/config/secrets.yml +5 -0
  76. data/spec/test_app/config.ru +4 -0
  77. data/spec/test_app/db/development.sqlite3 +0 -0
  78. data/spec/test_app/db/test.sqlite3 +0 -0
  79. data/spec/test_app/lib/assets/.keep +0 -0
  80. data/spec/test_app/log/.keep +0 -0
  81. data/spec/test_app/log/development.log +0 -0
  82. data/spec/test_app/log/test.log +0 -0
  83. data/spec/test_app/public/404.html +67 -0
  84. data/spec/test_app/public/422.html +67 -0
  85. data/spec/test_app/public/500.html +66 -0
  86. data/spec/test_app/public/favicon.ico +0 -0
  87. metadata +342 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5579aa68af285099783e050c1f060c193d4e5761
4
+ data.tar.gz: 55d5a56f2ddcc0e2294febb4aa2ff1175ca85d42
5
+ SHA512:
6
+ metadata.gz: 665329c030a0ec2e17f6120516c315d864a5540169f720e0f50b75ad15f30e541f7cbeff4bbd237b03dddb9d2ef78a5015c460de51036841983965e4935c54fb
7
+ data.tar.gz: 1d56e955901f795dd373eedde90853c2aa0234bcde80f70a593685338f09a9a5ebc7dc926421c7c27543a15c73394fafaa8a139401ccb789c1c472f3b5ee60e2
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ *~
2
+ .DS_Store
3
+ .idea
4
+ coverage
5
+ rdoc
6
+ Gemfile.lock
7
+ TODO
8
+ .rspec
9
+ tags
10
+
11
+ .bundle/
12
+ log/
13
+ pkg/
14
+ .bundle/
15
+ tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -r spec_helper
2
+ -r pry
data/.rubocop.yml ADDED
@@ -0,0 +1,89 @@
1
+ inherit_from:
2
+ - ./.rubocop_todo.yml
3
+
4
+ AllCops:
5
+ Include:
6
+ - '**/Gemfile'
7
+ - '**/Rakefile'
8
+ - '**/config.ru'
9
+ Exclude:
10
+ - 'bin/**/*'
11
+ - 'db/**/*'
12
+ - 'config/**/*'
13
+ - !ruby/regexp /old_and_unused\.rb$/
14
+ - 'vendor/**/*'
15
+ - 'spec/test_app/**/*'
16
+ DisplayCopNames: true
17
+
18
+ Lint/UnderscorePrefixedVariableName:
19
+ Enabled: false
20
+
21
+ Metrics/AbcSize:
22
+ Max: 30
23
+
24
+ Metrics/ClassLength:
25
+ Max: 110
26
+
27
+ Metrics/CyclomaticComplexity:
28
+ Max: 6
29
+
30
+ Metrics/MethodLength:
31
+ Max: 20
32
+
33
+ Metrics/ParameterLists:
34
+ Max: 3
35
+ CountKeywordArgs: false
36
+
37
+ Metrics/PerceivedComplexity:
38
+ Max: 6
39
+
40
+ Style/AlignHash:
41
+ Enabled: false
42
+
43
+ Style/AlignParameters:
44
+ Enabled: false
45
+
46
+ Style/AndOr:
47
+ EnforcedStyle: always
48
+
49
+ Style/AsciiComments:
50
+ Enabled: false
51
+
52
+ Style/BracesAroundHashParameters:
53
+ EnforcedStyle: no_braces
54
+
55
+ Style/ClassAndModuleChildren:
56
+ EnforcedStyle: compact
57
+
58
+ Style/Documentation:
59
+ Enabled: false
60
+
61
+ Style/DoubleNegation:
62
+ Enabled: false
63
+
64
+ Style/DotPosition:
65
+ EnforcedStyle: leading
66
+
67
+ Style/GuardClause:
68
+ Enabled: false
69
+
70
+ Style/HashSyntax:
71
+ EnforcedStyle: ruby19
72
+
73
+ Style/IfUnlessModifier:
74
+ Enabled: false
75
+
76
+ Style/Lambda:
77
+ Enabled: false
78
+
79
+ Style/MultilineOperationIndentation:
80
+ Enabled: false
81
+
82
+ Style/PerlBackrefs:
83
+ Enabled: false
84
+
85
+ Style/SignalException:
86
+ Enabled: false
87
+
88
+ Style/SpaceInsideHashLiteralBraces:
89
+ EnforcedStyle: space
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,28 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2015-12-08 15:31:21 +0000 using RuboCop version 0.35.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ # Cop supports --auto-correct.
11
+ # Configuration parameters: AlignWith, SupportedStyles, AutoCorrect.
12
+ Lint/EndAlignment:
13
+ Enabled: false
14
+
15
+ # Offense count: 1
16
+ Metrics/CyclomaticComplexity:
17
+ Max: 6
18
+
19
+ # Offense count: 6
20
+ # Configuration parameters: AllowURI, URISchemes.
21
+ Metrics/LineLength:
22
+ Max: 103
23
+
24
+ # Offense count: 4
25
+ # Cop supports --auto-correct.
26
+ # Configuration parameters: IndentWhenRelativeTo, SupportedStyles, IndentOneStep.
27
+ Style/CaseIndentation:
28
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.3
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'sqlite3', platforms: :ruby
4
+ gem 'rails', '~>4.2'
5
+
6
+ gemspec
7
+
data/Gemfile.lock ADDED
@@ -0,0 +1,180 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ browsing_history (0.0.2)
5
+ redis (>= 3.0.2)
6
+ redis-namespace
7
+ redis-objects
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actionmailer (4.2.6)
13
+ actionpack (= 4.2.6)
14
+ actionview (= 4.2.6)
15
+ activejob (= 4.2.6)
16
+ mail (~> 2.5, >= 2.5.4)
17
+ rails-dom-testing (~> 1.0, >= 1.0.5)
18
+ actionpack (4.2.6)
19
+ actionview (= 4.2.6)
20
+ activesupport (= 4.2.6)
21
+ rack (~> 1.6)
22
+ rack-test (~> 0.6.2)
23
+ rails-dom-testing (~> 1.0, >= 1.0.5)
24
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
25
+ actionview (4.2.6)
26
+ activesupport (= 4.2.6)
27
+ builder (~> 3.1)
28
+ erubis (~> 2.7.0)
29
+ rails-dom-testing (~> 1.0, >= 1.0.5)
30
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
31
+ activejob (4.2.6)
32
+ activesupport (= 4.2.6)
33
+ globalid (>= 0.3.0)
34
+ activemodel (4.2.6)
35
+ activesupport (= 4.2.6)
36
+ builder (~> 3.1)
37
+ activerecord (4.2.6)
38
+ activemodel (= 4.2.6)
39
+ activesupport (= 4.2.6)
40
+ arel (~> 6.0)
41
+ activesupport (4.2.6)
42
+ i18n (~> 0.7)
43
+ json (~> 1.7, >= 1.7.7)
44
+ minitest (~> 5.1)
45
+ thread_safe (~> 0.3, >= 0.3.4)
46
+ tzinfo (~> 1.1)
47
+ arel (6.0.3)
48
+ ast (2.2.0)
49
+ builder (3.2.2)
50
+ byebug (8.2.4)
51
+ coderay (1.1.1)
52
+ concurrent-ruby (1.0.1)
53
+ database_cleaner (1.5.3)
54
+ diff-lcs (1.2.5)
55
+ erubis (2.7.0)
56
+ generator_spec (0.9.3)
57
+ activesupport (>= 3.0.0)
58
+ railties (>= 3.0.0)
59
+ globalid (0.3.6)
60
+ activesupport (>= 4.1.0)
61
+ i18n (0.7.0)
62
+ json (1.8.3)
63
+ loofah (2.0.3)
64
+ nokogiri (>= 1.5.9)
65
+ mail (2.6.4)
66
+ mime-types (>= 1.16, < 4)
67
+ method_source (0.8.2)
68
+ mime-types (3.0)
69
+ mime-types-data (~> 3.2015)
70
+ mime-types-data (3.2016.0221)
71
+ mini_portile2 (2.0.0)
72
+ minitest (5.8.4)
73
+ nokogiri (1.6.7.2)
74
+ mini_portile2 (~> 2.0.0.rc2)
75
+ parser (2.3.0.6)
76
+ ast (~> 2.2)
77
+ powerpack (0.1.1)
78
+ pry (0.10.3)
79
+ coderay (~> 1.1.0)
80
+ method_source (~> 0.8.1)
81
+ slop (~> 3.4)
82
+ pry-byebug (3.3.0)
83
+ byebug (~> 8.0)
84
+ pry (~> 0.10)
85
+ rack (1.6.4)
86
+ rack-test (0.6.3)
87
+ rack (>= 1.0)
88
+ rails (4.2.6)
89
+ actionmailer (= 4.2.6)
90
+ actionpack (= 4.2.6)
91
+ actionview (= 4.2.6)
92
+ activejob (= 4.2.6)
93
+ activemodel (= 4.2.6)
94
+ activerecord (= 4.2.6)
95
+ activesupport (= 4.2.6)
96
+ bundler (>= 1.3.0, < 2.0)
97
+ railties (= 4.2.6)
98
+ sprockets-rails
99
+ rails-deprecated_sanitizer (1.0.3)
100
+ activesupport (>= 4.2.0.alpha)
101
+ rails-dom-testing (1.0.7)
102
+ activesupport (>= 4.2.0.beta, < 5.0)
103
+ nokogiri (~> 1.6.0)
104
+ rails-deprecated_sanitizer (>= 1.0.1)
105
+ rails-html-sanitizer (1.0.3)
106
+ loofah (~> 2.0)
107
+ railties (4.2.6)
108
+ actionpack (= 4.2.6)
109
+ activesupport (= 4.2.6)
110
+ rake (>= 0.8.7)
111
+ thor (>= 0.18.1, < 2.0)
112
+ rainbow (2.1.0)
113
+ rake (11.1.2)
114
+ redis (3.3.0)
115
+ redis-namespace (1.5.2)
116
+ redis (~> 3.0, >= 3.0.4)
117
+ redis-objects (1.2.1)
118
+ redis (>= 3.0.2)
119
+ rspec (3.4.0)
120
+ rspec-core (~> 3.4.0)
121
+ rspec-expectations (~> 3.4.0)
122
+ rspec-mocks (~> 3.4.0)
123
+ rspec-core (3.4.4)
124
+ rspec-support (~> 3.4.0)
125
+ rspec-expectations (3.4.0)
126
+ diff-lcs (>= 1.2.0, < 2.0)
127
+ rspec-support (~> 3.4.0)
128
+ rspec-mocks (3.4.1)
129
+ diff-lcs (>= 1.2.0, < 2.0)
130
+ rspec-support (~> 3.4.0)
131
+ rspec-rails (3.4.2)
132
+ actionpack (>= 3.0, < 4.3)
133
+ activesupport (>= 3.0, < 4.3)
134
+ railties (>= 3.0, < 4.3)
135
+ rspec-core (~> 3.4.0)
136
+ rspec-expectations (~> 3.4.0)
137
+ rspec-mocks (~> 3.4.0)
138
+ rspec-support (~> 3.4.0)
139
+ rspec-support (3.4.1)
140
+ rubocop (0.37.2)
141
+ parser (>= 2.3.0.4, < 3.0)
142
+ powerpack (~> 0.1)
143
+ rainbow (>= 1.99.1, < 3.0)
144
+ ruby-progressbar (~> 1.7)
145
+ unicode-display_width (~> 0.3)
146
+ ruby-progressbar (1.7.5)
147
+ slop (3.6.0)
148
+ sprockets (3.6.0)
149
+ concurrent-ruby (~> 1.0)
150
+ rack (> 1, < 3)
151
+ sprockets-rails (3.0.4)
152
+ actionpack (>= 4.0)
153
+ activesupport (>= 4.0)
154
+ sprockets (>= 3.0.0)
155
+ sqlite3 (1.3.11)
156
+ thor (0.19.1)
157
+ thread_safe (0.3.5)
158
+ timecop (0.8.1)
159
+ tzinfo (1.2.2)
160
+ thread_safe (~> 0.1)
161
+ unicode-display_width (0.3.1)
162
+
163
+ PLATFORMS
164
+ ruby
165
+
166
+ DEPENDENCIES
167
+ browsing_history!
168
+ database_cleaner
169
+ generator_spec
170
+ pry
171
+ pry-byebug
172
+ rails (~> 4.2)
173
+ rspec
174
+ rspec-rails
175
+ rubocop
176
+ sqlite3
177
+ timecop
178
+
179
+ BUNDLED WITH
180
+ 1.11.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 cluex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 tkclimb
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.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # browsing_history
2
+ Record browsing history by browser like users or something more simple and graceful.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = BrowsingHistory
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'BrowsingHistory'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'lib'
23
+ t.libs << 'test'
24
+ t.pattern = 'test/**/*_test.rb'
25
+ t.verbose = false
26
+ end
27
+
28
+ task default: :test
@@ -0,0 +1,34 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+ require 'browsing_history/version'
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'browsing_history'
6
+ gem.version = BrowsingHistory::VERSION
7
+ gem.authors = %w(tkclimb)
8
+ gem.email = %w(tkclimb0911@gmail.com)
9
+ gem.homepage = 'https://github.com/cluex/browsing_history'
10
+ gem.summary = 'Record browsing history more simple and graceful.'
11
+ gem.description = <<-EOS
12
+ Record browsing history by browser like users or something.
13
+ it make that more simple and graceful as possible.
14
+ EOS
15
+ gem.license = 'MIT'
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.test_files = `git ls-files -- spec`.split("\n")
19
+ gem.require_paths = %w(lib)
20
+
21
+ gem.add_dependency 'redis', '>= 3.0.2'
22
+ gem.add_dependency 'redis-objects'
23
+ gem.add_dependency 'redis-namespace'
24
+
25
+ gem.add_development_dependency 'rspec'
26
+ gem.add_development_dependency 'rspec-rails'
27
+ gem.add_development_dependency 'generator_spec'
28
+ gem.add_development_dependency 'database_cleaner'
29
+
30
+ gem.add_development_dependency 'pry'
31
+ gem.add_development_dependency 'pry-byebug'
32
+ gem.add_development_dependency 'rubocop'
33
+ gem.add_development_dependency 'timecop'
34
+ end
data/dump.rdb ADDED
@@ -0,0 +1 @@
1
+ REDIS0006�ܳC�Z��V
@@ -0,0 +1,66 @@
1
+ module BrowsingHistory::Browser
2
+ def browsing_histories
3
+ @browsing_histories ||= Association.new(self)
4
+ end
5
+
6
+ def self.included(klass)
7
+ klass.include IdentifyMethods
8
+ klass.extend IdentifyMethods
9
+ end
10
+
11
+ module IdentifyMethods
12
+ def browser_id
13
+ browser_instance? ? id : nil
14
+ end
15
+
16
+ def browser_type
17
+ browser_instance? ? self.class.to_s : to_s
18
+ end
19
+
20
+ def browser_class
21
+ browser_instance? ? self.class : self
22
+ end
23
+
24
+ def browser_instance?
25
+ !instance_of?(Object)
26
+ end
27
+ end
28
+
29
+ class Association
30
+ def initialize(browser)
31
+ @browser = browser
32
+ end
33
+
34
+ def recent(historizable, limit: 20)
35
+ BrowsingHistory::History.where(
36
+ browser: @browser,
37
+ historizable: historizable,
38
+ limit: limit
39
+ )
40
+ end
41
+
42
+ def previous(historizable, between, limit: 20)
43
+ BrowsingHistory::History.where(
44
+ browser: @browser,
45
+ historizable: historizable,
46
+ between: between,
47
+ limit: limit
48
+ )
49
+ end
50
+
51
+ def add(historizable)
52
+ BrowsingHistory::History.create(
53
+ browser: @browser,
54
+ historizable: historizable
55
+ )
56
+ end
57
+ alias << add
58
+
59
+ def count(historizable)
60
+ BrowsingHistory::History.count(
61
+ browser: @browser,
62
+ historizable: historizable
63
+ )
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,38 @@
1
+ module BrowsingHistory::Configuration
2
+ # storage type
3
+ attr_writer :storage_types
4
+
5
+ # BrowsingHistory namespace
6
+ attr_writer :namespace
7
+
8
+ # cofigure method
9
+ def configure
10
+ yield self
11
+ end
12
+
13
+ # BrowsingHistory storage_types
14
+ #
15
+ # @return the BrowsingHistory storage. default of :redis if not set.
16
+ def storage_types
17
+ @storage_types ||= %i(redis active_record)
18
+ end
19
+
20
+ # BrowsingHistory storage
21
+ #
22
+ # @return the BrowsingHistory storage. default of :redis if not set.
23
+ def storage_type
24
+ @storage_type ||= storage_types.first
25
+ end
26
+
27
+ def storage_type=(type)
28
+ return false unless storage_types.incude?(type)
29
+ @storage_type = type
30
+ end
31
+
32
+ # BrowsingHistory namespace for Redis.
33
+ #
34
+ # @return the BrowsingHistory namespace. default of 'activity_feed' if not set.
35
+ def namespace
36
+ @namespace ||= 'browsing_history'
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ class BrowsingHistory::Error < StandardError
2
+ end
@@ -0,0 +1,26 @@
1
+ module BrowsingHistory::Historizable
2
+ def self.included(klass)
3
+ klass.include IdentifyMethods
4
+ klass.extend IdentifyMethods
5
+ end
6
+
7
+ module IdentifyMethods
8
+ attr_accessor :target_storage
9
+
10
+ def historizable_id
11
+ historizable_instance? ? id : nil
12
+ end
13
+
14
+ def historizable_type
15
+ historizable_instance? ? self.class.to_s : to_s
16
+ end
17
+
18
+ def historizable_class
19
+ historizable_instance? ? self.class : self
20
+ end
21
+
22
+ def historizable_instance?
23
+ !instance_of?(Object)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,85 @@
1
+ require 'active_model'
2
+ require 'browsing_history/storage'
3
+
4
+ class BrowsingHistory::History
5
+ extend ActiveModel::Callbacks
6
+ include ActiveModel::Model
7
+ include ActiveModel::Validations
8
+
9
+ include BrowsingHistory::Storage
10
+
11
+ class ArgumentError < BrowsingHistory::Error; end
12
+
13
+ attr_accessor :browser, :historizable, :expiration
14
+
15
+ define_model_callbacks :save
16
+
17
+ after_save :save_to_storage
18
+
19
+ validate :valid_browser, :valid_historizable
20
+
21
+ def initialize(browser: nil, historizable: nil)
22
+ @browser = browser
23
+ @historizable = historizable
24
+ end
25
+
26
+ def save
27
+ valid? ? run_callbacks(:save) { true } : false
28
+ end
29
+
30
+ def save!
31
+ valid? ? run_callbacks(:save) { true } : raise_errors
32
+ end
33
+
34
+ class << self
35
+ def create(attributes = {})
36
+ instance = new(**attributes)
37
+ instance.save ? instance : false
38
+ end
39
+
40
+ def create!(attributes = {})
41
+ instance = new(**attributes)
42
+ instance.save!
43
+ instance
44
+ end
45
+
46
+ def where(browser: nil, historizable: nil, **opts)
47
+ return [] if !browser && !historizable
48
+
49
+ current_storage.fetch(browser, historizable, opts)
50
+ end
51
+
52
+ def count(browser: nil, historizable: nil)
53
+ return 0 if !browser && !historizable
54
+ current_storage.count(browser, historizable)
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def save_to_storage
61
+ self.class.current_storage.add(browser, historizable)
62
+ end
63
+
64
+ def valid_browser
65
+ if !browser.respond_to?(:browser_id) &&
66
+ !browser.respond_to?(:browser_type)
67
+ errors.add :browser, ':browser in args is not browser!!'
68
+ end
69
+ end
70
+
71
+ def valid_historizable
72
+ if !historizable.respond_to?(:historizable_id) &&
73
+ !historizable.respond_to?(:historizable_type)
74
+ errors.add :historizable, ':historizable in args is not historizable!!'
75
+ end
76
+ end
77
+
78
+ def raise_errors
79
+ errors.each do |_, message|
80
+ if message =~ /args/
81
+ raise ArgumentError, message
82
+ end
83
+ end
84
+ end
85
+ end