impressionist 1.4.4 → 1.4.5
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 +4 -4
- data/app/models/impressionist/impressionable.rb +2 -2
- data/lib/impressionist/counter_cache.rb +29 -33
- data/lib/impressionist/is_impressionable.rb +23 -0
- data/lib/impressionist/load.rb +4 -2
- data/lib/impressionist/models/active_record/impression.rb +3 -5
- data/lib/impressionist/models/active_record/impressionist/impressionable.rb +6 -20
- data/lib/impressionist/models/mongoid/impressionist/impressionable.rb +8 -24
- data/lib/impressionist/update_counters.rb +53 -47
- data/lib/impressionist/version.rb +1 -1
- data/tests/test_app/config/initializers/impression.rb +5 -2
- data/tests/test_app/db/migrate/20130719024021_create_impressions_table.rb +30 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81262fb9bc635b7807f03083d9f0a397f8f1d63f
|
4
|
+
data.tar.gz: f9db8dc841a204fed13f6394ac11f03de8b6534b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e06ea3557bac54ffafb0e067193ede993654a37a6be75cf012c05b8145fd3d73448df69517e860e8d89a50581421699ef0e3536c868556551175280c4ae4c4f3
|
7
|
+
data.tar.gz: b3dc48b55a06c462e11df5d293594fb48d5607779f98be349970b78f969a21efdbfb0577d1a09ae904cac2d79e9ecaab0abc4da6a2fb7b9c6f169f0896b6291b
|
@@ -12,7 +12,7 @@ module Impressionist
|
|
12
12
|
@impressionist_cache_options.reverse_merge!(DEFAULT_CACHE)
|
13
13
|
end
|
14
14
|
|
15
|
-
#
|
15
|
+
# asks impressionable entity whether or not it is counter_caching
|
16
16
|
def impressionist_counter_caching?
|
17
17
|
impressionist_counter_cache_options[:counter_cache]
|
18
18
|
end
|
@@ -22,7 +22,7 @@ module Impressionist
|
|
22
22
|
impressionist_counter_caching?
|
23
23
|
end
|
24
24
|
|
25
|
-
end
|
25
|
+
end # end of ClassMethods
|
26
26
|
|
27
27
|
# ------------------------------------------
|
28
28
|
# TODO: CLEAN UP, make it HUMAN readable
|
@@ -1,55 +1,34 @@
|
|
1
|
+
# Note
|
2
|
+
# It is only updatable if
|
3
|
+
# impressionist_id && impressionist_type(class) are present &&
|
4
|
+
# impressionable_class(which is imp_type.constantize) is counter_caching
|
5
|
+
# Defined like so
|
6
|
+
# is_impressionable :counter_cache => true
|
7
|
+
|
1
8
|
module Impressionist
|
2
9
|
module CounterCache
|
3
10
|
|
4
11
|
attr_reader :impressionable_class, :entity
|
5
12
|
|
6
13
|
private
|
7
|
-
LOG_MESSAGE = "Can't find impressionable_type or impressionable_id. Will not update_counters!"
|
8
14
|
|
9
15
|
# if updatable returns true, it must be qualified to update_counters
|
10
|
-
# Therefore there's no need to validate again
|
11
16
|
# impressionable_class instance var is set when updatable? is called
|
12
17
|
def impressionable_counter_cache_updatable?
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
def update_impression_cache
|
17
|
-
impressionable_find
|
18
|
-
impressionable_try
|
18
|
+
updatable? && impressionable_try
|
19
19
|
end
|
20
20
|
|
21
|
-
# asks imp_id whether it's present or not
|
22
|
-
#
|
23
|
-
# all should be true, so that it is updatable
|
21
|
+
# asks imp_id && imp_class whether it's present or not
|
22
|
+
# so that it is updatable
|
24
23
|
def updatable?
|
25
24
|
@impressionable_class = impressionable_class_set
|
26
25
|
impressionable_valid?
|
27
26
|
end
|
28
27
|
|
29
|
-
# imps_type == nil, constantize returns Object
|
30
|
-
# Therefore it attemps to return false so it won't be updatable
|
31
|
-
# calls to_s otherwise it would try to constantize nil
|
32
|
-
# and it would raise an exeception..
|
33
|
-
def impressionable_class_set
|
34
|
-
_type_ = self.impressionable_type.to_s.constantize
|
35
|
-
((_type_.to_s !~ /Object/) && _type_.impressionist_counter_caching? ? _type_ : false)
|
36
|
-
end
|
37
|
-
|
38
|
-
# Either true or false
|
39
|
-
# needs true to be updatable
|
40
28
|
def impressionable_valid?
|
41
|
-
(self.impressionable_id.present? && impressionable_class
|
42
|
-
end
|
43
|
-
|
44
|
-
# Logs to log file, expects a message to be passed
|
45
|
-
|
46
|
-
# default mode is ERROR
|
47
|
-
# ruby 1.8.7 support
|
48
|
-
def impressionist_log(str, mode=:error)
|
49
|
-
Rails.logger.send(mode.to_s, str)
|
29
|
+
(self.impressionable_id.present? && impressionable_class)
|
50
30
|
end
|
51
31
|
|
52
|
-
# read it out and LOUD
|
53
32
|
def impressionable_find
|
54
33
|
exeception_rescuer {
|
55
34
|
@entity = impressionable_class.find(self.impressionable_id)
|
@@ -58,10 +37,27 @@ module Impressionist
|
|
58
37
|
|
59
38
|
end
|
60
39
|
|
40
|
+
# imps_type == nil, constantize returns Object
|
41
|
+
# It attemps to return false so it won't be updatable
|
42
|
+
# calls to_s otherwise it would try to constantize nil
|
43
|
+
# and it would raise an exeception
|
44
|
+
# Must be !~ Object and be counter_caching to be qualified as updatable
|
45
|
+
def impressionable_class_set
|
46
|
+
klass = self.impressionable_type.to_s.constantize
|
47
|
+
(klass.to_s !~ /Object/) && klass.impressionist_counter_caching? ? klass : false
|
48
|
+
end
|
49
|
+
|
50
|
+
# default mode is ERROR
|
51
|
+
# ruby 1.8.7 support
|
52
|
+
def impressionist_log(str, mode=:error)
|
53
|
+
Rails.logger.send(mode.to_s, str)
|
54
|
+
end
|
55
|
+
|
61
56
|
# receives an entity(instance of a Model) and then tries to update
|
62
57
|
# counter_cache column
|
63
|
-
# entity is a
|
58
|
+
# entity is a impressionable instance model
|
64
59
|
def impressionable_try
|
60
|
+
impressionable_find
|
65
61
|
entity.try(:update_impressionist_counter_cache)
|
66
62
|
end
|
67
63
|
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Impressionist
|
2
|
+
module IsImpressionable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def is_impressionable(options={})
|
7
|
+
define_association
|
8
|
+
@impressionist_cache_options = options
|
9
|
+
|
10
|
+
true
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def define_association
|
16
|
+
has_many(:impressions,
|
17
|
+
:as => :impressionable,
|
18
|
+
:dependent => :destroy)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/lib/impressionist/load.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'impressionist/engine'
|
2
|
-
|
3
1
|
require 'impressionist/setup_association'
|
4
2
|
|
5
3
|
require 'impressionist/counter_cache'
|
@@ -7,3 +5,7 @@ require 'impressionist/counter_cache'
|
|
7
5
|
require 'impressionist/update_counters'
|
8
6
|
|
9
7
|
require 'impressionist/rails_toggle'
|
8
|
+
|
9
|
+
require 'impressionist/is_impressionable'
|
10
|
+
|
11
|
+
require 'impressionist/engine'
|
@@ -1,14 +1,12 @@
|
|
1
1
|
# Responsability
|
2
|
-
|
3
|
-
# * be able to update_counters
|
4
|
-
# * log an error if imps_id and imps_type can not be found
|
2
|
+
# * logs an error if imps_id and imps_type can not be found
|
5
3
|
# * asks updatable? whether it may or may not be updated
|
6
|
-
# FIX exeception raising when no imps_id is found
|
7
4
|
|
8
5
|
class Impression < ActiveRecord::Base
|
9
6
|
|
10
7
|
include Impressionist::CounterCache
|
11
|
-
|
8
|
+
|
9
|
+
# sets belongs_to and attr_accessible depending on Rails version
|
12
10
|
Impressionist::SetupAssociation.new(self).set
|
13
11
|
|
14
12
|
after_save :impressionable_counter_cache_updatable?
|
@@ -1,26 +1,12 @@
|
|
1
|
-
ActiveRecord::Base.send(:include, Impressionist::Impressionable)
|
2
|
-
|
3
1
|
module Impressionist
|
4
|
-
module Impressionable
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
|
9
|
-
def is_impressionable(options={})
|
10
|
-
define_association
|
11
2
|
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
def define_association
|
17
|
-
has_many(:impressions,
|
18
|
-
:as => :impressionable,
|
19
|
-
:dependent => :destroy)
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
3
|
+
module Impressionable
|
23
4
|
|
5
|
+
# extends AS::Concern
|
6
|
+
include Impressionist::IsImpressionable
|
24
7
|
end
|
25
8
|
|
26
9
|
end
|
10
|
+
|
11
|
+
ActiveRecord::Base.
|
12
|
+
send(:include, Impressionist::Impressionable)
|
@@ -1,31 +1,13 @@
|
|
1
|
-
# TODO: Refactor this Entity
|
2
|
-
# There's a lot of duplication
|
3
|
-
Mongoid::Document.send(:include, Impressionist::Impressionable)
|
4
|
-
|
5
1
|
module Impressionist
|
6
2
|
module Impressionable
|
7
|
-
extend ActiveSupport::Concern
|
8
|
-
|
9
|
-
module ClassMethods
|
10
3
|
|
11
|
-
|
12
|
-
|
13
|
-
@impressionist_cache_options = options
|
4
|
+
# extends AS::Concern
|
5
|
+
include Impressionist::IsImpressionable
|
14
6
|
|
15
|
-
|
16
|
-
end
|
7
|
+
## TODO: Make it readable
|
17
8
|
|
18
|
-
|
19
|
-
|
20
|
-
has_many(:impressions,
|
21
|
-
:as => :impressionable,
|
22
|
-
:dependent => :destroy)
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
##
|
28
|
-
# Overides active_record impressionist_count
|
9
|
+
# Overides impressionist_count in order to provied
|
10
|
+
# mongoid compability
|
29
11
|
def impressionist_count(options={})
|
30
12
|
options.reverse_merge!(:filter=>:request_hash, :start_date=>nil, :end_date=>Time.now)
|
31
13
|
imps = options[:start_date].blank? ? impressions : impressions.between(created_at: options[:start_date]..options[:end_date])
|
@@ -34,5 +16,7 @@ module Impressionist
|
|
34
16
|
end
|
35
17
|
|
36
18
|
end
|
37
|
-
|
38
19
|
end
|
20
|
+
|
21
|
+
Mongoid::Document.
|
22
|
+
send(:include, Impressionist::Impressionable)
|
@@ -1,62 +1,68 @@
|
|
1
|
+
# Note
|
2
|
+
# If impressionist_counter_cache_options[:counter_cache] is false(default)
|
3
|
+
# it won't event run this class
|
1
4
|
module Impressionist
|
5
|
+
|
2
6
|
class UpdateCounters
|
3
|
-
attr_reader :receiver, :
|
7
|
+
attr_reader :receiver, :klass
|
4
8
|
|
5
9
|
def initialize(receiver)
|
6
10
|
@receiver = receiver
|
7
|
-
@
|
11
|
+
@klass = receiver.class
|
8
12
|
end
|
9
13
|
|
10
14
|
def update
|
11
|
-
|
12
|
-
|
13
|
-
master.
|
15
|
+
klass.
|
14
16
|
update_counters(id, column_name => result)
|
15
17
|
end
|
16
18
|
|
17
19
|
private
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
20
|
+
|
21
|
+
def result
|
22
|
+
impressions_total - impressions_cached
|
23
|
+
end
|
24
|
+
|
25
|
+
# Count impressions based on unique_filter
|
26
|
+
# default is :ip_address when unique: true
|
27
|
+
def impressions_total
|
28
|
+
receiver.impressionist_count filter
|
29
|
+
end
|
30
|
+
|
31
|
+
# Fetch impressions from a receiver's column
|
32
|
+
def impressions_cached
|
33
|
+
receiver.send(column_name) || 0
|
34
|
+
end
|
35
|
+
|
36
|
+
def filter
|
37
|
+
{:filter => unique_filter}
|
38
|
+
end
|
39
|
+
|
40
|
+
# :filter gets assigned to :ip_address as default
|
41
|
+
# One could do
|
42
|
+
# is_impressionable :counter_cache => true,
|
43
|
+
# :unique => :any_other_filter
|
44
|
+
def unique_filter
|
45
|
+
Symbol === unique ?
|
46
|
+
unique :
|
47
|
+
:ip_address
|
48
|
+
end
|
49
|
+
|
50
|
+
def unique
|
51
|
+
cache_options[:unique]
|
52
|
+
end
|
53
|
+
|
54
|
+
def column_name
|
55
|
+
cache_options[:column_name].to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
def cache_options
|
59
|
+
klass.
|
60
|
+
impressionist_counter_cache_options
|
61
|
+
end
|
62
|
+
|
63
|
+
def id
|
64
|
+
receiver.id
|
65
|
+
end
|
60
66
|
|
61
67
|
end
|
62
68
|
|
@@ -1,5 +1,8 @@
|
|
1
1
|
# Use this hook to configure impressionist parameters
|
2
|
-
Impressionist.setup do |config|
|
2
|
+
#Impressionist.setup do |config|
|
3
3
|
# Define ORM. Could be :active_record (default), :mongo_mapper or :mongoid
|
4
4
|
# config.orm = :active_record
|
5
|
-
end
|
5
|
+
#end
|
6
|
+
|
7
|
+
|
8
|
+
Impressionist.orm = :active_record
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class CreateImpressionsTable < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :impressions, :force => true do |t|
|
4
|
+
t.string :impressionable_type
|
5
|
+
t.integer :impressionable_id
|
6
|
+
t.integer :user_id
|
7
|
+
t.string :controller_name
|
8
|
+
t.string :action_name
|
9
|
+
t.string :view_name
|
10
|
+
t.string :request_hash
|
11
|
+
t.string :ip_address
|
12
|
+
t.string :session_hash
|
13
|
+
t.text :message
|
14
|
+
t.text :referrer
|
15
|
+
t.timestamps
|
16
|
+
end
|
17
|
+
add_index :impressions, [:impressionable_type, :message, :impressionable_id], :name => "impressionable_type_message_index", :unique => false, :length => {:message => 255 }
|
18
|
+
add_index :impressions, [:impressionable_type, :impressionable_id, :request_hash], :name => "poly_request_index", :unique => false
|
19
|
+
add_index :impressions, [:impressionable_type, :impressionable_id, :ip_address], :name => "poly_ip_index", :unique => false
|
20
|
+
add_index :impressions, [:impressionable_type, :impressionable_id, :session_hash], :name => "poly_session_index", :unique => false
|
21
|
+
add_index :impressions, [:controller_name,:action_name,:request_hash], :name => "controlleraction_request_index", :unique => false
|
22
|
+
add_index :impressions, [:controller_name,:action_name,:ip_address], :name => "controlleraction_ip_index", :unique => false
|
23
|
+
add_index :impressions, [:controller_name,:action_name,:session_hash], :name => "controlleraction_session_index", :unique => false
|
24
|
+
add_index :impressions, :user_id
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.down
|
28
|
+
drop_table :impressions
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: impressionist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- johnmcaliley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpclient
|
@@ -208,6 +208,7 @@ files:
|
|
208
208
|
- lib/impressionist/controllers/mongoid/impressionist_controller.rb
|
209
209
|
- lib/impressionist/counter_cache.rb
|
210
210
|
- lib/impressionist/engine.rb
|
211
|
+
- lib/impressionist/is_impressionable.rb
|
211
212
|
- lib/impressionist/load.rb
|
212
213
|
- lib/impressionist/models/active_record/impression.rb
|
213
214
|
- lib/impressionist/models/active_record/impressionist/impressionable.rb
|
@@ -278,6 +279,7 @@ files:
|
|
278
279
|
- tests/test_app/db/migrate/20110201153144_create_articles.rb
|
279
280
|
- tests/test_app/db/migrate/20110210205028_create_posts.rb
|
280
281
|
- tests/test_app/db/migrate/20111127184039_create_widgets.rb
|
282
|
+
- tests/test_app/db/migrate/20130719024021_create_impressions_table.rb
|
281
283
|
- tests/test_app/db/schema.rb
|
282
284
|
- tests/test_app/db/seeds.rb
|
283
285
|
- tests/test_app/lib/assets/.gitkeep
|
@@ -397,6 +399,7 @@ test_files:
|
|
397
399
|
- tests/test_app/db/migrate/20110201153144_create_articles.rb
|
398
400
|
- tests/test_app/db/migrate/20110210205028_create_posts.rb
|
399
401
|
- tests/test_app/db/migrate/20111127184039_create_widgets.rb
|
402
|
+
- tests/test_app/db/migrate/20130719024021_create_impressions_table.rb
|
400
403
|
- tests/test_app/db/schema.rb
|
401
404
|
- tests/test_app/db/seeds.rb
|
402
405
|
- tests/test_app/lib/assets/.gitkeep
|