acts_as_active 0.1.3 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f32379b025f2eecaca909f9ea2fa6289cd353f480141fe8494e4179f339254b8
4
- data.tar.gz: be503eaf4cbf750cd66ab8eab07323050d6fee1e172e7eb5d01678418969cb0f
3
+ metadata.gz: c452d929cd97ab9aa78b33e2b6f470c05e54ef62771b5ab4b7894c567dfd0549
4
+ data.tar.gz: 26205d4a6a40162ee0ca68a9e0d622d802aecf4de082dc324ebb4403be3c1db5
5
5
  SHA512:
6
- metadata.gz: a546e8ca4ba37b06556559b763fae3433a8a23fa7eb1e2d2a5e3c845cd3b8072b95226463c3329cb390fbde29484f86a834932555cdc4be98aa1f775896411bb
7
- data.tar.gz: dfaae1ec7b0ce78e65bd218dcd01eac9e8048f90ec8597feeaea9ecbc311b397a303d11714b62ca4d2910563e9d573017cf73775525efefabec7b0e2b1e383f0
6
+ metadata.gz: ebc15ecf2c2ba7b55c53c6bce2d32b8c37b15602e0cf4685425337e66e42d9df5c0f11ee58c5d8813d637fd290706230b233df5c87982808ccebe9d22e871abd
7
+ data.tar.gz: 1043c825d41ad29fa487ade82b69be49cecb1810c05229282b1e1ff1fdd28a95d0a6190d58918b21344f722bac8911a916e27224396275e47c3e5d7739bc9ac0
data/README.md CHANGED
@@ -18,19 +18,26 @@ If bundler is not being used to manage dependencies, install the gem by executin
18
18
  gem install acts_as_active
19
19
  ```
20
20
 
21
+ Generators: ActsAsActive supports a metadata field in either json or jsonb format, depending on your database and preference (MySQL or PostgreSQL).
22
+
23
+ ```bash
24
+ rails g acts_as_active:install --metadata=json/jsonb/auto
25
+ ```
26
+
21
27
  ## Usage
22
28
 
23
29
  ```ruby
24
30
  class Record < ApplicationRecord
25
- acts_as_active on: [:create, :update], # ➜ track different actions
26
- if: -> { track_activity? }, # ➜ on different conditions
27
- unless: -> { skip_tracking? }
31
+ acts_as_active on: [:create, :update], # ➜ Track different actions
32
+ if: -> { track_activity? }, # ➜ On different conditions
33
+ unless: -> { skip_tracking? },
34
+ after_record: -> (activity) { activity.update(metadata: {be: "BOP!"}) } # ➜ Hook: runs after an activity is created or updated
28
35
  end
29
36
  ```
30
37
 
31
38
  ```ruby
32
- record = Record.create!(title: "First draft") # ➜ records activity for today
33
- record.update!(title: "Second draft") # ➜ activity count for today = 2
39
+ record = Record.create!(title: "First draft") # ➜ Records activity for today
40
+ record.update!(title: "Second draft") # ➜ Activity count for today = 2
34
41
 
35
42
 
36
43
  record.active_today? # => true
@@ -46,6 +53,50 @@ record.longest_streak # => 3 (e.g. active 1-3 Aug)
46
53
  record.current_streak # => 2 (e.g. active 5-6 Aug, still “on a streak” today)
47
54
  ```
48
55
 
56
+ ## Accessing Activities
57
+
58
+ ```ruby
59
+ record = Record.find(1)
60
+ record.activities # Accessing activities for a model that includes ActsAsActive
61
+
62
+ ActsAsActive::Activity.where(trackable: record) # Query the namespaced model directly
63
+ ActsAsActive::Activity.all # Get all activities across all trackable types
64
+ ```
65
+
66
+ ## Metadata usage
67
+
68
+ PostgreSQL
69
+
70
+ ```ruby
71
+ # Where metadata contains key/value
72
+ ActsAsActive::Activity.where("metadata @> ?", { source: "api" }.to_json)
73
+
74
+ # Where nested key/value
75
+ ActsAsActive::Activity.where("metadata @> ?", { user: { id: 42 } }.to_json)
76
+ ```
77
+
78
+ MySQL/SQLite
79
+
80
+ ```ruby
81
+ # MySQL: Find where metadata->source == 'api'
82
+ ActsAsActive::Activity.where("JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.source')) = ?", "api")
83
+
84
+ # SQLite (with JSON1 extension):
85
+ ActsAsActive::Activity.where("json_extract(metadata, '$.source') = ?", "api")
86
+ ```
87
+
88
+ Writing to metadata
89
+
90
+ ```ruby
91
+ activity.update!(
92
+ metadata: {
93
+ source: "api",
94
+ user: { id: 42, name: "Amit" },
95
+ tags: ["Dig", "Be", "Bop"]
96
+ }
97
+ )
98
+ ```
99
+
49
100
  ## Run Tests
50
101
 
51
102
  ```bash
