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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13c49b4d4a45c966a1e329a5e4fbecbf1d0766d2
4
- data.tar.gz: 8b828e895133ea716983708adf4c7a763c04519b
3
+ metadata.gz: 92f8cbeea38fa8e6c0df26be6722dba58b4d2672
4
+ data.tar.gz: e5ff164fe6b4023e64b3d9e3d55fbd95e8197167
5
5
  SHA512:
6
- metadata.gz: 39e077a00926918f3a62787ceb86ece91676aec46d0bc89052a4e15f77fb02a3a19926fb7d2bd97c4b618b39b2fb784d72449b57f9b06b5703b0e98c1eb4ec71
7
- data.tar.gz: 8e991e17f2b07de6ed7da2e212c60a3c3c75c813d360d3c9063010847ce2957510b825300a65fc2fe57235e793688bbab246297b462c4c67c800828a3cc2e5ae
6
+ metadata.gz: d19d2e4b6b8e87221957d9996f35f14366106a3d6a8dfe4ccb71a6b8f7b2b3802cb5ce6243216de342c0f659d44143d5f76028b6fc5e503f6c79271ee39ad78e
7
+ data.tar.gz: 0766e0cc21b355d1a8462298d81b394ed16add4f27d01155052bb4b873fc9bc748cc956a610e41254520478dd41e702259f061f01ebc43ca0d0b3c3448c0f9c7
@@ -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
- body = "#{self.class} created"
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.except(:created_at, :updated_at, :log)
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
- changes.each { |field, values| log_field_change(field, *values) }
25
- end
26
-
27
- def log_field_change(field, old_value, new_value)
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
@@ -1,3 +1,3 @@
1
1
  module Logga
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.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-16 00:00:00.000000000 Z
11
+ date: 2017-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler