acts_as_active 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/README.md +25 -6
- data/lib/acts_as_active/activable.rb +13 -2
- data/lib/acts_as_active/version.rb +1 -1
- data/lib/acts_as_active.rb +0 -1
- data/lib/generators/acts_as_active/install_generator.rb +8 -3
- data/lib/generators/acts_as_active/templates/create_activities.rb +7 -6
- metadata +5 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8d77b8db36fd55b8e5a5fd11c5d3c66086ca2edc782802accbc6dd868f7edd0e
|
|
4
|
+
data.tar.gz: c815c49b4daaa44a2b2dc752c744016ca3125a31be46700342053270759be6b6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f2bd60fd0f26c0e240179e2980c552aa69a83907742a33e3d979530ac4613e0227ce3c7357017ff22baa81019617809997e7af9e8fe9e2d19375dc385e89002d
|
|
7
|
+
data.tar.gz: 4fad00c674233c6b94b4eb4f2dbd1fe6011ac414cfa5946c85c9f2d31596dcf207c034d58c15c61c81be93a3981b95ff613fc44979af0ecfc3f50819d187849a
|
data/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# ActsAsActive
|
|
2
2
|
|
|
3
|
-
Acts As Active adds plug-and-play activity tracking to any ActiveRecord model, giving you instant daily stats, streak analytics, and
|
|
3
|
+
Acts As Active adds plug-and-play activity tracking to any ActiveRecord model, giving you instant daily stats, streak analytics, and heatmap-ready data.
|
|
4
|
+
|
|
5
|
+
It works by automatically establishing a polymorphic association with your model and generating an Activity record for each specified lifecycle event.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
@@ -20,15 +22,16 @@ gem install acts_as_active
|
|
|
20
22
|
|
|
21
23
|
```ruby
|
|
22
24
|
class Record < ApplicationRecord
|
|
23
|
-
acts_as_active on: [:create, :update],
|
|
24
|
-
if:
|
|
25
|
-
unless:
|
|
25
|
+
acts_as_active on: [:create, :update], # ➜ Track different actions
|
|
26
|
+
if: -> { track_activity? }, # ➜ On different conditions
|
|
27
|
+
unless: -> { skip_tracking? },
|
|
28
|
+
after_record: -> ( activity ) { puts "Modified: #{activity}" } # ➜ Hook: runs after an activity is created or updated
|
|
26
29
|
end
|
|
27
30
|
```
|
|
28
31
|
|
|
29
32
|
```ruby
|
|
30
|
-
record = Record.create!(title: "First draft") # ➜
|
|
31
|
-
record.update!(title: "Second draft") # ➜
|
|
33
|
+
record = Record.create!(title: "First draft") # ➜ Records activity for today
|
|
34
|
+
record.update!(title: "Second draft") # ➜ Activity count for today = 2
|
|
32
35
|
|
|
33
36
|
|
|
34
37
|
record.active_today? # => true
|
|
@@ -44,6 +47,22 @@ record.longest_streak # => 3 (e.g. active 1-3 Aug)
|
|
|
44
47
|
record.current_streak # => 2 (e.g. active 5-6 Aug, still “on a streak” today)
|
|
45
48
|
```
|
|
46
49
|
|
|
50
|
+
## Accessing Activities
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
record = Record.find(1)
|
|
54
|
+
record.activities # Accessing activities for a model that includes ActsAsActive
|
|
55
|
+
|
|
56
|
+
ActsAsActive::Activity.where(trackable: record) # Query the namespaced model directly
|
|
57
|
+
ActsAsActive::Activity.all # Get all activities across all trackable types
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Run Tests
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
ruby -Ilib:ruby test/test_acts_as_active.rb
|
|
64
|
+
```
|
|
65
|
+
|
|
47
66
|
## Contributing
|
|
48
67
|
|
|
49
68
|
Bug reports and pull requests are welcome on GitHub at https://github.com/amitleshed/acts_as_active. 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/amitleshed/acts_as_active/blob/main/CODE_OF_CONDUCT.md).
|
|
@@ -12,9 +12,11 @@ module ActsAsActive
|
|
|
12
12
|
SUPPORTED_EVENTS = %i[create update destroy].freeze
|
|
13
13
|
|
|
14
14
|
def acts_as_active(options = {})
|
|
15
|
-
has_many :activities, as: :trackable, class_name: "Activity"
|
|
15
|
+
has_many :activities, as: :trackable, class_name: "ActsAsActive::Activity"
|
|
16
|
+
|
|
17
|
+
@acts_as_active_after_record = options[:after_record]
|
|
18
|
+
events = Array(options[:on] || SUPPORTED_EVENTS) & SUPPORTED_EVENTS
|
|
16
19
|
|
|
17
|
-
events = Array(options[:on] || SUPPORTED_EVENTS) & SUPPORTED_EVENTS
|
|
18
20
|
events.each do |ev|
|
|
19
21
|
after_commit on: ev do |_ignored|
|
|
20
22
|
should_run = true
|
|
@@ -25,6 +27,10 @@ module ActsAsActive
|
|
|
25
27
|
end
|
|
26
28
|
end
|
|
27
29
|
end
|
|
30
|
+
|
|
31
|
+
def acts_as_active_after_record
|
|
32
|
+
@acts_as_active_after_record
|
|
33
|
+
end
|
|
28
34
|
end
|
|
29
35
|
|
|
30
36
|
|
|
@@ -35,6 +41,11 @@ module ActsAsActive
|
|
|
35
41
|
activity.count ||= 0
|
|
36
42
|
activity.count += 1
|
|
37
43
|
activity.save!
|
|
44
|
+
|
|
45
|
+
hook = self.class.acts_as_active_after_record
|
|
46
|
+
hook&.call(activity)
|
|
47
|
+
|
|
48
|
+
activity
|
|
38
49
|
end
|
|
39
50
|
end
|
|
40
51
|
|
data/lib/acts_as_active.rb
CHANGED
|
@@ -19,9 +19,14 @@ module ActsAsActive
|
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def create_model
|
|
22
|
-
create_file "app/models/activity.rb", <<~RUBY
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
create_file "app/models/acts_as_active/activity.rb", <<~RUBY
|
|
23
|
+
module ActsAsActive
|
|
24
|
+
class Activity < ApplicationRecord
|
|
25
|
+
self.table_name = "acts_as_active_activities"
|
|
26
|
+
|
|
27
|
+
belongs_to :trackable, polymorphic: true
|
|
28
|
+
belongs_to :actor, polymorphic: true, optional: true
|
|
29
|
+
end
|
|
25
30
|
end
|
|
26
31
|
RUBY
|
|
27
32
|
end
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
class CreateActivities < ActiveRecord::Migration[7.1]
|
|
2
2
|
def change
|
|
3
|
-
create_table :
|
|
4
|
-
t.string
|
|
5
|
-
t.bigint
|
|
6
|
-
t.
|
|
7
|
-
t.
|
|
3
|
+
create_table :acts_as_active_activities do |t|
|
|
4
|
+
t.string :trackable_type, null: false
|
|
5
|
+
t.bigint :trackable_id, null: false
|
|
6
|
+
t.references :actor, polymorphic: true, null: true
|
|
7
|
+
t.date :occurred_on, null: false
|
|
8
|
+
t.integer :count, null: false, default: 0
|
|
8
9
|
|
|
9
10
|
t.timestamps
|
|
10
11
|
end
|
|
11
12
|
|
|
12
|
-
add_index :
|
|
13
|
+
add_index :acts_as_active_activities, [:trackable_type, :trackable_id, :occurred_on], unique: true, name: "index_activities_on_trackable_and_occurred_on"
|
|
13
14
|
end
|
|
14
15
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: acts_as_active
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Amit Leshed
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-08-
|
|
11
|
+
date: 2025-08-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -71,12 +71,12 @@ files:
|
|
|
71
71
|
- lib/generators/acts_as_active/install_generator.rb
|
|
72
72
|
- lib/generators/acts_as_active/templates/create_activities.rb
|
|
73
73
|
- sig/acts_as_active.rbs
|
|
74
|
-
homepage: https://
|
|
74
|
+
homepage: https://www.amitleshed.com
|
|
75
75
|
licenses:
|
|
76
76
|
- MIT
|
|
77
77
|
metadata:
|
|
78
|
-
homepage_uri: https://
|
|
79
|
-
source_code_uri: https://
|
|
78
|
+
homepage_uri: https://www.amitleshed.com
|
|
79
|
+
source_code_uri: https://github.com/amitleshed/acts_as_active
|
|
80
80
|
post_install_message:
|
|
81
81
|
rdoc_options: []
|
|
82
82
|
require_paths:
|