@@ -14,7 +14,9 @@ module ActsAsActive
14
14
  def acts_as_active(options = {})
15
15
  has_many :activities, as: :trackable, class_name: "ActsAsActive::Activity"
16
16
 
17
- events = Array(options[:on] || SUPPORTED_EVENTS) & SUPPORTED_EVENTS
17
+ @acts_as_active_after_record = options[:after_record]
18
+ events = Array(options[:on] || SUPPORTED_EVENTS) & SUPPORTED_EVENTS
19
+
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsActive
4
- VERSION = "0.1.3"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -4,17 +4,21 @@ require "rails/generators/migration"
4
4
  module ActsAsActive
5
5
  module Generators
6
6
  class InstallGenerator < Rails::Generators::Base
7
+ desc "Generates the migration for ActsAsActive"
7
8
  include Rails::Generators::Migration
8
9
 
10
+ class_option :metadata, type: :string, default: "auto", desc: "jsonb|json|none|auto"
9
11
  source_root File.expand_path("templates", __dir__)
10
- desc "Generates the migration for ActsAsActive"
11
12
 
12
13
  def self.next_migration_number(dirname)
13
14
  Time.now.utc.strftime("%Y%m%d%H%M%S")
14
15
  end
15
16
 
16
17
  def copy_migration
17
- migration_template "create_activities.rb", "db/migrate/create_activities.rb"
18
+ @metadata_choice = normalize_metadata_choice(options[:metadata])
19
+ puts "!!! metadata_choice #{@metadata_choice}"
20
+ @adapter_name = (ActiveRecord::Base.connection.adapter_name rescue "Unknown")
21
+ migration_template "create_acts_as_active_activities.rb.erb", "db/migrate/create_acts_as_active_activities.rb"
18
22
  create_model
19
23
  end
20
24
 
@@ -30,6 +34,15 @@ module ActsAsActive
30
34
  end
31
35
  RUBY
32
36
  end
37
+
38
+ private
39
+
40
+ def normalize_metadata_choice(choice)
41
+ case choice&.downcase
42
+ when "jsonb", "json", "none" then choice.downcase
43
+ else "auto"
44
+ end
45
+ end
33
46
  end
34
47
  end
35
48
  end
@@ -0,0 +1,40 @@
1
+ class CreateActsAsActiveActivities < ActiveRecord::Migration[7.1]
2
+ def change
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
9
+ <%
10
+ adapter = @adapter_name.to_s
11
+ choice = @metadata_choice
12
+
13
+ def pg?(name) = name =~ /PostgreSQL/i
14
+ col_type =
15
+ if choice == "jsonb" || (choice == "auto" && pg?(adapter))
16
+ :jsonb
17
+ elsif choice == "json"
18
+ :json
19
+ elsif choice == "auto"
20
+ :json
21
+ else
22
+ nil
23
+ end
24
+ %>
25
+
26
+ <% if col_type %>
27
+ t.<%= col_type %> :metadata, null: false, default: {}
28
+ <% end %>
29
+ t.timestamps
30
+ end
31
+
32
+ add_index :acts_as_active_activities,
33
+ %i[trackable_type trackable_id occurred_on],
34
+ unique: true, name: "idx_aaa_trackable_on_day"
35
+ <% if col_type == :jsonb && pg?(@adapter_name) %>
36
+ add_index :acts_as_active_activities, :metadata, using: :gin
37
+ <% end %>
38
+
39
+ end
40
+ 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.1.3
4
+ version: 0.3.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 00:00:00.000000000 Z
11
+ date: 2025-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -69,7 +69,7 @@ files:
69
69
  - lib/acts_as_active/railtie.rb
70
70
  - lib/acts_as_active/version.rb
71
71
  - lib/generators/acts_as_active/install_generator.rb
72
- - lib/generators/acts_as_active/templates/create_activities.rb
72
+ - lib/generators/acts_as_active/templates/create_acts_as_active_activities.rb.erb
73
73
  - sig/acts_as_active.rbs
74
74
  homepage: https://www.amitleshed.com
75
75
  licenses:
@@ -1,15 +0,0 @@
1
- class CreateActivities < ActiveRecord::Migration[7.1]
2
- def change
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
9
-
10
- t.timestamps
11
- end
12
-
13
- add_index :acts_as_active_activities, [:trackable_type, :trackable_id, :occurred_on], unique: true, name: "index_activities_on_trackable_and_occurred_on"
14
- end
15
- end