logga 3.0.0 → 5.0.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: 156b89d500e231c81261932228af86c934bf2a95d57eaeed5016aa98fcd8ce72
4
- data.tar.gz: 6662b604701dc7a5422863977d7d2dbef2549af382f1bf7f038afb72c7d032e6
3
+ metadata.gz: d75ebd3e7aaf71d362a3434cfdf18c443f9a9e2211f493f9263f1297c73f1bca
4
+ data.tar.gz: 59c3bcfba124a2cff37290d364527bcedd26c9c2ddc9fcd290bdd7b3323f4181
5
5
  SHA512:
6
- metadata.gz: 7c13d5319ed50c2114252f174c5433b8b67a5c91ce78dca3f22f238f8c62c963e2a5a98ac21e68f5d70b89f95b02d80a94dc3de990f823fa1a100e31a60ca621
7
- data.tar.gz: 0bcdbdffe2682e5d21ec90f02b4dc2cdf26fa1fb3ebe0f2893b69573b4bbd903526b983b59783ed3a37a11d4960c0f023ba47562432db8cc600a65522dbfcbe9
6
+ metadata.gz: 638afbac5bd2330208069a0f4c725090fb88cb7e3785f64e1cf6a1d6c4255c5e7f39a7b92fc7481061e135a0e8765129ec4b12e06a48148e421b5f3cf62e6bc3
7
+ data.tar.gz: 6d07832218be126bb2328b72d0d2aba1b2640b46d830cf6a087e3b45fd1221d5947869ad892b2db33aff4f024304f94fe79811e4288aaa9a1449ca6084d1470e
data/README.md CHANGED
@@ -7,7 +7,7 @@ Provides attribute logging functionality to ActiveRecord objects.
7
7
 
8
8
  ## Requirements
9
9
 
10
- Requires Ruby 2.7.3 or above
10
+ - Ruby >= 3.0
11
11
 
12
12
  ## Installation
13
13
 
@@ -31,27 +31,94 @@ gem install logga
31
31
 
32
32
  ## Usage
33
33
 
34
- Add this to your model:
34
+ Add the following to your model:
35
35
 
36
36
  ```ruby
37
- class Order < ApplicationRecord
38
- add_log_entries_for :create, :update
37
+ class Thing < ApplicationRecord
38
+ # Optional author accessor. See #author
39
+ attr_accessor :author
40
+
41
+ # Association to :log_entries, which the loggable object must response to for logging.
42
+ has_many :log_entries, as: :loggable, dependent: :destroy
43
+
44
+ add_log_entries_for(
45
+ :create, # Log on object create
46
+ :delete, # Log on object delete
47
+ :update, # Log on object update
48
+ allowed_fields: [], # set an array of fields allowed to be logged
49
+ exclude_fields: [], # set an array of fields excluded from logging. Ignored if allowed_fields is set
50
+ fields: {}, # Custom messages for fields. See #fields
51
+ to: nil
52
+ )
39
53
  end
40
54
  ```
41
55
 
42
- So that new LogEntry records attached to a given Order instance will be created whenever a new one is created or
56
+ So that new `LogEntry` records attached to a given `Thing` instance will be created whenever a new one is created or
43
57
  modified.
44
58
 
59
+ ## Author
60
+
61
+ If you want to log the author of the changes you can do so by setting:
62
+
63
+ ```ruby
64
+ thing.author = { id: "1", name: "Barry", type: "User" }
65
+ ```
66
+
67
+ ## Fields
68
+
69
+ You can override the default messages per field by using:
70
+
71
+ ```ruby
72
+ add_log_entries_for(
73
+ :update,
74
+ fields: {
75
+ name: lambda { |record, field, old_value, new_value|
76
+ "Name changed from #{old_value} to #{new_value}"
77
+ }
78
+ }
79
+ )
80
+ ```
81
+
82
+ This is with the exeception on `:created_at` which only takes the created record.
83
+
84
+ ```ruby
85
+ add_log_entries_for(
86
+ :create,
87
+ fields: {
88
+ created_at: lambda { |record|
89
+ "Created object with id: #{record.id}"
90
+ }
91
+ }
92
+ )
93
+ ```
94
+
95
+ ## Configuration
96
+
97
+ Add an initializer to your project:
98
+
99
+ ```ruby
100
+ Logga.configure do |config|
101
+ config.enabled = true
102
+ config.excluded_fields = [] # Default array of excluded fields i.e. [:id] to ignore all :id fields for every object
103
+ config.excluded_suffixes = [] # Array of excluded suffixes i.e. [:_id] to ignore all fields that end in :_id for every object
104
+ end
105
+ ```
106
+
107
+ For example:
108
+
109
+ ```ruby
110
+ Logga.configure do |config|
111
+ config.excluded_fields = [:id] # Don't log any id changes
112
+ config.excluded_suffixes = [_id] # Don't log any column that ends in _id
113
+ end
114
+ ```
115
+
45
116
  ## Development
