mongoid_activity_tracker 0.2.4 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 94dd8cbd82e867c4fe185a8bef77492a814481d0da50a26cf03d36375f0747fd
4
- data.tar.gz: 9d676e598a9e75d47e5fabd836a86166b16e8cc1102be37029c0b41324c5208c
3
+ metadata.gz: b723478a7ba1e99b77eb60fb255cfde76de5e80382a25e4945b3a54b6517aee4
4
+ data.tar.gz: c3eefa74402cdf148777d0d42ae9f546a88b82c1fd2df1450c6930ead3e64d13
5
5
  SHA512:
6
- metadata.gz: 45d6590df78a1ee718a86916d7b5e72216c82d0c74dbdcdbdd2f2326da68d6a77f952ebabbecb86a35aa5cb158513c72f310d2579ee77208bdb9a3b1c177d6f5
7
- data.tar.gz: 21fe199dff8dc56708358ee1b51d6b245d70eb24b4540de4bb9f170d4bb9a73232202abb7ffb7494b955f3aa3b17cd3fcbfb764b0313f2329649cd9bd86ff7a0
6
+ metadata.gz: 47c1e0fcf15676c2c64994507c483bf649c5776925c137cf79cc6f3ca38e71d8ace2d0a35ab36d149da19168dcf70140b633f6091c2b434e639aa374e154b11e
7
+ data.tar.gz: b25734e19e8e241d39d146ff7d68c8e5e15ad94b28de791e9c3cb7bfc9c9d0ecc5e385fc124f7932b1e4b8d990de0f2d7caccff149b81015513d9f5879189512
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.2.5
4
+
5
+ * tracker is valid even if `:actor` is not present, but `:to_s` is stored in `#actor_cache`
6
+ * renders past activity valid event if the actor model has been removed from the database
7
+
3
8
  ## 0.2.4
4
9
 
5
10
  * Mongoid 7 compatibility
@@ -14,9 +14,7 @@ module MongoidActivityTracker
14
14
  create!({ action: action, actor: actor }.merge(options))
15
15
  end
16
16
 
17
- # ---------------------------------------------------------------------
18
-
19
- def tracks(relation_name, cache_methods: %i(to_s))
17
+ def tracks(relation_name, cache_methods: %i[to_s])
20
18
  field_name = [relation_name, 'cache'].join('_')
21
19
  accessor_name = [relation_name, 'cache_methods'].join('_')
22
20
  instance_variable_name = ['@', accessor_name].join
@@ -36,12 +34,10 @@ module MongoidActivityTracker
36
34
  OpenStruct.new(send(field_name))
37
35
  end
38
36
 
39
- before_save -> { set_cache(relation_name) }
37
+ before_validation -> { set_cache(relation_name) }
40
38
  end
41
39
  end
42
40
 
43
- # ---------------------------------------------------------------------
44
-
45
41
  def self.included(base)
46
42
  base.extend ClassMethods
47
43
  base.class_eval do
@@ -51,18 +47,16 @@ module MongoidActivityTracker
51
47
 
52
48
  tracks :actor
53
49
 
54
- validates :actor, presence: true
50
+ validates :actor, presence: true, unless: -> { actor_cache && actor_cache[:to_s].present? }
55
51
  validates :action, presence: true
56
52
  end
57
53
  end
58
54
 
59
- # =====================================================================
60
-
61
55
  def created_at
62
56
  id.generation_time.in_time_zone(Time.zone)
63
57
  end
64
58
 
65
- private # =============================================================
59
+ private
66
60
 
67
61
  def set_cache(relation_name)
68
62
  accessor_name = [relation_name, 'cache_methods'].join('_')
@@ -73,6 +67,7 @@ module MongoidActivityTracker
73
67
 
74
68
  send(accessor_name).each do |m|
75
69
  next if send(field_name)[m].present?
70
+
76
71
  send(field_name)[m] = send(relation_name).send(m) if send(relation_name).respond_to?(m)
77
72
  end
