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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 972197d1a2d63936b7f66950db15d0410575b4f0
4
- data.tar.gz: 27cc2dd5644cf4f92b959974f5fb103a1cd39b1b
3
+ metadata.gz: d4b1c7d019d794088470dbc81796a8347b428218
4
+ data.tar.gz: d64386c06c75aa8896197bd6cc95a74f83148307
5
5
  SHA512:
6
- metadata.gz: bf3aa0b8339b66b07ea33916601a9beee5c3b3c2a9a68a64d8ad1c0403bd8bd17017584261653e3e91ca8188b9d0dad9beb969507940939ec093ce6086b97bd0
7
- data.tar.gz: 1481e088f54ea6e5cdd93849976184bb5c39e359ad49c850bd234d70dfef292947103c27b7efc2c6b4d8eedce235bbbe50c1620f28fa99bd79ed50ed79ab1ace
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.titleize} changed from '#{before}' to '#{after}'", { attribute: attribute, before: before, after: after })
56
+ log("#{attribute} changed from #{before.presence || BLANK} to #{after.presence || BLANK}", { attribute: attribute, before: before, after: after })
46
57
  else
47
- log("#{attribute.titleize} set to '#{before}'", { attribute: attribute, value: before })
58
+ log("#{attribute} set to #{before || BLANK}", { attribute: attribute, value: before })
48
59
  end
49
60
  end
50
61
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveLogging
2
- VERSION = '1.5.4'.freeze
2
+ VERSION = '1.5.5'.freeze
3
3
  end
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
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-16 00:00:00.000000000 Z
11
+ date: 2016-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails