acts_as_active 0.2.0 → 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: 8d77b8db36fd55b8e5a5fd11c5d3c66086ca2edc782802accbc6dd868f7edd0e
4
- data.tar.gz: c815c49b4daaa44a2b2dc752c744016ca3125a31be46700342053270759be6b6
3
+ metadata.gz: c452d929cd97ab9aa78b33e2b6f470c05e54ef62771b5ab4b7894c567dfd0549
4
+ data.tar.gz: 26205d4a6a40162ee0ca68a9e0d622d802aecf4de082dc324ebb4403be3c1db5
5
5
  SHA512:
6
- metadata.gz: f2bd60fd0f26c0e240179e2980c552aa69a83907742a33e3d979530ac4613e0227ce3c7357017ff22baa81019617809997e7af9e8fe9e2d19375dc385e89002d
7
- data.tar.gz: 4fad00c674233c6b94b4eb4f2dbd1fe6011ac414cfa5946c85c9f2d31596dcf207c034d58c15c61c81be93a3981b95ff613fc44979af0ecfc3f50819d187849a
6
+ metadata.gz: ebc15ecf2c2ba7b55c53c6bce2d32b8c37b15602e0cf4685425337e66e42d9df5c0f11ee58c5d8813d637fd290706230b233df5c87982808ccebe9d22e871abd
7
+ data.tar.gz: 1043c825d41ad29fa487ade82b69be49cecb1810c05229282b1e1ff1fdd28a95d0a6190d58918b21344f722bac8911a916e27224396275e47c3e5d7739bc9ac0
data/README.md CHANGED
@@ -18,6 +18,12 @@ 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
@@ -25,7 +31,7 @@ class Record < ApplicationRecord
25
31
  acts_as_active on: [:create, :update], # ➜ Track different actions
26
32
  if: -> { track_activity? }, # ➜ On different conditions
27
33
  unless: -> { skip_tracking? },
28
- after_record: -> ( activity ) { puts "Modified: #{activity}" } # ➜ Hook: runs after an activity is created or updated
34
+ after_record: -> (activity) { activity.update(metadata: {be: "BOP!"}) } # ➜ Hook: runs after an activity is created or updated
29
35
  end
30
36
  ```
31
37
 
@@ -57,6 +63,40 @@ ActsAsActive::Activity.where(trackable: record) # Query the namespaced model di
57
63
  ActsAsActive::Activity.all # Get all activities across all trackable types
58
64
  ```
59
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
+
60
100
  ## Run Tests
61
101
 
62
102
  ```bash
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsActive
4
- VERSION = "0.2.0"
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.2.0
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