46
117
 
47
118
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
119
 
49
120
  To install this gem onto your local machine, run `bundle exec rake install`.
50
121
 
51
- ## Versioning
52
-
53
- The version of the gem should be set in the `VERSION` file found in the root of the project. This is then read by the `lib/boxt_aasm_ext/version.rb` file to set in the gem.
54
-
55
122
  ## Contributing
56
123
 
57
124
  Bug reports and pull requests are welcome on GitHub at https://github.com/boxt/logga.
@@ -62,6 +129,5 @@ The gem is available as open source under the terms of the [MIT License](http://
62
129
 
63
130
  ## TODOs
64
131
 
65
- - Write some tests
66
132
  - Improve the documentation
67
- - Remove explicit `EXCLUDED_KEYS` and `EXCLUDED_SUFFIXES` for something that can be set in an initializer
133
+ - Add migration generator for `:log_entries`
data/Rakefile CHANGED
@@ -8,4 +8,5 @@ RSpec::Core::RakeTask.new(:spec) do |t|
8
8
  t.pattern = Dir.glob("spec/**/*_spec.rb")
9
9
  end
10
10
 
11
+ desc "Map rake test to rake spec"
11
12
  task test: :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 5.0.0
@@ -4,21 +4,18 @@ module Logga
4
4
  module ActiveRecord
5
5
  extend ActiveSupport::Concern
6
6
 
7
- EXCLUDED_KEYS = %i[id created_at deleted_at initial updated_at log sent_after_sales_emails].freeze
8
- EXCLUDED_SUFFIXES = %i[_id _filenames].freeze
9
-
10
7
  included do
11
8
  class_attribute :allowed_fields, instance_writer: false
12
9
  class_attribute :excluded_fields, instance_writer: false
13
- class_attribute :log_fields, instance_writer: false
10
+ class_attribute :fields, instance_writer: false
14
11
 
15
12
  self.allowed_fields = []
16
13
  self.excluded_fields = []
17
- self.log_fields = {}
14
+ self.fields = {}
18
15
  end
19
16
 
20
17
  class_methods do
21
- def add_log_entries_for(*actions, to: :self, fields: {}, allowed_fields: [], exclude_fields: [])
18
+ def add_log_entries_for(*actions, allowed_fields: [], exclude_fields: [], fields: {}, to: :self)
22
19
  after_create :log_model_creation if actions.include?(:create)
23
20
  after_destroy :log_model_deletion if actions.include?(:delete)
24
21
  after_update :log_model_changes if actions.include?(:update)
@@ -26,7 +23,7 @@ module Logga
26
23
 
27
24
  self.allowed_fields = Array(allowed_fields)
28
25
  self.excluded_fields = allowed_fields.blank? ? Array(exclude_fields) : []
29
- self.log_fields = fields
26
+ self.fields = fields
30
27
  end
31
28
  end
32
29
 
@@ -34,27 +31,29 @@ module Logga
34
31
  return if changes.blank?
35
32
 
36
33
  body = field_changes_to_message(changes)
37
- create_log_entry(author_data.merge(body: body)) if body.present?
34
+ return if body.blank?
35
+
36
+ create_log_entry(author_data.merge(body: body))
38
37
  end
39
38
 
40
39
  def log_model_creation
41
- return if should_not_log?
40
+ return unless should_log?
42
41
 
43
42
  body_generator = ->(record) { default_creation_log_body(record) }
44
- body = log_fields.fetch(:created_at, body_generator).call(self)
43
+ body = fields.fetch(:created_at, body_generator).call(self)
45
44
  create_log_entry(author_data.merge(body: body, created_at: creation_at))
46
45
  end
47
46
 
48
47
  def log_model_deletion
49
- return if should_not_log?
48
+ return unless should_log?
50
49
 
51
50
  body_generator = ->(record) { default_deletion_log_body(record) }
52
- body = log_fields.fetch(:deleted_at, body_generator).call(self)
51
+ body = fields.fetch(:deleted_at, body_generator).call(self)
53
52
  create_log_entry(author_data.merge(body: body))
54
53
  end
55
54
 
56
55
  def log_model_changes
57
- return if should_not_log?
56
+ return unless should_log?
58
57
 
59
58
  field_changes = previous_changes.reject { |k, _| reject_change?(k) }
60
59
  log_field_changes(field_changes)
@@ -64,13 +63,22 @@ module Logga
64
63
 
65
64
  def author_data
66
65
  data = Hash(log_receiver.try(:author) || try(:author)).with_indifferent_access
66
+
67
67
  {
68
68
  author_id: data[:id],
69
- author_type: data[:type],
70
- author_name: data[:name]
69
+ author_name: data[:name],
70
+ author_type: data[:type]
71
71
  }
72
72
  end
73
73
 
74
+ def config_excluded_fields
75
+ Logga.configuration.excluded_fields
76
+ end
77
+
78
+ def config_excluded_suffixes
79
+ Logga.configuration.excluded_suffixes
80
+ end
81
+
74
82
  def create_log_entry(entry)
75
83
  log_receiver&.log_entries&.create(entry)
76
84
  end
@@ -82,10 +90,7 @@ module Logga
82
90
  end
83
91
 
84
92
  def default_creation_log_body(record)
85
- [
86
- "#{titleized_model_class_name(record)} created",
87
- ("(#{record.state})" if record.try(:state))
88
- ].compact.join(" ")
93
+ "#{titleized_model_class_name(record)} created"
89
94
  end
90
95
 
91
96
  def default_change_log_body(record, field, _old_value, new_value)
@@ -104,7 +109,7 @@ module Logga
104
109
  default_change_log_body(record, field, old_value, new_value)
105
110
  }
