effective_logging 1.5.4 → 1.5.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -0
- data/app/models/concerns/acts_as_loggable.rb +10 -0
- data/lib/effective_logging/active_record_logger.rb +13 -2
- data/lib/effective_logging/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4b1c7d019d794088470dbc81796a8347b428218
|
4
|
+
data.tar.gz: d64386c06c75aa8896197bd6cc95a74f83148307
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96945e6afd19f0fa439b87920e820f18a19806c2879d74a36919fc2d875a469d50b0fdeeb600368bf56cae67033f85d590a9dd54e2d57e21364b4f42a61bbc59
|
7
|
+
data.tar.gz: 192d2f2dfe7b82c420430b480a1f9ed0ce0b7360d716efe5c15f92b30302a9fe2c50d74fc5507f1810bc9d52950fa567c2c7fe88a6db4cbb2242db6f67aedbd0
|
data/README.md
CHANGED
@@ -232,6 +232,28 @@ There is some initial support for passing `only`, `except`, and `additionally` t
|
|
232
232
|
|
233
233
|
Define your model with `log_changes additionally: [:method1, :method2]` to also log the value of methods.
|
234
234
|
|
235
|
+
Apply your own formatting to the logged title of each attribute by creating an instance method on the resource:
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
# Format the title of this attribute. Return nil to use the default attribute.titleize
|
239
|
+
def log_changes_formatted_attribute(attribute)
|
240
|
+
attribute.downcase
|
241
|
+
end
|
242
|
+
```
|
243
|
+
|
244
|
+
Apply your own formatting to the logged before and after values of each attribute by creating an instance method on the resource:
|
245
|
+
|
246
|
+
```ruby
|
247
|
+
# Format the value of this attribute. Return nil to use the default to_s
|
248
|
+
def log_changes_formatted_value(attribute, value)
|
249
|
+
if ['cost'].include?(attribute)
|
250
|
+
ActionController::Base.helpers.number_to_currency(value)
|
251
|
+
elsif ['percentage'].include?(attribute)
|
252
|
+
ActionController::Base.helpers.number_to_percentage(value)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
```
|
256
|
+
|
235
257
|
### Logging From JavaScript
|
236
258
|
|
237
259
|
First, require the javascript in your application.js:
|
@@ -51,6 +51,16 @@ module ActsAsLoggable
|
|
51
51
|
|
52
52
|
# Regular instance methods
|
53
53
|
|
54
|
+
# Format the title of this attribute. Return nil to use the default attribute.titleize
|
55
|
+
def log_changes_formatted_attribute(attribute)
|
56
|
+
# Intentionally does nothing
|
57
|
+
end
|
58
|
+
|
59
|
+
# Format the value of this attribute. Return nil to use the default to_s
|
60
|
+
def log_changes_formatted_value(attribute, value)
|
61
|
+
# Intentionally does nothing
|
62
|
+
end
|
63
|
+
|
54
64
|
def log_changes_datatable
|
55
65
|
if persisted?
|
56
66
|
@log_changes_datatable ||= (
|
@@ -2,6 +2,8 @@ module EffectiveLogging
|
|
2
2
|
class ActiveRecordLogger
|
3
3
|
attr_accessor :resource, :logger, :depth, :options
|
4
4
|
|
5
|
+
BLANK = "''"
|
6
|
+
|
5
7
|
def initialize(resource, options = {})
|
6
8
|
raise ArgumentError.new('options must be a Hash') unless options.kind_of?(Hash)
|
7
9
|
raise ArgumentError.new('logger must respond to logged_changes') unless (options[:logger] || resource).respond_to?(:logged_changes)
|
@@ -41,10 +43,19 @@ module EffectiveLogging
|
|
41
43
|
# before_save
|
42
44
|
def changed!
|
43
45
|
applicable(changes).each do |attribute, (before, after)|
|
46
|
+
if resource.respond_to?(:log_changes_formatted_value)
|
47
|
+
before = resource.log_changes_formatted_value(attribute, before) || before
|
48
|
+
after = resource.log_changes_formatted_value(attribute, after) || after
|
49
|
+
end
|
50
|
+
|
51
|
+
attribute = if resource.respond_to?(:log_changes_formatted_attribute)
|
52
|
+
resource.log_changes_formatted_attribute(attribute)
|
53
|
+
end || attribute.titleize
|
54
|
+
|
44
55
|
if after.present?
|
45
|
-
log("#{attribute
|
56
|
+
log("#{attribute} changed from #{before.presence || BLANK} to #{after.presence || BLANK}", { attribute: attribute, before: before, after: after })
|
46
57
|
else
|
47
|
-
log("#{attribute
|
58
|
+
log("#{attribute} set to #{before || BLANK}", { attribute: attribute, value: before })
|
48
59
|
end
|
49
60
|
end
|
50
61
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: effective_logging
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code and Effect
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|