mongoid_activity_tracker 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ee65ee17d1eb90b0e1b2dcda4b58fdfdeb86f17
4
- data.tar.gz: 40b9bdf6a32a373639b277178c5d238d079d5def
3
+ metadata.gz: ee823c459da4ba5e2237e1b4f37781bd4cd2e415
4
+ data.tar.gz: 973cfac3d8cb5b3cb8ffa5e1dad4dd7537a2b46f
5
5
  SHA512:
6
- metadata.gz: 7501a73486046c4b8ab16b318e5b30b1cc9d9a1e5e6173693638b4a25584f17f524748222f01fdd0dd9fb412c127583084de4d719a0efd754b7175c2c6e3e222
7
- data.tar.gz: 9f950c7d392f3a12e0f8cb1ac52f87e52d4c578b33ae93975ef4b73347a1aa62d16284a18fac75db2d6e32d2805b2ff8d51816e4e03901f2f492aaf12d204509
6
+ metadata.gz: ed8320caa536de6ff2da6b6fce30c55e48860a50895415643161290d3d69d853a1ae481e4c7256427e574026f556db9e3bf211697db6cfce8253c579edc43068
7
+ data.tar.gz: d31ffa48203b59524e927a7d4e50662c7ac3f35c11b00c61998e1222017a0214ab3c5921bfad4eb5ea5405709267bd1d643147c0a5a4ff01c1fe41cd59d639cc
data/README.md CHANGED
@@ -37,42 +37,54 @@ end
37
37
  Track the activity:
38
38
 
39
39
  ```Ruby
40
- my_tracker = MongoidActivityTracker::TrackActivity.with(MyTracker, current_user)
41
-
42
- res = my_tracker.track('create')
40
+ res = MyTracker.track(current_user, :create)
43
41
 
44
42
  res.actor # returns the current_user
45
43
  res.actor_cache # => { to_s: … }
46
44
  res.actor_cache_object.to_s # the 'actor_cache_object' wraps the actor_cache hash into an OpenStruct
47
- res.action # => 'create'
45
+ res.action # => :create
48
46
  ```
49
47
 
50
48
  ## Configuration
51
49
 
52
50
  Along the action, it is possible to track any number of related documents.
53
51
 
54
- First, configure your tracker class using the `.tracks` macros:
52
+ First, configure the tracker class using the `.tracks` macros:
55
53
 
56
54
  ```Ruby
57
55
  class MyTracker
58
56
  include MongoidActivityTracker::Tracker
59
- tracks :subject, cache_methods: [:to_s, :id]
57
+ tracks :subject
60
58
  end
61
59
  ```
62
60
 
63
- Then, specify your subject when tracking the activity along the action:
61
+ Then, specify the subject when tracking the activity:
64
62
 
65
63
  ```Ruby
66
- my_tracker = MongoidActivityTracker::TrackActivity.with(MyTracker, current_user)
67
-
68
- res = my_tracker.track('create', subject: my_subject)
64
+ res = MyTracker.track(current_user, :create, subject: my_subject)
69
65
 
70
- res.subject # returns my_subject
71
- res.subject_cache # => { to_s: …, id: … }
66
+ res.subject # => my_subject
67
+ res.subject_cache # => { to_s: … }
72
68
  res.subject_cache_object.to_s # …
73
69
  ```
74
70
 
75
- By default, the `:cache_methods` parameters is set to track [:to_s].
71
+ By default, the `:cache_methods` parameter is set to track [:to_s]. It can be customised to cache results of other methods as follows:
72
+
73
+ ```Ruby
74
+ class MyTracker
75
+ include MongoidActivityTracker::Tracker
76
+ tracks :actor, cache_methods: [:to_s, :first_name, :last_name]
77
+ tracks :subject, cache_methods: [:to_s, :my_custom_method]
78
+ end
79
+ ```
80
+
81
+ The tracker class can be also subclassed:
82
+
83
+ ```Ruby
84
+ class MyTrackerWithTarget < MyTracker
85
+ tracks :target
86
+ end
87
+ ```
76
88
 
77
89
  ## Contributing
78
90
 
@@ -7,6 +7,15 @@ module MongoidActivityTracker
7
7
  attr_accessor :actor_cache_methods
8
8
 
9
9
  module ClassMethods