78
73
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidActivityTracker
2
- VERSION = '0.2.4'.freeze
2
+ VERSION = '0.2.5'.freeze
3
3
  end
@@ -1,5 +1,4 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'mongoid_activity_tracker/version'
5
4
 
@@ -1,112 +1,103 @@
1
1
  require 'test_helper'
2
2
 
3
- module MongoidActivityTracker
4
- describe Tracker do
5
- let(:actor) { TestActor.new }
6
- let(:sub) { TestSubject.new }
7
- subject { TestTracker.new }
8
-
9
- describe 'associations' do
10
- it 'belongs to :actor' do
11
- subject.must_respond_to :actor
12
- end
3
+ describe MongoidActivityTracker::Tracker do
4
+ subject { TestTracker.new }
5
+
6
+ let(:actor) { TestActor.new }
7
+ let(:sub) { TestSubject.new }
8
+
9
+ describe 'associations' do
10
+ it { subject.must_respond_to :actor }
11
+ end
12
+
13
+ describe 'validations' do
14
+ describe 'by default' do
15
+ before { subject.valid? }
16
+
17
+ it { subject.errors.messages.keys.must_include :actor }
18
+ it { subject.errors.messages.keys.must_include :action }
13
19
  end
14
20
 
15
- describe 'fields' do
16
- it 'has :action' do
17
- subject.must_respond_to :action
18
- end
19
- it 'has :actor_cache' do
20
- subject.must_respond_to :actor_cache
21
- subject.actor_cache.must_be_kind_of Hash
21
+ describe 'accepts to_s stored in actor_cache' do
22
+ before do
23
+ subject.actor_cache[:to_s] = 'John Doe'
24
+ subject.valid?
22
25
  end
26
+
27
+ it { subject.errors.messages.keys.wont_include :actor }
23
28
  end
29
+ end
24
30
 
25
- # ---------------------------------------------------------------------
31
+ describe 'fields' do
32
+ it { subject.must_respond_to :action }
33
+ it { subject.must_respond_to :actor_cache }
34
+ it { subject.actor_cache.must_be_kind_of Hash }
35
+ end
26
36
 
27
- describe '.track' do
28
- let(:result) { TestTracker.track(actor, :create, subject: sub) }
37
+ describe '.track' do
38
+ let(:result) { TestTracker.track(actor, :create, subject: sub) }
29
39
 
30
- it 'returns new object' do
31
- result.must_be_kind_of TestTracker
32
- end
33
- it 'always sets the :actor' do
34
- result.actor.must_equal actor
35
- end
36
- it 'sets the :actor_cache with :to_s by default' do
37
- result.actor_cache.must_equal(to_s: actor.to_s)
38
- end
39
- it 'sets the :action' do
40
- result.action.must_equal :create
41
- end
42
- it 'sets any other attributes' do
43
- result.subject.must_equal sub
44
- end
45
- end
40
+ it { result.must_be_kind_of TestTracker }
41
+ it { result.actor.must_equal actor }
42
+ it { result.actor_cache.must_equal(to_s: actor.to_s) }
43
+ it { result.action.must_equal :create }
44
+ it { result.subject.must_equal sub }
45
+ end
46
46
 
47
- # ---------------------------------------------------------------------
47
+ describe '.tracks' do
48
+ it { subject.must_respond_to :subject }
49
+ it { subject.must_respond_to :subject_cache }
50
+ it { subject.subject_cache.must_be_kind_of Hash }
51
+ it { subject.must_respond_to :subject_cache_methods }
52
+ it { subject.subject_cache_methods.must_equal %i[to_s] }
53
+ it { subject.subject_cache_object.must_respond_to :to_s }
48
54
 
