effective_logging 2.1.0 → 2.1.1

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: 7a608aab5a3d18fc5904353993d99f79d5661512
4
- data.tar.gz: f92d295365c081c2fc2656644d9826033dc7208d
3
+ metadata.gz: f3201ea4f4d500abedd1361c0d1299fea65671f3
4
+ data.tar.gz: 30cf8d48ed9887a9d52f8338a63f45e71842e015
5
5
  SHA512:
6
- metadata.gz: 41199b4ae698d4b6204979a76116bafacf9c9d2b25c741631938c174fc18ab090e22d987417dcfcecb3e27ee21ada1cdf40254aca0ebe3921accda85f79daf8b
7
- data.tar.gz: 2eb2d0f92b144e7517bd752d927d4f4513f0becf7fa7ba8b34bf0c583d1945002f24c8cee43c20ecde45c0ae06a18405039a4951d9d15b3ac49e2539671d2a22
6
+ metadata.gz: 1f959e4b05e4cd53ee7a2718eb088506adc0b15a4fa7e88b723f8e9e877e28806f5583ef7bdb3fb06a5a65eb555489c86f80fff0c7345da855f7e73991e20882
7
+ data.tar.gz: c5a64a3e94683f3f0d2ba554b6babe4f7666307e48fdfa8d29c5c7f6eb3469f9506f9aa28c78b9ca86f2ea42050367dd11b9f80dec15afadc12481f68dba32f2
@@ -1,6 +1,6 @@
1
1
  module EffectiveLogging
2
2
  class ActiveRecordLogger
3
- attr_accessor :object, :resource, :logger, :depth, :include_associated, :include_nested, :options
3
+ attr_accessor :object, :resource, :logger, :depth, :include_associated, :include_nested, :log_parents, :options
4
4
 
5
5
  BLANK = "''"
6
6
 
@@ -10,10 +10,11 @@ module EffectiveLogging
10
10
  @object = object
11
11
  @resource = Effective::Resource.new(object)
12
12
 
13
- @logger = options.delete(:logger) || object
14
- @depth = options.delete(:depth) || 0
13
+ @logger = options.fetch(:logger, object)
14
+ @depth = options.fetch(:depth, 0)
15
15
  @include_associated = options.fetch(:include_associated, true)
16
16
  @include_nested = options.fetch(:include_nested, true)
17
+ @log_parents = options.fetch(:log_parents, true)
17
18
  @options = options
18
19
 
19
20
  raise ArgumentError.new('logger must respond to logged_changes') unless @logger.respond_to?(:logged_changes)
@@ -34,18 +35,18 @@ module EffectiveLogging
34
35
  end
35
36
 
36
37
  def destroyed!
37
- log('Deleted', details: applicable(instance_attributes))
38
+ log('Deleted', applicable(instance_attributes))
38
39
  end
39
40
 
40
41
  def created!
41
- log('Created', details: applicable(instance_attributes))
42
+ log('Created', applicable(instance_attributes))
42
43
  end
43
44
 
44
45
  def updated!
45
46
  log_resource_changes!
46
47
  log_nested_resources!
47
48
 
48
- log('Updated', details: applicable(instance_attributes)) if logged? && depth == 0
49
+ (logged? && depth == 0) ? log('Updated', applicable(instance_attributes)) : true
49
50
  end
50
51
 
51
52
  def log_resource_changes!
@@ -62,7 +63,7 @@ module EffectiveLogging
62
63
  object.log_changes_formatted_attribute(attribute)
63
64
  end || attribute.titleize
64
65
 
65
- log("#{attribute}: #{before.presence || BLANK} → #{after.presence || BLANK}", details: { attribute: attribute, before: before, after: after })
66
+ log("#{attribute}: #{before.presence || BLANK} → #{after.presence || BLANK}", { attribute: attribute, before: before, after: after })
66
67
  end
67
68
  end
68
69
 
@@ -79,36 +80,67 @@ module EffectiveLogging
79
80
  child_options = options.merge(logger: logger, depth: depth+1, prefix: "#{title} #{index} - #{child} - ", include_associated: include_associated, include_nested: include_nested)
80
81
  child_options = child_options.merge(child.log_changes_options) if child.respond_to?(:log_changes_options)
81
82
 
82
- @logged = true if ::EffectiveLogging::ActiveRecordLogger.new(child, child_options).execute!
83
+ @logged = true if ActiveRecordLogger.new(child, child_options).execute!
83
84
  end
84
85
  end
85
86
  end
86
87
 
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
-
93
- def log(message, details: {})
94
- @logged = true
95
-
88
+ def log(message, details)
96
89
  log = logger.logged_changes.build(
97
90
  user: EffectiveLogging.current_user,
98
91
  status: EffectiveLogging.log_changes_status,
99
92
  message: "#{"\t" * depth}#{options[:prefix]}#{message}",
100
93
  associated_to_s: (logger.to_s rescue nil),
101
- details: details
94
+ details: (details.presence || {})
102
95
  )
103
96
 
104
- log.save
97
+ log_changes_to_parents(message, details) if depth == 0 && log_parents
98
+
99
+ @logged = log.save!
105
100
  log
106
101
  end
107
102
 
103
+ def log_changes_to_parents(message, details)
104
+ log_changes_parents.each do |parent|
105
+ title = object.class.name.to_s.singularize.titleize
106
+ parent_options = { logger: parent, prefix: "#{title} - #{object} - " }
107
+ ActiveRecordLogger.new(parent, parent_options).log(message, details)
108
+ end
109
+ end
110
+
111
+ private
112
+
108
113
  def logged?
109
114
  @logged == true
110
115
  end
111
116
 
117
+ def instance_attributes # effective_resources gem
118
+ resource.instance_attributes(include_associated: include_associated, include_nested: include_nested)
119
+ end
120
+
121
+ # A parent is a belongs_to that accepts_nested_attributes for this resource
122
+ def log_changes_parents
123
+ resource.belong_tos.map do |association|
124
+ parent_klass = (association.options[:polymorphic] ? object.public_send(association.name).class : association.klass)
125
+
126
+ # Skip if the parent doesn't log_changes
127
+ next unless parent_klass.respond_to?(:log_changes)
128
+
129
+ # Skip unless the parent accepts_nested_attributes
130
+ next unless Effective::Resource.new(parent_klass).nested_resources.find { |ass| ass.plural_name == resource.plural_name }
131
+
132
+ parent = object.public_send(association.name).presence
133
+
134
+ # Sanity check
135
+ next unless parent.respond_to?(:log_changes_options)
136
+
137
+ # Skip if the parent does not log its nested or associated items
138
+ next unless parent.log_changes_options[:include_nested] || parent.log_changes_options[:include_associated]
139
+
140
+ parent
141
+ end.compact
142
+ end
143
+
112
144
  # TODO: Make this work better with nested objects
113
145
  def applicable(attributes)
114
146
  atts = if options[:only].present?
@@ -1,3 +1,3 @@
1
1
  module EffectiveLogging
2
- VERSION = '2.1.0'.freeze
2
+ VERSION = '2.1.1'.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.1.0
4
+ version: 2.1.1
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: 2019-01-09 00:00:00.000000000 Z
11
+ date: 2019-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails