effective_logging 2.0.8 → 2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: deba877f9d52dbbb410ed1ea2a9996f6dbff4d16
4
- data.tar.gz: 3c6bc27bd1a2cd2e3a6b2a87bc8590cc68b85671
3
+ metadata.gz: 7a608aab5a3d18fc5904353993d99f79d5661512
4
+ data.tar.gz: f92d295365c081c2fc2656644d9826033dc7208d
5
5
  SHA512:
6
- metadata.gz: 87675d323a2666c478c6ce4258d4e0a0b034cb963e8371bc674112bc43cdcd98f84be588a72b4a077d2b77caebaba59fa6be1065d725352b271dbf92e113ce38
7
- data.tar.gz: f28003a2a9cdf88f75b6fed98f097f762820af5e356b21ac812c05889cd0421707220f2da89b72d66e176895c5f6c45ac370ba98d971004dd0e2ded346b7916d
6
+ metadata.gz: 41199b4ae698d4b6204979a76116bafacf9c9d2b25c741631938c174fc18ab090e22d987417dcfcecb3e27ee21ada1cdf40254aca0ebe3921accda85f79daf8b
7
+ data.tar.gz: 2eb2d0f92b144e7517bd752d927d4f4513f0becf7fa7ba8b34bf0c583d1945002f24c8cee43c20ecde45c0ae06a18405039a4951d9d15b3ac49e2539671d2a22
@@ -9,7 +9,7 @@ module ActsAsLoggable
9
9
  raise ArgumentError.new('invalid arguments passed to (effective_logging) log_changes. Example usage: log_changes except: [:created_at]')
10
10
  end
11
11
 
12
- if (unknown = (@acts_as_loggable_options.keys - [:only, :except, :additionally, :include_associated])).present?
12
+ if (unknown = (@acts_as_loggable_options.keys - [:only, :except, :additionally, :include_associated, :include_nested])).present?
13
13
  raise ArgumentError.new("unknown keyword: #{unknown.join(', ')}")
14
14
  end
15
15
 
@@ -20,39 +20,12 @@ module ActsAsLoggable
20
20
  included do
21
21
  has_many :logged_changes, -> { order(:id).where(status: EffectiveLogging.log_changes_status) }, as: :associated, class_name: 'Effective::Log'
22
22
 
23
- around_save(unless: -> { EffectiveLogging.supressed? }) do |_, block|
24
- @acts_as_loggable_new_record = new_record?
25
-
26
- unless @acts_as_loggable_new_record
27
- @acts_as_loggable_update_record = ::EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).execute!
28
- end
29
-
30
- block.call
31
- true
32
- end
33
-
34
- before_destroy(unless: -> { EffectiveLogging.supressed? }) do
35
- @acts_as_loggable_destroy_record = true
36
- ::EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).destroyed!
37
- true
38
- end
39
-
40
- after_commit(unless: -> { EffectiveLogging.supressed? }) do
41
- if @acts_as_loggable_new_record
42
- ::EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).created!
43
- elsif !@acts_as_loggable_destroy_record && @acts_as_loggable_update_record
44
- ::EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).updated!
45
- end
46
-
47
- true
48
- end
49
-
50
- # Parse Options
51
23
  log_changes_options = {
52
24
  only: Array(@acts_as_loggable_options[:only]).map { |attribute| attribute.to_s },
53
25
  except: Array(@acts_as_loggable_options[:except]).map { |attribute| attribute.to_s },
54
26
  additionally: Array(@acts_as_loggable_options[:additionally]).map { |attribute| attribute.to_s },
55
- include_associated: @acts_as_loggable_options.fetch(:include_associated, true)
27
+ include_associated: @acts_as_loggable_options.fetch(:include_associated, true),
28
+ include_nested: @acts_as_loggable_options.fetch(:include_nested, true)
56
29
  }
57
30
 
58
31
  if name == 'User'
@@ -60,6 +33,18 @@ module ActsAsLoggable
60
33
  end
61
34
 
62
35
  self.send(:define_method, :log_changes_options) { log_changes_options }
36
+
37
+ after_create(unless: -> { EffectiveLogging.supressed? }) do
38
+ ::EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).execute!
39
+ end
40
+
41
+ after_destroy(unless: -> { EffectiveLogging.supressed? }) do
42
+ ::EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).execute!
43
+ end
44
+
45
+ after_update(unless: -> { EffectiveLogging.supressed? }) do
46
+ ::EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).execute!
47
+ end
63
48
  end
64
49
 
65
50
  module ClassMethods
@@ -1,17 +1,18 @@
1
1
  .panel.panel-default.card
2
2
  .panel-heading.card-header
3
3
  .row
4
- .col-md-10
4
+ .col-md-8
5
5
  - parents_of_log(log).each do |parent|
6
6
  = link_to(log.message, request.fullpath.sub('/' + log.to_param, '/' + parent.to_param))
7
7
  = ' > '
8
8
  %p= log.message.html_safe
9
- .col-md-2.text-right
9
+
10
+ .col-md-4.text-right
10
11
  - if log.prev_log
11
12
  = link_to 'Prev', request.fullpath.sub('/' + log.to_param, '/' + log.prev_log.to_param), class: 'btn btn-primary'
12
13
 
13
14
  - if log.next_log
14
- = link_to 'Next', request.fullpath.sub('/' + log.to_param, '/' + log.prev_log.to_param), class: 'btn btn-primary'
15
+ = link_to 'Next', request.fullpath.sub('/' + log.to_param, '/' + log.next_log.to_param), class: 'btn btn-primary'
15
16
 
16
17
  .panel-body.card-body
17
18
  .row
@@ -1,6 +1,6 @@
1
1
  module EffectiveLogging
