logga 6.0.0 → 7.1.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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +24 -1
  3. data/Rakefile +1 -2
  4. data/VERSION +1 -1
  5. data/lib/logga/active_record.rb +40 -23
  6. metadata +8 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a816cf8594e1bc18bba798c03ce28d73c05210cf396951f7f6850493deaf5029
4
- data.tar.gz: 20e9e662498165522363f68a03cc51908ba58d62762e766eecb17468acb2084f
3
+ metadata.gz: a7b492a4ed4bdf31b51aa0c37a16e30e8f5d3aa2a3251e94c4ab34ee37c6e830
4
+ data.tar.gz: 38c9bb8054121cd8c8d300621ac253f96080765a3d2d1eec5332f8af07d806b9
5
5
  SHA512:
6
- metadata.gz: f6a639733d7e1674246a118d76f28a33193a664fa3b6c7d13c2a9970b286e2da97862c7c42a752ffce76c186d862759b4a6428f3c2587d66fa42e4e63b74ff0b
7
- data.tar.gz: 0b581d6df25fa0857fa82da1a2c335f24895b0e1d67bf1b946d700caa22d70fd87f8d709f14b3d9c3e2d00efc8e5e165a6e05c317c3564a963c2858561f11047
6
+ metadata.gz: a458eb5d44c828e9021d3e6ff065862291d56a2111fe724d231a1fc670df180296fa0439800586007534bf58ede9f312664b100e7855fb522a325dd0e2976516
7
+ data.tar.gz: 731f4d712c9d5eccf03ff86fe3e98927015478ac93fbe5eb0a48b8ff84f455b83d42f66b1a9d1794ef5b72ee88e795d8b6aa28fa6794d52357080354b678aafd
data/README.md CHANGED
@@ -79,7 +79,7 @@ add_log_entries_for(
79
79
  )
80
80
  ```
81
81
 
82
- This is with the exeception on `:created_at` which only takes the created record.
82
+ This is with the exception on `:created_at` which only takes the created record.
83
83
 
84
84
  ```ruby
