logga 7.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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +24 -1
  3. data/VERSION +1 -1
  4. data/lib/logga/active_record.rb +37 -20
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 685871096fd248182a308edbac9b01f080db2a221175fcf034f90c9f5ed62bec
4
- data.tar.gz: 2a458597ad7163614a50029c8935e79b70012bd2083accbada811d7fb00f939f
3
+ metadata.gz: a7b492a4ed4bdf31b51aa0c37a16e30e8f5d3aa2a3251e94c4ab34ee37c6e830
4
+ data.tar.gz: 38c9bb8054121cd8c8d300621ac253f96080765a3d2d1eec5332f8af07d806b9
5
5
  SHA512:
6
- metadata.gz: 8083f55faa1eeba8b0b02a12bdd44f129181be47a80f6d31affffcfe9f1e44144cb2110cf1c98d62f4810794fddd6255d35e583679ec1826a07316a7e27e5e26
7
- data.tar.gz: b566d51b5e5cf8eec6a0f4a34adefc841f5eb1ff6a832fccaa1edef21613f07a598f0b504d891c9751f21bbfc0e938b6726b79419677f2a5a0f8174020f62fa9
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/VERSION CHANGED
@@ -1 +1 @@
1
- 7.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
 
@@ -40,7 +53,7 @@ module Logga
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)
56
+ body = logga_options[:fields].fetch(:created_at, body_generator).call(self)
44
57
  create_log_entry(author_data.merge(body:, created_at: creation_at))
45
58
  end
46
59
 
@@ -48,7 +61,7 @@ module Logga
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)
64
+ body = logga_options[:fields].fetch(:deleted_at, body_generator).call(self)
52
65
  create_log_entry(author_data.merge(body:))
53
66
  end
54
67
 
@@ -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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logga
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.0
4
+ version: 7.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boxt
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  requirements: []
86
- rubygems_version: 3.6.9
86
+ rubygems_version: 4.0.6
87
87
  specification_version: 4
88
88
  summary: ActiveRecord log entries on model changes
89
89
  test_files: []