2
2
  class ActiveRecordLogger
3
- attr_accessor :object, :resource, :logger, :depth, :include_associated, :options
3
+ attr_accessor :object, :resource, :logger, :depth, :include_associated, :include_nested, :options
4
4
 
5
5
  BLANK = "''"
6
6
 
@@ -9,49 +9,46 @@ module EffectiveLogging
9
9
 
10
10
  @object = object
11
11
  @resource = Effective::Resource.new(object)
12
- @logged = false # If work was done
13
12
 
14
13
  @logger = options.delete(:logger) || object
15
14
  @depth = options.delete(:depth) || 0
16
15
  @include_associated = options.fetch(:include_associated, true)
16
+ @include_nested = options.fetch(:include_nested, true)
17
17
  @options = options
18
18
 
19
19
  raise ArgumentError.new('logger must respond to logged_changes') unless @logger.respond_to?(:logged_changes)
20
20
  end
21
21
 
22
- # execute! is called when we recurse, otherwise the following methods are best called individually
23
22
  def execute!
24
- if object.new_record?
23
+ @logged = false
24
+
25
+ if new_record?(object)
25
26
  created!
26
- elsif object.marked_for_destruction?
27
+ elsif destroyed_record?(object)
27
28
  destroyed!
28
29
  else
29
- changed!
30
+ updated!
30
31
  end
31
32
 
32
- log_nested_resources! if include_associated
33
-
34
- @logged
33
+ logged?
35
34
  end
36
35
 
37
- # before_destroy
38
36
  def destroyed!
39
- log('Deleted')
37
+ log('Deleted', details: applicable(instance_attributes))
40
38
  end
41
39
 
42
- # after_commit
43
40
  def created!
44
- log('Created', details: applicable(resource.instance_attributes(include_associated: include_associated)))
41
+ log('Created', details: applicable(instance_attributes))
45
42
  end
46
43
 
47
- # after_commit
48
44
  def updated!
49
- log('Updated', details: applicable(resource.instance_attributes(include_associated: include_associated)))
50
- end
45
+ log_resource_changes!
46
+ log_nested_resources!
51
47
 
52
- private
48
+ log('Updated', details: applicable(instance_attributes)) if logged? && depth == 0
49
+ end
53
50
 
54
- def changed!
51
+ def log_resource_changes!
55
52
  applicable(resource.instance_changes).each do |attribute, (before, after)|
56
53
  if object.respond_to?(:log_changes_formatted_value)
57
54
  before = object.log_changes_formatted_value(attribute, before) || before
@@ -70,17 +67,29 @@ module EffectiveLogging
70
67
  end
71
68
 
72
69
  def log_nested_resources!
70
+ return unless include_nested
71
+
73
72
  # Log changes on all accepts_as_nested_parameters has_many associations
74
73
  resource.nested_resources.each do |association|
75
74
  title = association.name.to_s.singularize.titleize
76
75
 
77
76
  Array(object.send(association.name)).each_with_index do |child, index|
78
- child_options = options.merge(logger: logger, depth: depth+1, include_associated: include_associated, prefix: "#{title} #{index} - #{child} - ")
77
+ next unless child.present?
78
+
79
+ child_options = options.merge(logger: logger, depth: depth+1, prefix: "#{title} #{index} - #{child} - ", include_associated: include_associated, include_nested: include_nested)
80
+ child_options = child_options.merge(child.log_changes_options) if child.respond_to?(:log_changes_options)
81
+
79
82
  @logged = true if ::EffectiveLogging::ActiveRecordLogger.new(child, child_options).execute!
80
83
  end
81
84
  end
82
85
  end
83
86
 
87
+ private
88
+
89
+ def instance_attributes # effective_resources gem
90
+ resource.instance_attributes(include_associated: include_associated, include_nested: include_nested)
91
+ end
92
+
84
93
  def log(message, details: {})
85
94
  @logged = true
86
95
 
@@ -96,9 +105,12 @@ module EffectiveLogging
96
105
  log
97
106
  end
98
107
 
108
+ def logged?
109
+ @logged == true
110
+ end
111
+
99
112
  # TODO: Make this work better with nested objects
100
113
  def applicable(attributes)
101
-
102
114
  atts = if options[:only].present?
103
115
  attributes.stringify_keys.slice(*options[:only])
104
116
  elsif options[:except].present?
@@ -115,7 +127,22 @@ module EffectiveLogging
115
127
  end
116
128
 
117
129
  # Blacklist
118
- atts.except(:logged_changes, :trash, 'logged_changes', 'trash')
130
+ atts.except(:logged_changes, :trash, :updated_at, 'logged_changes', 'trash', 'updated_at')
131
+ end
132
+
133
+ def new_record?(object)
134
+ return true if object.respond_to?(:new_record?) && object.new_record?
135
+ return true if object.respond_to?(:id_was) && object.id_was.nil?
136
+ return true if object.respond_to?(:previous_changes) && object.previous_changes.key?('id') && object.previous_changes['id'].first.nil?
137
+ false
138
+ end
139
+
140
+ def destroyed_record?(object)
141
+ return true if object.respond_to?(:destroyed?) && object.destroyed?
142
+ return true if object.respond_to?(:marked_for_destruction?) && object.marked_for_destruction?
143
+ return true if object.respond_to?(:previous_changes) && object.previous_changes.key?('id') && object.previous_changes['id'].last.nil?
144
+ false
119
145
  end
120
146
  end
147
+
121
148
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveLogging
2
- VERSION = '2.0.8'.freeze
2
+ VERSION = '2.1.0'.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: 2.0.8
4
+ version: 2.1.0
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: 2018-05-11 00:00:00.000000000 Z
11
+ date: 2019-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails