rails_audit_log 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf000427595792f1a97214b6ad5d6dc7f6875681f315570260760accf4e70ba3
4
- data.tar.gz: 8978577023bb75e0832d475b8fcaeaeedb4a02ac3c5b1e1500a6d2187e1c5e5c
3
+ metadata.gz: 99754d2a1e8f4d7bba4cff5815b8bd6df8e8569f8817da525611e97a586d3114
4
+ data.tar.gz: 9fe8b370070705d1eb5685bceaa82474b63f981a51b22911db337793dabe38ff
5
5
  SHA512:
6
- metadata.gz: 897e191130a3895876e5ec8328386d5cd8e456b866a348a0c10edbacc4969883a8ca550e2fa2491f68937ed1a9558c5143a49503c9c80b4f1824789bbc40eee1
7
- data.tar.gz: d1035cdcfcc8d243fc8cfe43fac0842973e9e0dba78e16f4917c83ec83e6db81b8eb999dc441620d01027984ca4a89cdaafb8fb0501b3b98fa4712571d08a706
6
+ metadata.gz: d7122bfd0a99dd298a48597690267a050a8baac1c7d7152f16e2b5766b3e4fac60b1d334f1fdc897a35d6e5b9dd26019204497ec73386257c3eecbe02ea9e36a
7
+ data.tar.gz: fea300d386237a044f3f57eb3fe94ac0109b5d8ec4e5704bcacdf80137bfeff8bb3df2ae05ee67baaaeee72b9f6a05f86cd8da625603d87b2fd9f801e27dd1c5
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # RailsAuditLog
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/rails_audit_log.svg)](https://badge.fury.io/rb/rails_audit_log)
3
+ [![CI](https://github.com/eclectic-coding/rails_audit_log/actions/workflows/ci.yml/badge.svg)](https://github.com/eclectic-coding/rails_audit_log/actions/workflows/ci.yml)
4
+ [![Gem Version](https://img.shields.io/gem/v/rails_audit_log.svg)](https://rubygems.org/gems/rails_audit_log)
5
+ [![Downloads](https://img.shields.io/gem/dt/rails_audit_log.svg)](https://rubygems.org/gems/rails_audit_log)
4
6
  [![Ruby](https://img.shields.io/badge/ruby-%3E%3D%203.3-ruby)](https://www.ruby-lang.org)
5
7
  [![codecov](https://codecov.io/gh/eclectic-coding/rails_audit_log/branch/main/graph/badge.svg)](https://codecov.io/gh/eclectic-coding/rails_audit_log)
6
8
 
data/Rakefile CHANGED
@@ -21,9 +21,9 @@ namespace :dev do
21
21
  dummy_env = { "RAILS_ENV" => "development" }
22
22
  dummy_rake = "cd spec/dummy && bundle exec rake"
23
23
 
24
- desc "Create and migrate the development database"
24
+ desc "Create and load the development database from schema"
25
25
  task :setup do
26
- sh dummy_env, "#{dummy_rake} db:create db:migrate"
26
+ sh dummy_env, "#{dummy_rake} db:create db:schema:load"
27
27
  end
28
28
 
29
29
  desc "Seed the development database"
@@ -31,8 +31,8 @@ namespace :dev do
31
31
  sh dummy_env, "#{dummy_rake} db:seed"
32
32
  end
33
33
 
34
- desc "Drop, recreate, migrate, and seed the development database"
34
+ desc "Drop, recreate, load schema, and seed the development database"
35
35
  task :reset do
36
- sh dummy_env, "#{dummy_rake} db:drop db:create db:migrate db:seed"
36
+ sh dummy_env, "#{dummy_rake} db:drop db:create db:schema:load db:seed"
37
37
  end
38
38
  end
@@ -11,8 +11,52 @@ module RailsAuditLog
11
11
  validates :item_type, presence: true
12
12
  validates :item_id, presence: true
13
13
 
14
- scope :creates, -> { where(event: "create") }
15
- scope :updates, -> { where(event: "update") }
16
- scope :destroys, -> { where(event: "destroy") }
14
+ # Event scopes
15
+ scope :created_events, -> { where(event: "create") }
16
+ scope :updated_events, -> { where(event: "update") }
17
+ scope :destroyed_events, -> { where(event: "destroy") }
18
+
19
+ # Deprecated short aliases kept for backwards compatibility
20
+
21
+ scope :creates, -> { created_events }
22
+ scope :updates, -> { updated_events }
23
+ scope :destroys, -> { destroyed_events }
24
+
25
+ # Actor / resource scopes
26
+ scope :by_actor, ->(actor) { where(actor_type: actor.class.name, actor_id: actor.id) }
27
+ scope :for_resource, lambda { |resource|
28
+ if resource.is_a?(Class)
29
+ where(item_type: resource.name)
30
+ else
31
+ where(item_type: resource.class.name, item_id: resource.id)
32
+ end
33
+ }
34
+
35
+ # Time scopes
36
+ scope :since, ->(time) { where(created_at: time..) }
37
+ scope :until, ->(time) { where(created_at: ..time) }
38
+
39
+ # Instance methods
40
+
41
+ def changed_attributes
42
+ object_changes&.keys || []
43
+ end
44
+
45
+ def diff
46
+ return {} unless object_changes
47
+
48
+ object_changes.transform_values { |from_to| { from: from_to[0], to: from_to[1] } }
49
+ end
50
+
51
+ # Attribute scope — uses json_extract (SQLite/MySQL) or json ? key (PostgreSQL)
52
+ scope :touching, ->(attribute) {
53
+ if connection.adapter_name =~ /PostgreSQL/i
54
+ # :nocov:
55
+ where("object_changes ? :key", key: attribute.to_s)
56
+ # :nocov:
57
+ else
58
+ where("json_extract(object_changes, ?) IS NOT NULL", "$.#{attribute}")
59
+ end
60
+ }
17
61
  end
18
62
  end
@@ -1,3 +1,3 @@
1
1
  module RailsAuditLog
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_audit_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuck Smith