model_timeline 0.1.0 → 0.1.1
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 +17 -13
- data/lib/model_timeline/generators/templates/migration.rb.tt +2 -0
- data/lib/model_timeline/timelineable.rb +16 -5
- data/lib/model_timeline/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6a6a1184662cefc583a463199b9ba93d0962f3c3c4d6fbcf2940074f0d508c71
|
|
4
|
+
data.tar.gz: 2380ec1607093282a3de23d9d20760ff4a09db115b1f84dc572cc8a57af5ac49
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 92139b98feed631b10c0cf102c0e702f9c8e901f9dfa3d769487c45611c95ceb22271233032d05e78d91fb23417d56e42e5571bcab46ee10c3dedd201c89a12f
|
|
7
|
+
data.tar.gz: d69052dbea970f0edade2611df282db871087ac850ccabce1f0aa56888d86fed35e8c399f603578cd364fbc5ce11fb8b69c6eb0ff9df7891fb6640a482fa0d97
|
data/README.md
CHANGED
|
@@ -127,7 +127,6 @@ end
|
|
|
127
127
|
|
|
128
128
|
### Using Metadata
|
|
129
129
|
|
|
130
|
-
<!-- ! THIS IS WRONG, NEEDS TO BE UPDATED. -->
|
|
131
130
|
ModelTimeline allows you to include custom metadata with your timeline entries, which is especially useful for tracking changes across related entities or adding domain-specific context.
|
|
132
131
|
|
|
133
132
|
#### Adding Metadata Through Configuration
|
|
@@ -147,8 +146,7 @@ end
|
|
|
147
146
|
```
|
|
148
147
|
|
|
149
148
|
If your timeline table has columns that match the keys in your `meta` hash, these values will be stored in
|
|
150
|
-
those dedicated columns. Otherwise they will be
|
|
151
|
-
added to put additional metadata that doesn't have a dedicated column.)
|
|
149
|
+
those dedicated columns. Otherwise, they will be stored inside `metadata` column.
|
|
152
150
|
|
|
153
151
|
#### Adding Metadata at Runtime
|
|
154
152
|
|
|
@@ -172,13 +170,16 @@ For tracking related entities more effectively, you can create a custom timeline
|
|
|
172
170
|
class CreatePostTimelineEntries < ActiveRecord::Migration[6.1]
|
|
173
171
|
def change
|
|
174
172
|
create_table :post_timeline_entries do |t|
|
|
175
|
-
#
|
|
176
|
-
t.string
|
|
177
|
-
t.
|
|
178
|
-
t.string
|
|
179
|
-
t.jsonb
|
|
180
|
-
t.
|
|
181
|
-
t.string
|
|
173
|
+
# Default Columns - All of them are required.
|
|
174
|
+
t.string :timelineable_type
|
|
175
|
+
t.bigint :timelineable_id
|
|
176
|
+
t.string :action, null: false
|
|
177
|
+
t.jsonb :object_changes, default: {}, null: false
|
|
178
|
+
t.jsonb :metadata, default: {}, null: false
|
|
179
|
+
t.string :user_type
|
|
180
|
+
t.bigint :user_id
|
|
181
|
+
t.string :username
|
|
182
|
+
t.inet :ip_address
|
|
182
183
|
|
|
183
184
|
# Custom columns that can be populated via the meta option
|
|
184
185
|
t.integer :post_id
|
|
@@ -186,9 +187,12 @@ class CreatePostTimelineEntries < ActiveRecord::Migration[6.1]
|
|
|
186
187
|
t.timestamps
|
|
187
188
|
end
|
|
188
189
|
|
|
189
|
-
add_index :post_timeline_entries, [:timelineable_type, :timelineable_id]
|
|
190
|
-
add_index :post_timeline_entries, :
|
|
191
|
-
add_index :post_timeline_entries, :
|
|
190
|
+
add_index :post_timeline_entries, [:timelineable_type, :timelineable_id], name: 'idx_timeline_on_timelineable'
|
|
191
|
+
add_index :post_timeline_entries, [:user_type, :user_id], name: 'idx_timeline_on_user'
|
|
192
|
+
add_index :post_timeline_entries, :object_changes, using: :gin, name: 'idx_timeline_on_changes'
|
|
193
|
+
add_index :post_timeline_entries, :metadata, using: :gin, name: 'idx_timeline_on_meta'
|
|
194
|
+
add_index :post_timeline_entries, :ip_address, name: 'idx_timeline_on_ip'
|
|
195
|
+
add_index :post_timeline_entries, :post_id, name: 'idx_timeline_on_post_id'
|
|
192
196
|
end
|
|
193
197
|
end
|
|
194
198
|
```
|
|
@@ -7,6 +7,7 @@ class CreateModelTimelineTables < ActiveRecord::Migration[<%= ActiveRecord::Migr
|
|
|
7
7
|
|
|
8
8
|
# Use PostgreSQL's JSONB type for better performance
|
|
9
9
|
t.jsonb :object_changes, default: {}, null: false
|
|
10
|
+
t.jsonb :metadata, default: {}, null: false
|
|
10
11
|
|
|
11
12
|
# Polymorphic user association
|
|
12
13
|
t.string :user_type
|
|
@@ -22,6 +23,7 @@ class CreateModelTimelineTables < ActiveRecord::Migration[<%= ActiveRecord::Migr
|
|
|
22
23
|
add_index :<%= @table_name %>, [:timelineable_type, :timelineable_id], name: 'idx_timeline_on_timelineable'
|
|
23
24
|
add_index :<%= @table_name %>, [:user_type, :user_id], name: 'idx_timeline_on_user'
|
|
24
25
|
add_index :<%= @table_name %>, :object_changes, using: :gin, name: 'idx_timeline_on_changes'
|
|
26
|
+
add_index :<%= @table_name %>, :metadata, using: :gin, name: 'idx_timeline_on_meta'
|
|
25
27
|
add_index :<%= @table_name %>, :ip_address, name: 'idx_timeline_on_ip'
|
|
26
28
|
end
|
|
27
29
|
end
|
|
@@ -107,9 +107,10 @@ module ModelTimeline
|
|
|
107
107
|
timelineable_id: id,
|
|
108
108
|
action: action,
|
|
109
109
|
object_changes: object_changes,
|
|
110
|
+
metadata: object_metadata(config),
|
|
110
111
|
ip_address: current_ip_address,
|
|
111
112
|
**current_user_attributes,
|
|
112
|
-
**
|
|
113
|
+
**column_metadata(config)
|
|
113
114
|
)
|
|
114
115
|
end
|
|
115
116
|
|
|
@@ -128,9 +129,10 @@ module ModelTimeline
|
|
|
128
129
|
timelineable_id: id,
|
|
129
130
|
action: 'destroy',
|
|
130
131
|
object_changes: {},
|
|
132
|
+
metadata: object_metadata(config),
|
|
131
133
|
ip_address: current_ip_address,
|
|
132
134
|
**current_user_attributes,
|
|
133
|
-
**
|
|
135
|
+
**column_metadata(config)
|
|
134
136
|
)
|
|
135
137
|
end
|
|
136
138
|
|
|
@@ -186,8 +188,7 @@ module ModelTimeline
|
|
|
186
188
|
# @param meta_config [Hash] The metadata configuration
|
|
187
189
|
# @param config_key [String] The configuration key for this model
|
|
188
190
|
# @return [Hash] Collected metadata
|
|
189
|
-
def collect_metadata(meta_config
|
|
190
|
-
config = self.class.loggers[config_key]
|
|
191
|
+
def collect_metadata(meta_config)
|
|
191
192
|
metadata = {}
|
|
192
193
|
|
|
193
194
|
# First, add any thread-level metadata
|
|
@@ -206,9 +207,19 @@ module ModelTimeline
|
|
|
206
207
|
metadata[key] = resolved_value
|
|
207
208
|
end
|
|
208
209
|
|
|
209
|
-
|
|
210
|
+
metadata
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def column_metadata(config)
|
|
214
|
+
metadata = collect_metadata(config[:meta])
|
|
210
215
|
column_names = config[:klass].column_names.map(&:to_sym)
|
|
211
216
|
metadata.slice(*column_names)
|
|
212
217
|
end
|
|
218
|
+
|
|
219
|
+
def object_metadata(config)
|
|
220
|
+
metadata = collect_metadata(config[:meta])
|
|
221
|
+
column_names = config[:klass].column_names.map(&:to_sym)
|
|
222
|
+
metadata.except(*column_names)
|
|
223
|
+
end
|
|
213
224
|
end
|
|
214
225
|
end
|