10
+
11
+ def track _actor, _action, options={}
12
+ self.create(
13
+ { action: _action, actor: _actor }.merge(options)
14
+ )
15
+ end
16
+
17
+ # ---------------------------------------------------------------------
18
+
10
19
  def tracks relation_name, cache_methods: %i(to_s)
11
20
  belongs_to relation_name, polymorphic: true
12
21
 
@@ -15,8 +24,8 @@ module MongoidActivityTracker
15
24
  attr_accessor "#{relation_name}_cache_methods"
16
25
 
17
26
  define_method "#{relation_name}_cache_methods" do
18
- instance_variable_set "@#{relation_name}_cache_methods", cache_methods unless instance_variable_get "@#{relation_name}_cache_methods"
19
- instance_variable_get "@#{relation_name}_cache_methods"
27
+ instance_variable_set("@#{relation_name}_cache_methods", cache_methods) unless instance_variable_get("@#{relation_name}_cache_methods")
28
+ instance_variable_get("@#{relation_name}_cache_methods")
20
29
  end
21
30
 
22
31
  define_method "#{relation_name}_cache_object" do
@@ -34,7 +43,7 @@ module MongoidActivityTracker
34
43
  base.class_eval do
35
44
  include Mongoid::Document
36
45
 
37
- field :action, type: String
46
+ field :action, type: Symbol
38
47
 
39
48
  tracks :actor
40
49
 
@@ -1,3 +1,3 @@
1
1
  module MongoidActivityTracker
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,4 +1,3 @@
1
1
  require 'mongoid_activity_tracker/version'
2
2
 
3
- require 'mongoid_activity_tracker/track_activity'
4
3
  require 'mongoid_activity_tracker/tracker'
@@ -1,11 +1,10 @@
1
1
  require 'test_helper'
2
2
 
3
- require_relative '../../lib/mongoid_activity_tracker/tracker'
4
-
5
3
  module MongoidActivityTracker
6
4
  describe Tracker do
7
5
 
8
6
  let(:actor) { TestActor.new }
7
+ let(:sub) { TestSubject.new }
9
8
  subject { TestTracker.new }
10
9
 
11
10
  describe 'associations' do
@@ -24,31 +23,53 @@ module MongoidActivityTracker
24
23
  end
25
24
  end
26
25
 
26
+ # ---------------------------------------------------------------------
27
+
28
+ describe '.track' do
29
+ before do
30
+ @result = TestTracker.track(actor, :create, subject: sub)
31
+ end
32
+ it 'returns new object' do
33
+ @result.must_be_kind_of TestTracker
34
+ end
35
+ it 'always sets the :actor' do
36
+ @result.actor.must_equal actor
37
+ end
38
+ it 'sets the :actor_cache with :to_s by default' do
39
+ @result.actor_cache.must_equal({ to_s: actor.to_s })
40
+ end
41
+ it 'sets the :action' do
42
+ @result.action.must_equal :create
43
+ end
44
+ it 'sets any other attributes' do
45
+ @result.subject.must_equal sub
46
+ end
47
+ end
48
+
27
49
  # ---------------------------------------------------------------------
28
50
 
29
51
  describe '.tracks' do
30
- let(:tracker_two) { TestTrackerTwo.new }
31
52
  it 'adds the belongs_to :subject relation' do
32
- tracker_two.must_respond_to :subject
53
+ subject.must_respond_to :subject
33
54
  end
34
55
  it 'adds the :subject_cache field' do
35
- tracker_two.must_respond_to :subject_cache
36
- tracker_two.subject_cache.must_be_kind_of Hash
56
+ subject.must_respond_to :subject_cache
57
+ subject.subject_cache.must_be_kind_of Hash
37
58
  end
38
59
  it 'defines the :subject_cache_methods accessor' do
39
- tracker_two.must_respond_to :subject_cache_methods
60
+ subject.must_respond_to :subject_cache_methods
40
61
  end
41
62
  it 'sets :to_s as a default for the :subject_cache_methods' do
42
- tracker_two.subject_cache_methods.must_equal %i(to_s)
63
+ subject.subject_cache_methods.must_equal %i(to_s)
43
64
  end
44
65
  it 'adds the :subject_cache_object wrapper' do
45
- tracker_two.subject_cache_object.must_respond_to :to_s
66
+ subject.subject_cache_object.must_respond_to :to_s
46
67
  end
47
68
  it 'sets the cache before :save' do
48
69
  test_subject = TestSubject.new
