acts_as_tracked 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +52 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +10 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +142 -0
- data/LICENSE.txt +21 -0
- data/README.md +187 -0
- data/Rakefile +8 -0
- data/acts_as_tracked.gemspec +40 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/acts_as_tracked.rb +13 -0
- data/lib/acts_as_tracked/activity.rb +36 -0
- data/lib/acts_as_tracked/extenders/trackable.rb +24 -0
- data/lib/acts_as_tracked/tracking.rb +106 -0
- data/lib/acts_as_tracked/version.rb +5 -0
- data/lib/generators/acts_as_tracked/migration/migration_generator.rb +41 -0
- data/lib/generators/acts_as_tracked/migration/templates/active_record/migration.erb +20 -0
- metadata +204 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 395a0143c5923febe5d923e31332bcd516bb901915a4515162d5372889a820b9
|
4
|
+
data.tar.gz: 7f717b289f17654c9ccfd02b5504dd1efdf018fff83f110ec84fb0431537af25
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2c1149d0fee0c6a2a44f51420e758bb6dc600753d6157d3ea9f5ea1ec236acf5057beaaae0472db71fe6304a0b56d1990c08e87a36aaf0b66328996e329b6ac2
|
7
|
+
data.tar.gz: 2370e1de45f80e14a09f3c9b8477773ea9240ac906674ac8702d881bd0f8af751105028ca07cc13215a7c76e883741293c876c79b2733e56be3a7ab804e0d62d
|
@@ -0,0 +1,52 @@
|
|
1
|
+
version: 2.1
|
2
|
+
orbs:
|
3
|
+
ruby: circleci/ruby@0.1.2
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
docker:
|
8
|
+
- image: circleci/ruby:2.6.3-stretch-node
|
9
|
+
environment:
|
10
|
+
RAILS_ENV: test
|
11
|
+
PGHOST: 127.0.0.1
|
12
|
+
PGUSER: root
|
13
|
+
|
14
|
+
- image: circleci/postgres:9.6.2-alpine
|
15
|
+
environment:
|
16
|
+
POSTGRES_USER: root
|
17
|
+
POSTGRES_DB: acts_as_tracked_test
|
18
|
+
|
19
|
+
executor: ruby/default
|
20
|
+
steps:
|
21
|
+
- checkout
|
22
|
+
|
23
|
+
- restore_cache:
|
24
|
+
name: Restore bundle cache
|
25
|
+
keys:
|
26
|
+
- acts-as-tracked-dependencies
|
27
|
+
|
28
|
+
- run:
|
29
|
+
name: Install Bundler
|
30
|
+
command: gem install bundler
|
31
|
+
|
32
|
+
- run:
|
33
|
+
name: Bundle Install
|
34
|
+
command: |
|
35
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
36
|
+
|
37
|
+
- run: sudo apt install -y postgresql-client || true
|
38
|
+
|
39
|
+
- save_cache:
|
40
|
+
name: Store bundle cache
|
41
|
+
paths:
|
42
|
+
- ./vendor/bundle
|
43
|
+
key: acts-as-tracked-dependencies
|
44
|
+
|
45
|
+
- run:
|
46
|
+
name: Rubocop check
|
47
|
+
command: bundle exec rubocop
|
48
|
+
|
49
|
+
- run:
|
50
|
+
name: Test Suite
|
51
|
+
command: PREPARE_COMBUSTION_DB_USING_SCHEMA=true bundle exec rspec
|
52
|
+
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at sahil.gadimbay@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
acts_as_tracked (1.0.0)
|
5
|
+
actionview (>= 4.2)
|
6
|
+
activerecord (>= 4.2)
|
7
|
+
activesupport (>= 4.2)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
actionpack (6.0.3.2)
|
13
|
+
actionview (= 6.0.3.2)
|
14
|
+
activesupport (= 6.0.3.2)
|
15
|
+
rack (~> 2.0, >= 2.0.8)
|
16
|
+
rack-test (>= 0.6.3)
|
17
|
+
rails-dom-testing (~> 2.0)
|
18
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
19
|
+
actionview (6.0.3.2)
|
20
|
+
activesupport (= 6.0.3.2)
|
21
|
+
builder (~> 3.1)
|
22
|
+
erubi (~> 1.4)
|
23
|
+
rails-dom-testing (~> 2.0)
|
24
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
25
|
+
activemodel (6.0.3.2)
|
26
|
+
activesupport (= 6.0.3.2)
|
27
|
+
activerecord (6.0.3.2)
|
28
|
+
activemodel (= 6.0.3.2)
|
29
|
+
activesupport (= 6.0.3.2)
|
30
|
+
activesupport (6.0.3.2)
|
31
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
32
|
+
i18n (>= 0.7, < 2)
|
33
|
+
minitest (~> 5.1)
|
34
|
+
tzinfo (~> 1.1)
|
35
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
36
|
+
ast (2.4.1)
|
37
|
+
builder (3.2.4)
|
38
|
+
codecov (0.1.17)
|
39
|
+
json
|
40
|
+
simplecov
|
41
|
+
url
|
42
|
+
combustion (1.3.0)
|
43
|
+
activesupport (>= 3.0.0)
|
44
|
+
railties (>= 3.0.0)
|
45
|
+
thor (>= 0.14.6)
|
46
|
+
concurrent-ruby (1.1.6)
|
47
|
+
crass (1.0.6)
|
48
|
+
diff-lcs (1.4.2)
|
49
|
+
docile (1.3.2)
|
50
|
+
erubi (1.9.0)
|
51
|
+
i18n (1.8.3)
|
52
|
+
concurrent-ruby (~> 1.0)
|
53
|
+
json (2.3.0)
|
54
|
+
loofah (2.6.0)
|
55
|
+
crass (~> 1.0.2)
|
56
|
+
nokogiri (>= 1.5.9)
|
57
|
+
method_source (1.0.0)
|
58
|
+
mini_portile2 (2.4.0)
|
59
|
+
minitest (5.14.1)
|
60
|
+
nokogiri (1.10.9)
|
61
|
+
mini_portile2 (~> 2.4.0)
|
62
|
+
parallel (1.19.2)
|
63
|
+
parser (2.7.1.4)
|
64
|
+
ast (~> 2.4.1)
|
65
|
+
pg (1.2.3)
|
66
|
+
rack (2.2.3)
|
67
|
+
rack-test (1.1.0)
|
68
|
+
rack (>= 1.0, < 3)
|
69
|
+
rails-dom-testing (2.0.3)
|
70
|
+
activesupport (>= 4.2.0)
|
71
|
+
nokogiri (>= 1.6)
|
72
|
+
rails-html-sanitizer (1.3.0)
|
73
|
+
loofah (~> 2.3)
|
74
|
+
railties (6.0.3.2)
|
75
|
+
actionpack (= 6.0.3.2)
|
76
|
+
activesupport (= 6.0.3.2)
|
77
|
+
method_source
|
78
|
+
rake (>= 0.8.7)
|
79
|
+
thor (>= 0.20.3, < 2.0)
|
80
|
+
rainbow (3.0.0)
|
81
|
+
rake (12.3.3)
|
82
|
+
regexp_parser (1.7.1)
|
83
|
+
rexml (3.2.4)
|
84
|
+
rspec-core (3.9.2)
|
85
|
+
rspec-support (~> 3.9.3)
|
86
|
+
rspec-expectations (3.9.2)
|
87
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
88
|
+
rspec-support (~> 3.9.0)
|
89
|
+
rspec-mocks (3.9.1)
|
90
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
91
|
+
rspec-support (~> 3.9.0)
|
92
|
+
rspec-rails (4.0.1)
|
93
|
+
actionpack (>= 4.2)
|
94
|
+
activesupport (>= 4.2)
|
95
|
+
railties (>= 4.2)
|
96
|
+
rspec-core (~> 3.9)
|
97
|
+
rspec-expectations (~> 3.9)
|
98
|
+
rspec-mocks (~> 3.9)
|
99
|
+
rspec-support (~> 3.9)
|
100
|
+
rspec-support (3.9.3)
|
101
|
+
rubocop (0.85.1)
|
102
|
+
parallel (~> 1.10)
|
103
|
+
parser (>= 2.7.0.1)
|
104
|
+
rainbow (>= 2.2.2, < 4.0)
|
105
|
+
regexp_parser (>= 1.7)
|
106
|
+
rexml
|
107
|
+
rubocop-ast (>= 0.0.3)
|
108
|
+
ruby-progressbar (~> 1.7)
|
109
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
110
|
+
rubocop-ast (0.0.3)
|
111
|
+
parser (>= 2.7.0.1)
|
112
|
+
ruby-progressbar (1.10.1)
|
113
|
+
simplecov (0.18.5)
|
114
|
+
docile (~> 1.1)
|
115
|
+
simplecov-html (~> 0.11)
|
116
|
+
simplecov-html (0.12.2)
|
117
|
+
thor (1.0.1)
|
118
|
+
thread_safe (0.3.6)
|
119
|
+
tzinfo (1.2.7)
|
120
|
+
thread_safe (~> 0.1)
|
121
|
+
unicode-display_width (1.7.0)
|
122
|
+
url (0.3.2)
|
123
|
+
with_model (2.1.4)
|
124
|
+
activerecord (>= 5.2, < 6.1)
|
125
|
+
zeitwerk (2.3.0)
|
126
|
+
|
127
|
+
PLATFORMS
|
128
|
+
ruby
|
129
|
+
|
130
|
+
DEPENDENCIES
|
131
|
+
acts_as_tracked!
|
132
|
+
codecov
|
133
|
+
combustion (~> 1.3)
|
134
|
+
pg
|
135
|
+
rake (~> 12.0)
|
136
|
+
rspec-rails
|
137
|
+
rubocop
|
138
|
+
simplecov
|
139
|
+
with_model
|
140
|
+
|
141
|
+
BUNDLED WITH
|
142
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Sahil Gadimbayli
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/acts-as-tracked.svg)](https://badge.fury.io/rb/acts-as-tracked)
|
2
|
+
[![CircleCI](https://circleci.com/gh/ramblingcode/acts-as-tracked.svg?style=svg)](https://circleci.com/gh/ramblingcode/acts-as-tracked)
|
3
|
+
[![codecov](https://codecov.io/gh/ramblingcode/acts-as-tracked/branch/master/graph/badge.svg)](https://codecov.io/gh/ramblingcode/acts-as-tracked)
|
4
|
+
|
5
|
+
|
6
|
+
# ActsAsTracked
|
7
|
+
|
8
|
+
Welcome to ActsAsTracked! This gem is an extension to your ActiveRecord models to track activities. It does not track everything all the time, but can be used wherever you find it necessary to have a history for changes alongside their actors.
|
9
|
+
|
10
|
+
There are few other gems such as [audited](https://github.com/collectiveidea/audited), however, it is tracking every change on your models. ActsAsTracked is controlled by `you` and will track changes only when `used directly`.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'acts_as_tracked'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
```shell
|
23
|
+
$ bundle install
|
24
|
+
```
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
```shell
|
29
|
+
$ gem install acts_as_tracked
|
30
|
+
```
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
### Create Activities table
|
35
|
+
|
36
|
+
First, you would need to generate a migration to create Activities table in your database.
|
37
|
+
|
38
|
+
ActsAsTracked has extension to generate migrations. Please, run:
|
39
|
+
|
40
|
+
```shell
|
41
|
+
bundle exec rails generate acts_as_tracked:migration
|
42
|
+
```
|
43
|
+
|
44
|
+
This will generate following migration:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class ActsAsTrackedMigration < ActiveRecord::Migration<Rails Version>
|
48
|
+
def self.up
|
49
|
+
create_table :activities do |t|
|
50
|
+
t.integer :actor_id, index: true
|
51
|
+
t.string :actor_type
|
52
|
+
t.integer :subject_id, index: true
|
53
|
+
t.string :subject_type
|
54
|
+
t.integer :parent_id, index: true
|
55
|
+
t.string :parent_type
|
56
|
+
t.text :attribute_changes
|
57
|
+
t.string :activity_type
|
58
|
+
t.string :human_description
|
59
|
+
t.timestamps null: false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.down
|
64
|
+
drop_table :activity
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
Run `bundle exec rails db:migrate` and let's move on to the usage.
|
70
|
+
|
71
|
+
### Call acts_as_tracked in your AR model
|
72
|
+
|
73
|
+
If you would like to track changes of `Post` model, you would need to call `acts_as_tracked` in it.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class Post < ApplicationRecord
|
77
|
+
acts_as_tracked
|
78
|
+
|
79
|
+
# You may optionally pass in exluded_activity_attributes
|
80
|
+
# as an argument to not track given fields.
|
81
|
+
#
|
82
|
+
# acts_as_tracked(exclude_activity_attributes: %i[:api_key, :username])
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
### Tracking Changes
|
87
|
+
|
88
|
+
Now, you are able to track changes on Post.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
@post = Post.first
|
92
|
+
|
93
|
+
Post.tracking_changes(actor: User.find(1)) do
|
94
|
+
@post.update(
|
95
|
+
content: 'New Content'
|
96
|
+
)
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
Let's try to get activities for Post we just changed:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
@post.activities
|
104
|
+
|
105
|
+
[
|
106
|
+
#<ActsAsTracked::Activity:0x000056475df12840
|
107
|
+
id: 1,
|
108
|
+
actor_id: 1,
|
109
|
+
actor_type: "User",
|
110
|
+
subject_id: 2,
|
111
|
+
subject_type: "Post",
|
112
|
+
parent_id: nil,
|
113
|
+
parent_type: nil,
|
114
|
+
attribute_changes: {"content"=>["Great post content.", "New Content"]},
|
115
|
+
activity_type: "updated",
|
116
|
+
human_description: nil,
|
117
|
+
created_at: Thu, 25 Jun 2020 12:03:39 UTC +00:00,
|
118
|
+
updated_at: Thu, 25 Jun 2020 12:03:39 UTC +00:00>
|
119
|
+
]
|
120
|
+
```
|
121
|
+
|
122
|
+
## More features
|
123
|
+
|
124
|
+
You can check activities for the record in which it was an Actor:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
@post.activities_as_actor
|
128
|
+
|
129
|
+
...activities
|
130
|
+
```
|
131
|
+
|
132
|
+
You can check activities for the record in which it was a Subject:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
@post.activities_as_subject
|
136
|
+
|
137
|
+
...activities
|
138
|
+
```
|
139
|
+
|
140
|
+
You can check activities for collection of records by passing ids:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
Post.activities_for([post_id1, post_id2])
|
144
|
+
|
145
|
+
...activities
|
146
|
+
```
|
147
|
+
|
148
|
+
### Extra options to pass to `tracking_changes` call
|
149
|
+
|
150
|
+
`tracking_changes` method can accept 3 arguments:
|
151
|
+
|
152
|
+
1. actor: mandatory -> actor record
|
153
|
+
2. subject: optional -> acting on record, defaults to record you are changing
|
154
|
+
3. parent: optional -> parent record for acting on record, defaults to nil
|
155
|
+
4. human_description: optional -> description you would like to include on change, defaults to `nil`
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
Post.tracking_changes(actor: User.first, subject: Post.first, human_description: 'Some description of change', parent: Post.first.parent) do
|
159
|
+
...your changes here
|
160
|
+
end
|
161
|
+
```
|
162
|
+
|
163
|
+
### Example application using ActsAsTracked
|
164
|
+
|
165
|
+
I have created a Rails 6 application with the usages of ActsAsTracked. Please refer to this [repo](https://github.com/ramblingcode/rails6-acts-as-tracked-usage)
|
166
|
+
|
167
|
+
## Credits
|
168
|
+
|
169
|
+
Initial work of ActsAsTracked has been done by @rogercampos and @camaloon team. I have refined, packaged, documented, added generators and published it.
|
170
|
+
|
171
|
+
## Development
|
172
|
+
|
173
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
174
|
+
|
175
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
176
|
+
|
177
|
+
## Contributing
|
178
|
+
|
179
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ramblingcode/acts_as_tracked. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/acts_as_tracked/blob/master/CODE_OF_CONDUCT.md).
|
180
|
+
|
181
|
+
## License
|
182
|
+
|
183
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
184
|
+
|
185
|
+
## Code of Conduct
|
186
|
+
|
187
|
+
Everyone interacting in the ActsAsTracked project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/acts_as_tracked/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/acts_as_tracked/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'acts_as_tracked'
|
7
|
+
spec.version = ActsAsTracked::VERSION
|
8
|
+
spec.authors = ['Sahil Gadimbayli']
|
9
|
+
spec.email = ['sahil.gadimbay@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Activity Tracker to plug into ActiveRecord.'
|
12
|
+
spec.description = 'Track activities in your ActiveRecord models.'
|
13
|
+
spec.homepage = 'https://github.com/ramblingcode/acts-as-tracked'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.bindir = 'exe'
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ['lib']
|
28
|
+
|
29
|
+
spec.add_dependency 'actionview', '>= 4.2'
|
30
|
+
spec.add_dependency 'activerecord', '>= 4.2'
|
31
|
+
spec.add_dependency 'activesupport', '>= 4.2'
|
32
|
+
|
33
|
+
spec.add_development_dependency 'codecov'
|
34
|
+
spec.add_development_dependency 'combustion', '~> 1.3'
|
35
|
+
spec.add_development_dependency 'pg'
|
36
|
+
spec.add_development_dependency 'rspec-rails'
|
37
|
+
spec.add_development_dependency 'rubocop'
|
38
|
+
spec.add_development_dependency 'simplecov'
|
39
|
+
spec.add_development_dependency 'with_model'
|
40
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'acts_as_tracked'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'acts_as_tracked/version'
|
4
|
+
|
5
|
+
require 'active_record'
|
6
|
+
require 'active_support'
|
7
|
+
|
8
|
+
require 'acts_as_tracked/activity'
|
9
|
+
require 'acts_as_tracked/extenders/trackable'
|
10
|
+
|
11
|
+
module ActsAsTracked
|
12
|
+
ActiveRecord::Base.extend ActsAsTracked::Extenders::Trackable
|
13
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActsAsTracked
|
4
|
+
class Activity < ::ActiveRecord::Base
|
5
|
+
belongs_to :actor, polymorphic: true
|
6
|
+
belongs_to :subject, polymorphic: true
|
7
|
+
belongs_to :parent, polymorphic: true
|
8
|
+
|
9
|
+
before_save { self[:attribute_changes] = self[:attribute_changes].with_indifferent_access }
|
10
|
+
|
11
|
+
store :attribute_changes
|
12
|
+
|
13
|
+
validates :subject, :actor, :activity_type, presence: true
|
14
|
+
|
15
|
+
default_scope -> { order(arel_table[:created_at].desc) }
|
16
|
+
|
17
|
+
def human_changes
|
18
|
+
case activity_type
|
19
|
+
when 'created'
|
20
|
+
attribute_changes.reject { |_, values| values.last.to_s.blank? }
|
21
|
+
when 'updated'
|
22
|
+
attribute_changes.each { |_, values| values.map! { |v| v.presence || 'empty' } }
|
23
|
+
when 'destroyed'
|
24
|
+
attribute_changes.reject { |_, values| values.first.to_s.blank? }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def subject_class
|
29
|
+
@subject_class ||= subject_type&.constantize
|
30
|
+
end
|
31
|
+
|
32
|
+
def parent_class
|
33
|
+
@parent_class ||= parent_type&.constantize
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# require 'active_support/concern'
|
4
|
+
|
5
|
+
module ActsAsTracked
|
6
|
+
module Extenders
|
7
|
+
module Trackable
|
8
|
+
# extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
# Includes Tracking module in specified model
|
11
|
+
#
|
12
|
+
# @params exclude_activity_attributes [:foo, :bar]
|
13
|
+
# if given, excludes specified params from tracking
|
14
|
+
|
15
|
+
def acts_as_tracked(args = {})
|
16
|
+
require 'acts_as_tracked/tracking'
|
17
|
+
|
18
|
+
include ::ActsAsTracked::Tracking
|
19
|
+
|
20
|
+
exclude_activity_attributes(*args[:exclude_activity_attributes]) if args[:exclude_activity_attributes]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module ActsAsTracked
|
6
|
+
module Tracking
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
has_many :activities_as_subject, as: :subject, class_name: 'ActsAsTracked::Activity', dependent: :nullify
|
11
|
+
has_many :activities_as_parent, as: :parent, class_name: 'ActsAsTracked::Activity', dependent: :nullify
|
12
|
+
has_many :activities_as_actor, as: :actor, class_name: 'ActsAsTracked::Activity', dependent: :nullify
|
13
|
+
|
14
|
+
after_create :track_create_activity!, if: :tracking_changes?
|
15
|
+
after_update :track_update_activity!, if: :tracking_changes?, prepend: true
|
16
|
+
after_destroy :track_destroy_activity!, if: :tracking_changes?
|
17
|
+
|
18
|
+
cattr_accessor(:__global_activity_attributes) { {} }
|
19
|
+
cattr_accessor(:__global_tracking_changes) { false }
|
20
|
+
cattr_accessor(:__excluded_attributes) { %w[updated_at created_at] }
|
21
|
+
end
|
22
|
+
|
23
|
+
def activities
|
24
|
+
t = Activity.arel_table
|
25
|
+
Activity.where t[:subject_type].eq(self.class.name).and(t[:subject_id].eq(id))
|
26
|
+
.or(t[:parent_type].eq(self.class.name).and(t[:parent_id].eq(id)))
|
27
|
+
end
|
28
|
+
|
29
|
+
def tracking_changes(attributes)
|
30
|
+
@activity_attributes = attributes
|
31
|
+
@tracking_changes = true
|
32
|
+
yield
|
33
|
+
ensure
|
34
|
+
@activity_attributes = {}
|
35
|
+
@tracking_changes = false
|
36
|
+
end
|
37
|
+
|
38
|
+
def tracking_changes?
|
39
|
+
@tracking_changes || self.class.__global_tracking_changes
|
40
|
+
end
|
41
|
+
|
42
|
+
def activity_attributes
|
43
|
+
self.class.__global_activity_attributes.merge(@activity_attributes || {})
|
44
|
+
end
|
45
|
+
|
46
|
+
def activity_label
|
47
|
+
raise NotImplementedError, "You must define this method in #{self.class}"
|
48
|
+
end
|
49
|
+
|
50
|
+
class_methods do
|
51
|
+
def tracking_changes(opts)
|
52
|
+
self.__global_activity_attributes = __global_activity_attributes.merge(opts)
|
53
|
+
self.__global_tracking_changes = true
|
54
|
+
yield
|
55
|
+
ensure
|
56
|
+
self.__global_activity_attributes = {}
|
57
|
+
self.__global_tracking_changes = false
|
58
|
+
end
|
59
|
+
|
60
|
+
def exclude_activity_attributes(*attributes)
|
61
|
+
self.__excluded_attributes += attributes.map(&:to_s)
|
62
|
+
end
|
63
|
+
|
64
|
+
def activities_for(ids)
|
65
|
+
ids = Array.wrap(ids)
|
66
|
+
t = Activity.arel_table
|
67
|
+
Activity.where t[:subject_type].eq(name).and(t[:subject_id].in(ids))
|
68
|
+
.or(t[:parent_type].eq(name).and(t[:parent_id].in(ids)))
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
protected
|
73
|
+
|
74
|
+
def track_activity!(type, defaults = {})
|
75
|
+
Activity.create!(defaults.merge(subject: self, activity_type: type))
|
76
|
+
end
|
77
|
+
|
78
|
+
def track_create_activity!
|
79
|
+
return true unless activity_changes.any?
|
80
|
+
|
81
|
+
track_activity!(:created, activity_attributes.merge(attribute_changes: activity_changes))
|
82
|
+
true
|
83
|
+
end
|
84
|
+
|
85
|
+
def track_update_activity!
|
86
|
+
return true unless activity_changes.any?
|
87
|
+
|
88
|
+
track_activity!(:updated, activity_attributes.merge(attribute_changes: activity_changes))
|
89
|
+
true
|
90
|
+
end
|
91
|
+
|
92
|
+
def track_destroy_activity!
|
93
|
+
track_activity!(:destroyed, activity_attributes)
|
94
|
+
true
|
95
|
+
end
|
96
|
+
|
97
|
+
def activity_changes
|
98
|
+
changes = saved_changes.transform_values(&:first).keys.reject do |x|
|
99
|
+
self.class.__excluded_attributes.include?(x.to_s)
|
100
|
+
end
|
101
|
+
changes.map do |x|
|
102
|
+
[x, [send("#{x}_before_last_save").to_s, self[x]]]
|
103
|
+
end.to_h
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/migration'
|
4
|
+
|
5
|
+
module ActsAsTracked
|
6
|
+
class MigrationGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
MIGRATION_TIMESTAMP_FORMAT = '%Y%m%d%H%M%S'
|
9
|
+
|
10
|
+
def self.source_root
|
11
|
+
File.join(File.dirname(__FILE__), 'templates', 'active_record')
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.next_migration_number(_path)
|
15
|
+
Time.now.utc.strftime(MIGRATION_TIMESTAMP_FORMAT)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_migration_file
|
19
|
+
migration_template 'migration.erb',
|
20
|
+
'db/migrate/acts_as_tracked_migration.rb'
|
21
|
+
{ migration_version: migration_version }
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# Rails 5.x+ requires Rails version to
|
27
|
+
# be specified in migration file
|
28
|
+
|
29
|
+
def migration_version
|
30
|
+
migration_versions = {
|
31
|
+
'5' => '[5.0]',
|
32
|
+
'6' => '[6.0]'
|
33
|
+
}
|
34
|
+
|
35
|
+
migration_versions.fetch(
|
36
|
+
Rails.version[0],
|
37
|
+
nil
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class ActsAsTrackedMigration < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
def self.up
|
3
|
+
create_table :activities do |t|
|
4
|
+
t.integer :actor_id, index: true
|
5
|
+
t.string :actor_type
|
6
|
+
t.integer :subject_id, index: true
|
7
|
+
t.string :subject_type
|
8
|
+
t.integer :parent_id, index: true
|
9
|
+
t.string :parent_type
|
10
|
+
t.text :attribute_changes
|
11
|
+
t.string :activity_type
|
12
|
+
t.string :human_description
|
13
|
+
t.timestamps null: false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :activity
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_tracked
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sahil Gadimbayli
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionview
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: codecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: combustion
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pg
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: with_model
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: Track activities in your ActiveRecord models.
|
154
|
+
email:
|
155
|
+
- sahil.gadimbay@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".circleci/config.yml"
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".rubocop.yml"
|
164
|
+
- CODE_OF_CONDUCT.md
|
165
|
+
- Gemfile
|
166
|
+
- Gemfile.lock
|
167
|
+
- LICENSE.txt
|
168
|
+
- README.md
|
169
|
+
- Rakefile
|
170
|
+
- acts_as_tracked.gemspec
|
171
|
+
- bin/console
|
172
|
+
- bin/setup
|
173
|
+
- lib/acts_as_tracked.rb
|
174
|
+
- lib/acts_as_tracked/activity.rb
|
175
|
+
- lib/acts_as_tracked/extenders/trackable.rb
|
176
|
+
- lib/acts_as_tracked/tracking.rb
|
177
|
+
- lib/acts_as_tracked/version.rb
|
178
|
+
- lib/generators/acts_as_tracked/migration/migration_generator.rb
|
179
|
+
- lib/generators/acts_as_tracked/migration/templates/active_record/migration.erb
|
180
|
+
homepage: https://github.com/ramblingcode/acts-as-tracked
|
181
|
+
licenses:
|
182
|
+
- MIT
|
183
|
+
metadata:
|
184
|
+
homepage_uri: https://github.com/ramblingcode/acts-as-tracked
|
185
|
+
post_install_message:
|
186
|
+
rdoc_options: []
|
187
|
+
require_paths:
|
188
|
+
- lib
|
189
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
190
|
+
requirements:
|
191
|
+
- - ">="
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: 2.3.0
|
194
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
|
+
requirements:
|
196
|
+
- - ">="
|
197
|
+
- !ruby/object:Gem::Version
|
198
|
+
version: '0'
|
199
|
+
requirements: []
|
200
|
+
rubygems_version: 3.0.3
|
201
|
+
signing_key:
|
202
|
+
specification_version: 4
|
203
|
+
summary: Activity Tracker to plug into ActiveRecord.
|
204
|
+
test_files: []
|