85
85
  add_log_entries_for(
@@ -113,6 +113,29 @@ Logga.configure do |config|
113
113
  end
114
114
  ```
115
115
 
116
+ ## Class Name
117
+
118
+ Logga automatically figures out class names and construct a sensible log line if you have not provided one. It `.demodulize` and `.titelize` the model class name to come up with the log line.
119
+
120
+ However in some cases, it may be desirable to override the default name. To support such cases, you can pass in class_name param:
121
+
122
+ ```ruby
123
+ add_log_entries_for(
124
+ :create, # Log on object create
125
+ :update, # Log on object update
126
+ class_name: "My Class"
127
+ )
128
+ ```
129
+
130
+ This will result in log lines like following:
131
+
132
+ ```ruby
133
+ my_model.update(active: true)
134
+
135
+ # will result in log entry as follows:
136
+ # "My Class active set to true"
137
+ ```
138
+
116
139
  ## Development
117
140
 
118
141
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec appraisal rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -8,5 +8,4 @@ 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"
12
- task test: :spec
11
+ task default: :appraisal if !ENV["APPRAISAL_INITIALIZED"] && !ENV["CI"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 6.0.0
1
+ 7.1.0
@@ -5,25 +5,38 @@ module Logga
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- class_attribute :allowed_fields, instance_writer: false
9
- class_attribute :excluded_fields, instance_writer: false
10
- class_attribute :fields, instance_writer: false
11
-
12
- self.allowed_fields = []
13
- self.excluded_fields = []
14
- self.fields = {}
8
+ class_attribute :logga_options, instance_writer: false
15
9
  end
16
10
 
17
11
  class_methods do
18
- def add_log_entries_for(*actions, allowed_fields: [], exclude_fields: [], fields: {}, to: :self)
12
+ def add_log_entries_for(*actions, **options)
13
+ configure_logga_options(options)
14
+ setup_logga_callbacks(actions)
15
+ define_logga_receiver_method
16
+ end
17
+
18
+ def configure_logga_options(options)
19
+ default_logga_options = {
20
+ allowed_fields: [],
21
+ excluded_fields: [],
22
+ fields: {},
23
+ class_name: nil,
24
+ to: :itself
25
+ }
26
+ self.logga_options = default_logga_options.merge(options.slice(:allowed_fields, :fields, :class_name, :to))
27
+ return if options[:allowed_fields].present?
28
+
29
+ self.logga_options = logga_options.merge(excluded_fields: options[:exclude_fields] || [])
30
+ end
31
+
32
+ def setup_logga_callbacks(actions)
19
33
  after_create :log_model_creation if actions.include?(:create)
20
34
  after_destroy :log_model_deletion if actions.include?(:delete)
21
35
  after_update :log_model_changes if actions.include?(:update)
22
- define_method(:log_receiver) { to == :self ? self : send(to) }
36
+ end
23
37
 
24
- self.allowed_fields = Array(allowed_fields)
25
- self.excluded_fields = allowed_fields.blank? ? Array(exclude_fields) : []
26
- self.fields = fields
38
+ def define_logga_receiver_method
39
+ define_method(:log_receiver) { send(logga_options[:to]) }
27
40
  end
28
41
  end
29
42
 
@@ -33,23 +46,23 @@ module Logga
33
46
  body = field_changes_to_message(changes)
34
47
  return if body.blank?
35
48
 
36
- create_log_entry(author_data.merge(body: body))
49
+ create_log_entry(author_data.merge(body:))
37
50
  end
38
51
 
39
52
  def log_model_creation
40
53
  return unless should_log?
41
54
 
42
55
  body_generator = ->(record) { default_creation_log_body(record) }
43
- body = fields.fetch(:created_at, body_generator).call(self)
44
- create_log_entry(author_data.merge(body: body, created_at: creation_at))
56
+ body = logga_options[:fields].fetch(:created_at, body_generator).call(self)
57
+ create_log_entry(author_data.merge(body:, created_at: creation_at))
45
58
  end
46
59
 
47
60
  def log_model_deletion
48
61
  return unless should_log?
49
62
 
50
63
  body_generator = ->(record) { default_deletion_log_body(record) }
51
- body = fields.fetch(:deleted_at, body_generator).call(self)
52
- create_log_entry(author_data.merge(body: body))
64
+ body = logga_options[:fields].fetch(:deleted_at, body_generator).call(self)
65
+ create_log_entry(author_data.merge(body:))
53
66
  end
54
67
 
55
68
  def log_model_changes
@@ -109,18 +122,22 @@ module Logga
109
122
  default_change_log_body(record, field, old_value, new_value)
110
123
  }
111
124
  changes.inject([]) do |result, (field, (old_value, new_value))|
112
- result << fields.fetch(field.to_sym, body_generator).call(self, field, old_value, new_value)
125
+ result << logga_options[:fields].fetch(field.to_sym, body_generator).call(self, field, old_value, new_value)
113
126
  end.compact.join("\n")
114
127
  end
115
128
 
116
129
  def reject_change?(key)
117
130
  sym_key = key.to_sym
118
- return allowed_fields.exclude?(sym_key) if allowed_fields.present?
131
+ return logga_options[:allowed_fields].exclude?(sym_key) if logga_options[:allowed_fields].present?
132
+
133
+ excluded_change?(key, sym_key)
134
+ end
119
135
 
136
+ def excluded_change?(key, sym_key)
120
137
  config_excluded_fields.include?(sym_key) ||
121
- (fields.exclude?(sym_key) &&
122
- (excluded_fields.include?(sym_key) ||
123
- config_excluded_suffixes.any? { |suffix| key.to_s.end_with?(suffix.to_s) }))
138
+ (logga_options[:fields].exclude?(sym_key) &&
139
+ (logga_options[:excluded_fields].include?(sym_key) ||
140
+ config_excluded_suffixes.any? { |suffix| key.to_s.end_with?(suffix.to_s) }))
124
141
  end
125
142
 
126
143
  def should_log?
@@ -128,7 +145,7 @@ module Logga
128
145
  end
129
146
 
130
147
  def titleized_model_class_name(record)
131
- record.class.name.demodulize.titleize
148
+ logga_options[:class_name] || record.class.name.demodulize.titleize
132
149
  end
133
150
  end
134
151
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logga
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0
4
+ version: 7.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boxt
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-11 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activerecord
@@ -16,7 +15,7 @@ dependencies:
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '7'
18
+ version: 7.1.0
20
19
  - - "<"
21
20
  - !ruby/object:Gem::Version
22
21
  version: '9'
@@ -26,7 +25,7 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- version: '7'
28
+ version: 7.1.0
30
29
  - - "<"
31
30
  - !ruby/object:Gem::Version
32
31
  version: '9'
@@ -36,7 +35,7 @@ dependencies:
36
35
  requirements:
37
36
  - - ">="
38
37
  - !ruby/object:Gem::Version
39
- version: '7'
38
+ version: 7.1.0
40
39
  - - "<"
41
40
  - !ruby/object:Gem::Version
42
41
  version: '9'
@@ -46,7 +45,7 @@ dependencies:
46
45
  requirements:
47
46
  - - ">="
48
47
  - !ruby/object:Gem::Version
49
- version: '7'
48
+ version: 7.1.0
50
49
  - - "<"
51
50
  - !ruby/object:Gem::Version
52
51
  version: '9'
@@ -70,7 +69,6 @@ licenses:
70
69
  - MIT
71
70
  metadata:
72
71
  rubygems_mfa_required: 'true'
73
- post_install_message:
74
72
  rdoc_options: []
75
73
  require_paths:
76
74
  - lib
@@ -78,15 +76,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
76
  requirements:
79
77
  - - ">="
80
78
  - !ruby/object:Gem::Version
81
- version: '3.1'
79
+ version: '3.2'
82
80
  required_rubygems_version: !ruby/object:Gem::Requirement
83
81
  requirements:
84
82
  - - ">="
85
83
  - !ruby/object:Gem::Version
86
84
  version: '0'
87
85
  requirements: []
88
- rubygems_version: 3.5.22
89
- signing_key:
86
+ rubygems_version: 4.0.6
90
87
  specification_version: 4
91
88
  summary: ActiveRecord log entries on model changes
92
89
  test_files: []