49
- tracker_two.subject = test_subject
50
- tracker_two.run_callbacks(:save)
51
- tracker_two.subject_cache.fetch(:to_s).must_equal test_subject.to_s
70
+ subject.subject = test_subject
71
+ subject.run_callbacks(:save)
72
+ subject.subject_cache.fetch(:to_s).must_equal test_subject.to_s
52
73
  end
53
74
  end
54
75
 
data/test/test_helper.rb CHANGED
@@ -3,9 +3,9 @@ require 'database_cleaner'
3
3
  require 'minitest'
4
4
  require 'minitest/autorun'
5
5
  require 'minitest/spec'
6
+
6
7
  require 'mongoid'
7
8
  require 'mongoid_activity_tracker'
8
- require 'mongoid_activity_tracker/tracker'
9
9
 
10
10
  if ENV["CI"]
11
11
  require "coveralls"
@@ -39,18 +39,13 @@ DatabaseCleaner.orm = :mongoid
39
39
  DatabaseCleaner.strategy = :truncation
40
40
 
41
41
  class MiniTest::Spec
42
- before(:each) { DatabaseCleaner.start }
43
- after(:each) { DatabaseCleaner.clean }
42
+ before(:each) { DatabaseCleaner.clean }
44
43
  end
45
44
 
46
45
  # ---------------------------------------------------------------------
47
46
 
48
47
  class TestTracker
49
48
  include MongoidActivityTracker::Tracker
50
- end
51
-
52
- class TestTrackerTwo
53
- include MongoidActivityTracker::Tracker
54
49
  tracks :subject
55
50
  end
56
51
 
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.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Celizna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-15 00:00:00.000000000 Z
11
+ date: 2014-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -137,11 +137,9 @@ files:
137
137
  - README.md
138
138
  - Rakefile
139
139
  - lib/mongoid_activity_tracker.rb
140
- - lib/mongoid_activity_tracker/track_activity.rb
141
140
  - lib/mongoid_activity_tracker/tracker.rb
142
141
  - lib/mongoid_activity_tracker/version.rb
143
142
  - mongoid_activity_tracker.gemspec
144
- - test/mongoid_activity_tracker/track_activity_test.rb
145
143
  - test/mongoid_activity_tracker/tracker_test.rb
146
144
  - test/test_helper.rb
147
145
  homepage: https://github.com/tomasc/mongoid_activity_tracker
@@ -169,6 +167,5 @@ signing_key:
169
167
  specification_version: 4
170
168
  summary: Minimal set of classes and modules to help with activity tracking.
171
169
  test_files:
172
- - test/mongoid_activity_tracker/track_activity_test.rb
173
170
  - test/mongoid_activity_tracker/tracker_test.rb
174
171
  - test/test_helper.rb
@@ -1,22 +0,0 @@
1
- module MongoidActivityTracker
2
- class TrackActivity
3
-
4
- def self.with *args
5
- new(*args)
6
- end
7
-
8
- # ---------------------------------------------------------------------
9
-
10
- def initialize tracker_class, actor
11
- @tracker_class = tracker_class
12
- @actor = actor
13
- end
14
-
15
- def track action, options={}
16
- @tracker_class.create(
17
- { action: action, actor: @actor}.merge(options)
18
- )
19
- end
20
-
21
- end
22
- end
@@ -1,33 +0,0 @@
1
- require 'test_helper'
2
-
3
- require_relative '../../lib/mongoid_activity_tracker/track_activity'
4
-
5
- module MongoidActivityTracker
6
- describe TrackActivity do
7
- let(:actor) { TestActor.new }
8
- subject { TrackActivity.with(TestTrackerTwo, actor) }
9
- let(:sub) { TestSubject.new }
10
-
11
- describe '.call' do
12
- before do
13
- @tracker = subject.track('create', subject: sub)
14
- end
15
- it 'returns the tracker instance' do
16
- @tracker.must_be_kind_of TestTrackerTwo
17
- end
18
- it 'always sets the :actor' do
19
- @tracker.actor.must_equal actor
20
- end
21
- it 'sets the :actor_cache with :to_s by default' do
22
- @tracker.actor_cache.must_equal({ to_s: actor.to_s })
23
- end
24
- it 'always sets the :action' do
25
- @tracker.action.must_equal 'create'
26
- end
27
- it 'sets any other attributes' do
28
- @tracker.subject.must_equal sub
29
- end
30
- end
31
-
32
- end
33
- end