logga 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/logga/active_record.rb +32 -9
- data/lib/logga/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: 92f8cbeea38fa8e6c0df26be6722dba58b4d2672
|
4
|
+
data.tar.gz: e5ff164fe6b4023e64b3d9e3d55fbd95e8197167
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d19d2e4b6b8e87221957d9996f35f14366106a3d6a8dfe4ccb71a6b8f7b2b3802cb5ce6243216de342c0f659d44143d5f76028b6fc5e503f6c79271ee39ad78e
|
7
|
+
data.tar.gz: 0766e0cc21b355d1a8462298d81b394ed16add4f27d01155052bb4b873fc9bc748cc956a610e41254520478dd41e702259f061f01ebc43ca0d0b3c3448c0f9c7
|
data/lib/logga/active_record.rb
CHANGED
@@ -2,30 +2,42 @@ module Logga
|
|
2
2
|
module ActiveRecord
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
EXCLUDED_KEYS = [:created_at, :updated_at, :log, :sent_photos_chaser_email, :sent_after_sales_emails]
|
6
|
+
EXCLUDED_KEYS_SUFFIXES = [:_id, :_filenames]
|
7
|
+
|
8
|
+
included do
|
9
|
+
class_attribute :log_fields, instance_writer: false
|
10
|
+
self.log_fields = {}
|
11
|
+
end
|
12
|
+
|
5
13
|
class_methods do
|
6
|
-
def add_log_entries_for(*actions, to: :self)
|
14
|
+
def add_log_entries_for(*actions, to: :self, fields: {})
|
7
15
|
after_create :log_model_creation if actions.include?(:create)
|
8
16
|
after_update :log_model_changes if actions.include?(:update)
|
9
17
|
define_method(:log_receiver) { to == :self ? self : send(to) }
|
18
|
+
self.log_fields = fields
|
10
19
|
end
|
11
20
|
end
|
12
21
|
|
13
22
|
def log_model_creation
|
14
|
-
|
23
|
+
body_generator = ->(record) { default_creation_log_body(record) }
|
24
|
+
body = log_fields.fetch(:created_at, body_generator).call(self)
|
15
25
|
log_receiver.log_entries.create(author_data.merge(body: body))
|
16
26
|
end
|
17
27
|
|
18
28
|
def log_model_changes
|
19
|
-
field_changes = changes.
|
29
|
+
field_changes = changes.reject do |k, _|
|
30
|
+
EXCLUDED_KEYS.include?(k.to_sym) ||
|
31
|
+
EXCLUDED_KEYS_SUFFIXES.any? { |suffix| k.to_s.end_with?(suffix.to_s) }
|
32
|
+
end
|
20
33
|
log_field_changes(field_changes)
|
21
34
|
end
|
22
35
|
|
23
36
|
def log_field_changes(changes)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
body = "changed #{field} from #{old_value} to #{new_value}"
|
37
|
+
body_generator = ->(record, field, old_value, new_value) { default_change_log_body(record, field, old_value, new_value) }
|
38
|
+
body = changes.inject([]) do |body, (field, (old_value, new_value))|
|
39
|
+
body << log_fields.fetch(field.to_sym, body_generator).call(self, field, old_value, new_value)
|
40
|
+
end.join('\n')
|
29
41
|
log_receiver.log_entries.create(author_data.merge(body: body))
|
30
42
|
end
|
31
43
|
|
@@ -37,5 +49,16 @@ module Logga
|
|
37
49
|
author_name: data[:name]
|
38
50
|
}
|
39
51
|
end
|
52
|
+
|
53
|
+
def default_creation_log_body(record)
|
54
|
+
[
|
55
|
+
"#{record.class.name.demodulize} created",
|
56
|
+
("(#{record.state})" if record.try(:state))
|
57
|
+
].compact.join(' ')
|
58
|
+
end
|
59
|
+
|
60
|
+
def default_change_log_body(record, field, old_value, new_value)
|
61
|
+
"#{record.class.name.demodulize} #{field} set to #{new_value}"
|
62
|
+
end
|
40
63
|
end
|
41
|
-
end
|
64
|
+
end
|
data/lib/logga/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logga
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stuart Chinery, Rob Hesketh, Lorenzo Tello
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|