activerecord_activity_tracker 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3c09229f37893a454390cccc6cc3443cd969bc7c
4
+ data.tar.gz: 9c5cb93539f0c16fe7ddb8376a2e0c9a9aa95c93
5
+ SHA512:
6
+ metadata.gz: 6ec127a4a30576b4308835c5a126d090f94604b18865b250bf32c6a278605e9017c8e161ebd2fa064dd70feec513a6793a420bda4c66f39bb7f2217f247b722a
7
+ data.tar.gz: 4157605a7bacbbb6953dc33f7e38026d57c0028c8849362d7fd99dff4b727908d1eb7525ad617c433215124fc4941bfc233ac0ccfc6df9b8222fb682bc290912
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Mudafar
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.
@@ -0,0 +1,270 @@
1
+ # ActiverecordActivityTracker
2
+ `activerecord_activity_tracker` provides simple yet powerful activity tracker for your Rails **ActiveRecord** models.
3
+
4
+ This gem will allows you to create the data model for social **news feed** used in many modern platform in no time.
5
+
6
+ `Note:` This is an abstract gem, thus there are no views or controllers provided.
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+ ### Features
19
+ - Track model's creation and/or updating activities.
20
+ - Track any custom activity's event easily.
21
+ - Add optional data to any custom activity.
22
+ - Set owner once, and use it in all tracked models seamlessly.
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+ ## Usage
37
+
38
+ ### Track a model
39
+ - Include `ActiverecordActivityTracker::ActsAsTrackable`
40
+ - For automatic tracking add `acts_as_trackable`
41
+ - For custom event use `create_ar_activity`
42
+
43
+ ### Set default owner (user)
44
+ - Include `ActiverecordActivityTracker::Owner`
45
+ - To set the owner use `set_owner(current_user)`
46
+ - To get the owner use `get_owner`
47
+ - To clear owner use `clear_owner`
48
+
49
+ `Note:` setting the owner is required, as each activity must have an owner, see [set default owner example](#set-default-owner-to-devise-current_user)
50
+
51
+
52
+ ### Access model's tracked activities
53
+ - Include `ActiverecordActivityTracker::ActsAsTrackable`
54
+ - Use `ar_activities`
55
+
56
+ ### Examples
57
+
58
+ #### Track comment's creation
59
+ ```ruby
60
+ # app/models/comment.rb
61
+ class Comment < ApplicationRecord
62
+ include ActiverecordActivityTracker::ActsAsTrackable
63
+
64
+ acts_as_trackable [:create]
65
+ end
66
+ ```
67
+
68
+ #### Track comment's creation and modification
69
+ ```ruby
70
+ # app/models/comment.rb
71
+ class Comment < ApplicationRecord
72
+ include ActiverecordActivityTracker::ActsAsTrackable
73
+
74
+ acts_as_trackable [:create, :update]
75
+ end
76
+ ```
77
+
78
+ #### Track comment's custom event (title's change)
79
+
80
+ ```ruby
81
+ # app/models/comment.rb
82
+ class Comment < ApplicationRecord
83
+ include ActiverecordActivityTracker::ActsAsTrackable
84
+
85
+ after_update :add_title_change_activity
86
+
87
+ private
88
+
89
+ def add_title_change_activity
90
+ create_ar_activity(key: 'comment.title.change') if saved_change_to_title?
91
+ end
92
+
93
+ end
94
+ ```
95
+
96
+ #### Set default owner to devise current_user
97
+ `Note:` owner is a polymorphic relation, thus any model can be used, not just user.
98
+
99
+ ```ruby
100
+ # app/controllers/application_controller.rb
101
+ class ApplicationController < ActionController::Base
102
+ include ActiverecordActivityTracker::Owner
103
+
104
+ protect_from_forgery with: :exception
105
+
106
+ around_action :set_ar_activity_owner
107
+
108
+
109
+ private
110
+
111
+ def set_ar_activity_owner
112
+ set_owner current_user
113
+ yield
114
+ ensure
115
+ clear_owner
116
+ end
117
+
118
+ end
119
+ ```
120
+
121
+ ### Access comment's activities
122
+
123
+ ```html
124
+ <!-- app/views/comments/show.erb -->
125
+ <% @comment.ar_activities.each do |activity| %>
126
+ <%= activity.key %>
127
+ <br>
128
+ <% end %>
129
+ ```
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+ ## Installation
145
+ Add this line to your application's Gemfile:
146
+
147
+ ```ruby
148
+ gem 'activerecord_activity_tracker'
149
+ ```
150
+
151
+ And then execute:
152
+ ```bash
153
+ $ bundle
154
+ ```
155
+
156
+ Or install it yourself as:
157
+ ```bash
158
+ $ gem install activerecord_activity_tracker
159
+ ```
160
+
161
+ ### Setup
162
+ Execute this line in your application's directory:
163
+ ```bash
164
+ $ rails generate activerecord_activity_tracker:install
165
+ ```
166
+
167
+ And then execute:
168
+ ```bash
169
+ $ rails db:migrate
170
+ ```
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+ ### Documentation / API
185
+ #### ActiverecordActivityTracker::ActsAsTrackable
186
+ - `acts_as_trackable(tracked = [:create, :update]) => nil`
187
+ Overview:
188
+ Track model automatically.
189
+
190
+ Parameters:
191
+ tracked (array) (defaults to: [:create, :update]) -- instance options to set the tracked events.
192
+
193
+
194
+ - `create_ar_activity(options = {}) => nil`
195
+ Overview:
196
+ Create custom activity, prevent duplication.
197
+
198
+ Options:
199
+ - key (string) (defaults to: "#{model_name.param_key}.create").
200
+ - owner (active_record_relation) (defaults to: get_owner).
201
+ - data (string) (defaults to: nil).
202
+
203
+ Parameters:
204
+ -- options (hash) (defaults to: {}) -- instance options to set custom params.
205
+
206
+
207
+ - `create_ar_activity!(options = {}) => nil`
208
+ Overview:
209
+ Similar to `create_ar_activity` but allows duplication.
210
+
211
+
212
+
213
+ - `has_many :ar_activities, as: :trackable, dependent: :destroy`
214
+ Overview:
215
+ Handle activities' relations.
216
+
217
+ #### ActiverecordActivityTracker::Owner
218
+ - `get_owner() => owner (active_record_model or nil)`
219
+ Overview: Get the current owner
220
+
221
+ - `set_owner(owner) => nil`
222
+ Overview: Set the current owner.
223
+
224
+ Parameters:
225
+ owner (active_record_model) -- instance active record model to set current owner.
226
+
227
+
228
+ - `clear_owner => nil`
229
+ Overview: Set the current owner to nil.
230
+
231
+
232
+ #### ArActivityTracker
233
+ - ##### Description
234
+ Activity model, to handle trackable and owner relations.
235
+
236
+ - ##### Data structure
237
+ ` t.string "trackable_type"
238
+ t.integer "trackable_id"
239
+ t.string "owner_type"
240
+ t.integer "owner_id"
241
+ t.string "key"
242
+ t.string "data"
243
+ `
244
+ - ##### Key default format
245
+ MODEL_NAME.ACTIVITY_TYPE
246
+
247
+ **For example:**
248
+ Given a `tracked` comment model `Comment`, then the keys (by default) will be:
249
+ `comment.create` and `comment.update` for creation and modification activities respectively.
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+ ## Contributing
261
+
262
+ 1. Fork it ( https://github.com/mudafar/activerecord_activity_tracker/fork )
263
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
264
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
265
+ 4. Push to the branch (`git push origin my-new-feature`)
266
+ 5. Create a new Pull Request
267
+
268
+
269
+ ## License
270
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,33 @@
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 = 'ActiverecordActivityTracker'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+
33
+ task default: :test
@@ -0,0 +1,7 @@
1
+ require 'activerecord_activity_tracker/acts_as_trackable'
2
+ require 'activerecord_activity_tracker/owner'
3
+
4
+ module ActiverecordActivityTracker
5
+
6
+
7
+ end
@@ -0,0 +1,59 @@
1
+ # require 'activerecord_activity_tracker/activerecord_activity'
2
+ require 'activerecord_activity_tracker/owner'
3
+
4
+ module ActiverecordActivityTracker
5
+ module ActsAsTrackable
6
+ extend ActiveSupport::Concern
7
+ include ActiverecordActivityTracker::Owner
8
+
9
+ class_methods do
10
+ def acts_as_trackable(tracked = [:create, :update])
11
+ if tracked.include?(:create)
12
+ before_create -> {ar_activities.build(owner: get_owner, key: "#{model_name.param_key}.create")}
13
+ end
14
+
15
+ if tracked.include?(:update)
16
+ before_update -> {ar_activities.build(owner: get_owner, key: "#{model_name.param_key}.update")}
17
+ end
18
+ end
19
+ end
20
+
21
+
22
+ included do
23
+ has_many :ar_activities, as: :trackable, dependent: :destroy
24
+
25
+ # Prevent duplication by default
26
+ def create_ar_activity(options = {})
27
+ _create_ar_activity(false, options)
28
+ end
29
+
30
+ # Allow duplication
31
+ def create_ar_activity!(options = {})
32
+ _create_ar_activity(true, options)
33
+ end
34
+
35
+ end
36
+
37
+
38
+ private
39
+
40
+ def _create_ar_activity(allow_dup, options = {})
41
+ default_options = {
42
+ key: "#{model_name.param_key}.create",
43
+ data: nil,
44
+ owner: get_owner
45
+ }
46
+ default_options.merge!(options)
47
+
48
+
49
+ if allow_dup ||
50
+ (!ar_activities.exists?(key: default_options[:key]) ||
51
+ !ar_activities.exists?(owner: default_options[:owner]) ||
52
+ !ar_activities.exists?(data: default_options[:data])
53
+ )
54
+ ar_activities.create(default_options)
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,6 @@
1
+ module ActiverecordActivityTracker
2
+ module CurrentOwner
3
+ thread_mattr_accessor :owner
4
+ end
5
+ end
6
+
@@ -0,0 +1,19 @@
1
+ require 'activerecord_activity_tracker/current_owner'
2
+
3
+ module ActiverecordActivityTracker
4
+ module Owner
5
+
6
+ def get_owner
7
+ ActiverecordActivityTracker::CurrentOwner.owner
8
+ end
9
+
10
+ def set_owner(owner)
11
+ ActiverecordActivityTracker::CurrentOwner.owner = owner
12
+ end
13
+
14
+ def clear_owner
15
+ ActiverecordActivityTracker::CurrentOwner.owner = nil
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module ActiverecordActivityTracker
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module ActiverecordActivityTracker
4
+ # A generator module with Activity table schema.
5
+ module Generators
6
+ # A base module
7
+ module Base
8
+ # Get path for migration template
9
+ def source_root
10
+ @_activerecord_activity_tracker_source_root ||= File.expand_path(File.join('../activerecord_activity_tracker', generator_name, 'templates'), __FILE__)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ require 'generators/activerecord_activity_tracker'
2
+ require 'rails/generators/active_record'
3
+
4
+ module ActiverecordActivityTracker
5
+ module Generators
6
+ # Migration generator that creates migration file from template
7
+ class InstallGenerator < ActiveRecord::Generators::Base
8
+ extend Base
9
+
10
+ argument :name, type: :string, default: 'create_activerecord_activities'
11
+
12
+ def generate_model
13
+ Rails::Generators.invoke('active_record:model',
14
+ %w(ArActivity trackable:references{polymorphic} owner:references{polymorphic} key:string data:string),
15
+ behavior: behavior)
16
+ end
17
+
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :activerecord_activity_tracker do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_activity_tracker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mudafar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.5
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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
+ description: This gem will allows you to create the data model for social news feed
42
+ used in many modern platform in no time.
43
+ email:
44
+ - mudafar@ula.ve
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - lib/activerecord_activity_tracker.rb
53
+ - lib/activerecord_activity_tracker/acts_as_trackable.rb
54
+ - lib/activerecord_activity_tracker/current_owner.rb
55
+ - lib/activerecord_activity_tracker/owner.rb
56
+ - lib/activerecord_activity_tracker/version.rb
57
+ - lib/generators/activerecord_activity_tracker.rb
58
+ - lib/generators/activerecord_activity_tracker/install/install_generator.rb
59
+ - lib/tasks/activerecord_activity_tracker_tasks.rake
60
+ homepage: https://github.com/mudafar/activerecord_activity_tracker
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.6.11
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: activerecord_activity_tracker provides simple yet powerful activity tracker
84
+ for your Rails ActiveRecord models.
85
+ test_files: []