49
- describe '.tracks' do
50
- it 'adds the belongs_to :subject relation' do
51
- subject.must_respond_to :subject
52
- end
53
- it 'adds the :subject_cache field' do
54
- subject.must_respond_to :subject_cache
55
- subject.subject_cache.must_be_kind_of Hash
56
- end
57
- it 'defines the :subject_cache_methods accessor' do
58
- subject.must_respond_to :subject_cache_methods
59
- end
60
- it 'sets :to_s as a default for the :subject_cache_methods' do
61
- subject.subject_cache_methods.must_equal %i(to_s)
62
- end
63
- it 'adds the :subject_cache_object wrapper' do
64
- subject.subject_cache_object.must_respond_to :to_s
65
- end
66
- it 'sets the cache before :save' do
67
- test_subject = TestSubject.new
68
- subject.subject = test_subject
69
- subject.run_callbacks(:save)
70
- subject.subject_cache.fetch(:to_s).must_equal test_subject.to_s
55
+ describe TestSubject do
56
+ let(:tsubject) { TestSubject.new }
57
+
58
+ before do
59
+ subject.subject = tsubject
60
+ subject.run_callbacks(:validation)
71
61
  end
62
+
63
+ it { subject.subject_cache.fetch(:to_s).must_equal tsubject.to_s }
72
64
  end
65
+ end
73
66
 
74
- # ---------------------------------------------------------------------
67
+ describe ':created_at' do
68
+ it { subject.must_respond_to :created_at }
69
+ it { subject.created_at.must_be_kind_of Time }
70
+ end
75
71
 
76
- describe ':created_at' do
77
- it 'infers time from BSON id' do
78
- subject.must_respond_to :created_at
79
- subject.created_at.must_be_kind_of Time
80
- end
72
+ describe ':actor_cache' do
73
+ before do
74
+ subject.actor = actor
75
+ subject.run_callbacks(:validation)
81
76
  end
82
77
 
83
- describe ':actor_cache' do
78
+ it { subject.actor_cache.fetch(:to_s).must_equal actor.to_s }
79
+
80
+ describe ':to_s' do
84
81
  before do
85
- subject.actor = actor
86
- end
87
- it 'sets :actor_cache on save' do
88
- subject.run_callbacks(:save)
89
- subject.actor_cache.fetch(:to_s).must_equal actor.to_s
90
- end
91
- it 'allows preset cache manually' do
92
82
  subject.actor_cache[:to_s] = 'foo-bar'
93
- subject.run_callbacks(:save)
94
- subject.actor_cache.fetch(:to_s).must_equal 'foo-bar'
95
- end
96
- it 'allows to override the default cache methods' do
97
- subject.actor_cache_methods = %i(class)
98
- subject.run_callbacks(:save)
99
- subject.actor_cache.fetch(:class).must_equal TestActor
83
+ subject.run_callbacks(:validation)
100
84
  end
85
+
86
+ it { subject.actor_cache.fetch(:to_s).must_equal 'foo-bar' }
101
87
  end
102
88
 
103
- describe ':actor_cache_object' do
104
- it 'wraps the :actor_cache with a Struct' do
105
- subject.actor_cache_object.must_be_kind_of OpenStruct
106
- end
107
- it 'converts keys to methods' do
108
- subject.actor_cache_object.must_respond_to :to_s
89
+ describe '#actor_cache_methods' do
90
+ before do
91
+ subject.actor_cache_methods = %i[class]
92
+ subject.run_callbacks(:validation)
109
93
  end
94
+
95
+ it { subject.actor_cache.fetch(:class).must_equal TestActor }
110
96
  end
111
97
  end
98
+
99
+ describe ':actor_cache_object' do
100
+ it { subject.actor_cache_object.must_be_kind_of OpenStruct }
101
+ it { subject.actor_cache_object.must_respond_to :to_s }
102
+ end
112
103
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_activity_tracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Celizna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-03 00:00:00.000000000 Z
11
+ date: 2019-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -168,8 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
168
  - !ruby/object:Gem::Version
169
169
  version: '0'
170
170
  requirements: []
171
- rubyforge_project:
172
- rubygems_version: 2.7.3
171
+ rubygems_version: 3.0.1
173
172
  signing_key:
174
173
  specification_version: 4
175
174
  summary: Minimal set of classes and modules to help with activity tracking.