106
111
  changes.inject([]) do |result, (field, (old_value, new_value))|
107
- result << log_fields.fetch(field.to_sym, body_generator).call(self, field, old_value, new_value)
112
+ result << fields.fetch(field.to_sym, body_generator).call(self, field, old_value, new_value)
108
113
  end.compact.join("\n")
109
114
  end
110
115
 
@@ -112,14 +117,14 @@ module Logga
112
117
  sym_key = key.to_sym
113
118
  return allowed_fields.exclude?(sym_key) if allowed_fields.present?
114
119
 
115
- EXCLUDED_KEYS.include?(sym_key) ||
116
- (log_fields.exclude?(sym_key) &&
120
+ config_excluded_fields.include?(sym_key) ||
121
+ (fields.exclude?(sym_key) &&
117
122
  (excluded_fields.include?(sym_key) ||
118
- EXCLUDED_SUFFIXES.any? { |suffix| key.to_s.end_with?(suffix.to_s) }))
123
+ config_excluded_suffixes.any? { |suffix| key.to_s.end_with?(suffix.to_s) }))
119
124
  end
120
125
 
121
- def should_not_log?
122
- !log_receiver.respond_to?(:log_entries)
126
+ def should_log?
127
+ Logga.enabled? && log_receiver.respond_to?(:log_entries)
123
128
  end
124
129
 
125
130
  def titleized_model_class_name(record)
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logga
4
+ class Config
5
+ attr_accessor :enabled, :excluded_fields, :excluded_suffixes
6
+
7
+ def initialize(enabled: true, excluded_fields: [], excluded_suffixes: [])
8
+ @enabled = enabled
9
+ @excluded_fields = excluded_fields
10
+ @excluded_suffixes = excluded_suffixes
11
+ end
12
+ end
13
+ end
data/lib/logga.rb CHANGED
@@ -3,11 +3,37 @@
3
3
  require "active_support"
4
4
  require "active_support/core_ext"
5
5
  require "active_support/concern"
6
- require_relative "logga/version"
6
+
7
7
  require_relative "logga/active_record"
8
+ require_relative "logga/config"
9
+ require_relative "logga/version"
8
10
 
9
11
  module Logga
10
12
  ActiveSupport.on_load(:active_record) do
11
13
  include Logga::ActiveRecord
12
14
  end
15
+
16
+ class << self
17
+ def configuration
18
+ @configuration ||= Config.new
19
+ end
20
+
21
+ def configure
22
+ yield(configuration)
23
+ end
24
+
25
+ # Switches Logga on or off
26
+ def enabled=(value)
27
+ configuration.enabled = value
28
+ end
29
+
30
+ # Returns `true` if Logga is on, `false` otherwise
31
+ def enabled?
32
+ !!configuration.enabled
33
+ end
34
+
35
+ def version
36
+ Logga::VERSION
37
+ end
38
+ end
13
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logga
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boxt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-07 00:00:00.000000000 Z
11
+ date: 2024-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -63,6 +63,7 @@ files:
63
63
  - VERSION
64
64
  - lib/logga.rb
65
65
  - lib/logga/active_record.rb
66
+ - lib/logga/config.rb
66
67
  - lib/logga/version.rb
67
68
  homepage: https://github.com/boxt/logga
68
69
  licenses:
@@ -77,14 +78,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
78
  requirements:
78
79
  - - ">="
79
80
  - !ruby/object:Gem::Version
80
- version: '2.7'
81
+ version: '3.0'
81
82
  required_rubygems_version: !ruby/object:Gem::Requirement
82
83
  requirements:
83
84
  - - ">="
84
85
  - !ruby/object:Gem::Version
85
86
  version: '0'
86
87
  requirements: []
87
- rubygems_version: 3.2.22
88
+ rubygems_version: 3.5.3
88
89
  signing_key:
89
90
  specification_version: 4
90
91
  summary: ActiveRecord log entries on model changes