effective_logging 1.10.2 → 1.10.3

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: 4c25bb89ed7d4ea61f1a557b544bc2476d87346a
4
- data.tar.gz: c648b6749add1af7a26aa1ae8afc189b90b906b3
3
+ metadata.gz: 5d440bb1c8fc00063bd8be23b294627eddc3968a
4
+ data.tar.gz: 61aed25b0111ef56a941eedda9726ec40cb02bb4
5
5
  SHA512:
6
- metadata.gz: 9900a5a17e851dae1b6e41a61c73972c29b91108bfb1359a436baa828f00c07e30ddfd46112cb51b145f8e8f9b3852d1d94c83d23cc8e6c3944a54737aeae470
7
- data.tar.gz: 0da0a1ce53129cbe60680b5766e7dac74a2710e3ac6295247e0fcb10a1d0aeb1bfecbcb2dcb15333cd39ba22784f48313d7ceb5ce518d3dfe462f4c81de353cb
6
+ metadata.gz: cfedf4bfa32ecfcdb42e37a575e0cbc36edb0f794b974a8e41b213e4c73559833eb95e25112918615cb1bb296af3dacb05b136f035fe9294d9c68b630e1de687
7
+ data.tar.gz: 4525eb564843dc558935da3044cb82c3ed5c3f79258c9c2df28da2840a445ec5ba7df4c27c7b6c20537b4e87709fe0e02b399545c4947a6643ba8a9ada763d56
data/README.md CHANGED
@@ -246,9 +246,9 @@ Apply your own formatting to the logged before and after values of each attribut
246
246
  # Format the value of this attribute. Return nil to use the default to_s
247
247
  def log_changes_formatted_value(attribute, value)
248
248
  if ['cost'].include?(attribute)
249
- ActionController::Base.helpers.number_to_currency(value)
249
+ ApplicationController.helpers.number_to_currency(value)
250
250
  elsif ['percentage'].include?(attribute)
251
- ActionController::Base.helpers.number_to_percentage(value)
251
+ ApplicationController.helpers.number_to_percentage(value)
252
252
  end
253
253
  end
254
254
  ```
@@ -17,13 +17,13 @@ module Admin
17
17
 
18
18
  def show
19
19
  @log = Effective::Log.includes(:logs).find(params[:id])
20
- @log.next_log = Effective::Log.unscoped.order(:id).where(:parent_id => @log.parent_id).where('id > ?', @log.id).first
21
- @log.prev_log = Effective::Log.unscoped.order(:id).where(:parent_id => @log.parent_id).where('id < ?', @log.id).last
20
+ @log.next_log = Effective::Log.unscoped.order(:id).where(parent_id: @log.parent_id).where('id > ?', @log.id).first
21
+ @log.prev_log = Effective::Log.unscoped.order(:id).where(parent_id: @log.parent_id).where('id < ?', @log.id).last
22
22
 
23
23
  @page_title = "Log ##{@log.to_param}"
24
24
 
25
25
  if @log.logs.present?
26
- @log.datatable = Effective::Datatables::Logs.new(:log_id => @log.id) if defined?(EffectiveDatatables)
26
+ @log.datatable = Effective::Datatables::Logs.new(log_id: @log.id) if defined?(EffectiveDatatables)
27
27
  end
28
28
 
29
29
  EffectiveLogging.authorized?(self, :show, @log)
@@ -39,10 +39,10 @@ module Effective
39
39
 
40
40
  # This is the User index event
41
41
  def index
42
- @datatable = Effective::Datatables::Logs.new(:user_id => current_user.id)
42
+ @datatable = Effective::Datatables::Logs.new(user_id: current_user.id)
43
43
  @page_title = 'My Activity'
44
44
 
45
- EffectiveLogging.authorized?(self, :index, Effective::Log.new(:user_id => current_user.id))
45
+ EffectiveLogging.authorized?(self, :index, Effective::Log.new(user_id: current_user.id))
46
46
  end
47
47
 
48
48
  # This is the User show event
@@ -27,21 +27,23 @@ module EffectiveLoggingHelper
27
27
  # Any other options are sent to the table tag
28
28
  def tableize_hash(hash, options = {})
29
29
  if hash.present? && hash.kind_of?(Hash)
30
- content_tag(:table, options) do
31
- hash.map do |k, v|
32
- content_tag(:tr) do
33
- content_tag((options[:th] ? :th : :td), k) +
34
- content_tag(:td) do
35
- if v.kind_of?(Hash)
36
- tableize_hash(v, options.merge({:class => 'table table-bordered', :th => (options.key?(:sub_th) ? options[:sub_th] : options[:th])}))
37
- elsif v.kind_of?(Array)
38
- '[' + v.join(', ') + ']'
39
- else
40
- v.to_s
30
+ content_tag(:table, class: options[:class]) do
31
+ content_tag(:tbody) do
32
+ hash.map do |k, v|
33
+ content_tag(:tr) do
34
+ content_tag((options[:th] ? :th : :td), k) +
35
+ content_tag(:td) do
36
+ if v.kind_of?(Hash)
37
+ tableize_hash(v, options.merge({class: 'table table-hover', th: (options.key?(:sub_th) ? options[:sub_th] : options[:th])}))
38
+ elsif v.kind_of?(Array)
39
+ '[' + v.join(', ') + ']'
40
+ else
41
+ v.to_s
42
+ end
41
43
  end
42
44
  end
43
- end
44
- end.join('').html_safe
45
+ end.join('').html_safe
46
+ end
45
47
  end.html_safe
46
48
  else
47
49
  hash.to_s.html_safe
@@ -65,7 +67,7 @@ module EffectiveLoggingHelper
65
67
  value = log.details[key]
66
68
 
67
69
  if value.kind_of?(Hash)
68
- tableize_hash(value, :class => 'table', :th => true)
70
+ tableize_hash(value, class: 'table table-hover', th: true)
69
71
  elsif value.kind_of?(Array)
70
72
  value.map { |value| effective_logging_simple_format(value) }.join.html_safe
71
73
  else
@@ -18,12 +18,13 @@ module ActsAsLoggable
18
18
 
19
19
  around_save do |_, block|
20
20
  @acts_as_loggable_new_record = new_record?
21
- block.call
22
21
  EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).changed! unless @acts_as_loggable_new_record
22
+ block.call
23
23
  true
24
24
  end
25
25
 
26
26
  before_destroy do
27
+ @acts_as_loggable_destroy_record = true
27
28
  EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).destroyed!
28
29
  true
29
30
  end
@@ -31,9 +32,10 @@ module ActsAsLoggable
31
32
  after_commit do
32
33
  if @acts_as_loggable_new_record
33
34
  EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).created!
34
- else
35
+ elsif !@acts_as_loggable_destroy_record
35
36
  EffectiveLogging::ActiveRecordLogger.new(self, log_changes_options).updated!
36
37
  end
38
+
37
39
  true
38
40
  end
39
41
 
@@ -65,12 +67,7 @@ module ActsAsLoggable
65
67
  def log_changes_datatable
66
68
  if persisted?
67
69
  @log_changes_datatable ||= (
68
- Effective::Datatables::Logs.new(
69
- associated_id: id,
70
- associated_type: self.class.name,
71
- status: false,
72
- log_changes: true
73
- )
70
+ Effective::Datatables::Logs.new(associated_id: id, associated_type: self.class.name, log_changes: true, status: false)
74
71
  )
75
72
  end
76
73
  end
@@ -5,9 +5,9 @@ if defined?(EffectiveDatatables)
5
5
  include EffectiveLoggingHelper
6
6
 
7
7
  datatable do
8
- default_order :created_at, :desc
8
+ default_order :updated_at, :desc
9
9
 
10
- table_column :created_at
10
+ table_column :updated_at, label: 'Date'
11
11
  table_column :id, visible: false
12
12
 
13
13
  if attributes[:user] == false
@@ -24,9 +24,11 @@ if defined?(EffectiveDatatables)
24
24
  table_column :status, filter: { type: :select, values: EffectiveLogging.statuses }
25
25
  end
26
26
 
27
- table_column :associated_type, visible: false
28
- table_column :associated_id, visible: false, label: 'Associated Id'
29
- table_column :associated_to_s, label: 'Associated'
27
+ unless attributes[:log_changes]
28
+ table_column :associated_type, visible: false
29
+ table_column :associated_id, visible: false, label: 'Associated Id'
30
+ table_column :associated_to_s, label: 'Associated'
31
+ end
30
32
 
31
33
  table_column :message do |log|
32
34
  log.message.starts_with?("\t") ? log.message.gsub("\t", "&nbsp;&nbsp;") : log.message
@@ -38,8 +40,6 @@ if defined?(EffectiveDatatables)
38
40
  tableize_hash(log.details.except(:email), th: true, sub_th: false, width: '100%')
39
41
  end
40
42
 
41
- table_column :updated_at, visible: false
42
-
43
43
  unless attributes[:actions] == false
44
44
  actions_column partial: 'admin/logs/actions', partial_local: :log
45
45
  end
@@ -1,14 +1,16 @@
1
1
  module EffectiveLogging
2
2
  class ActiveRecordLogger
3
- attr_accessor :resource, :logger, :depth, :options
3
+ attr_accessor :object, :resource, :logger, :depth, :options
4
4
 
5
5
  BLANK = "''"
6
6
 
7
- def initialize(resource, options = {})
7
+ def initialize(object, options = {})
8
8
  raise ArgumentError.new('options must be a Hash') unless options.kind_of?(Hash)
9
9
 
10
- @resource = resource
11
- @logger = options.delete(:logger) || resource
10
+ @object = object
11
+ @resource = Effective::Resource.new(object)
12
+
13
+ @logger = options.delete(:logger) || object
12
14
  @depth = options.delete(:depth) || 0
13
15
  @options = options
14
16
 
@@ -17,9 +19,9 @@ module EffectiveLogging
17
19
 
18
20
  # execute! is called when we recurse, otherwise the following methods are best called individually
19
21
  def execute!
20
- if resource.new_record?
22
+ if object.new_record?
21
23
  created!
22
- elsif resource.marked_for_destruction?
24
+ elsif object.marked_for_destruction?
23
25
  destroyed!
24
26
  else
25
27
  changed!
@@ -28,84 +30,44 @@ module EffectiveLogging
28
30
 
29
31
  # before_destroy
30
32
  def destroyed!
31
- log('Deleted', details: applicable(attributes))
33
+ log('Deleted', details: applicable(resource.instance_attributes))
32
34
  end
33
35
 
34
36
  # after_commit
35
37
  def created!
36
- log('Created', details: applicable(attributes))
38
+ log('Created', details: applicable(resource.instance_attributes))
37
39
  end
38
40
 
39
41
  # after_commit
40
42
  def updated!
41
- log('Updated', details: applicable(attributes))
43
+ log('Updated', details: applicable(resource.instance_attributes))
42
44
  end
43
45
 
44
46
  # before_save
45
47
  def changed!
46
- applicable(changes).each do |attribute, (before, after)|
47
- if resource.respond_to?(:log_changes_formatted_value)
48
- before = resource.log_changes_formatted_value(attribute, before) || before
49
- after = resource.log_changes_formatted_value(attribute, after) || after
48
+ applicable(resource.instance_changes).each do |attribute, (before, after)|
49
+ if object.respond_to?(:log_changes_formatted_value)
50
+ before = object.log_changes_formatted_value(attribute, before) || before
51
+ after = object.log_changes_formatted_value(attribute, after) || after
50
52
  end
51
53
 
52
- attribute = if resource.respond_to?(:log_changes_formatted_attribute)
53
- resource.log_changes_formatted_attribute(attribute)
54
+ attribute = if object.respond_to?(:log_changes_formatted_attribute)
55
+ object.log_changes_formatted_attribute(attribute)
54
56
  end || attribute.titleize
55
57
 
56
- log("#{attribute}: #{before.presence || BLANK} to #{after.presence || BLANK}", details: { attribute: attribute, before: before, after: after })
58
+ log("#{attribute}: #{before.presence || BLANK} &rarr; #{after.presence || BLANK}", details: { attribute: attribute, before: before, after: after })
57
59
  end
58
60
 
59
61
  # Log changes on all accepts_as_nested_parameters has_many associations
60
- (resource.class.try(:reflect_on_all_autosave_associations) || []).each do |association|
61
- child_name = association.name.to_s.singularize.titleize
62
+ resource.nested_resources.each do |association|
63
+ title = association.name.to_s.singularize.titleize
62
64
 
63
- Array(resource.send(association.name)).each_with_index do |child, index|
64
- ActiveRecordLogger.new(child, options.merge(logger: logger, depth: (depth + 1), prefix: "#{child_name} ##{index+1}: ")).execute!
65
+ Array(object.send(association.name)).each_with_index do |child, index|
66
+ ActiveRecordLogger.new(child, options.merge(logger: logger, depth: (depth + 1), prefix: "#{title} ##{index+1}: ")).execute!
65
67
  end
66
68
  end
67
69
  end
68
70
 
69
- def attributes
70
- attributes = { attributes: resource.attributes }
71
-
72
- # Collect to_s representations of all belongs_to associations
73
- (resource.class.try(:reflect_on_all_associations, :belongs_to) || []).each do |association|
74
- attributes[association.name] = resource.send(association.name).to_s.presence
75
- end
76
-
77
- # Collect to_s representations for all has_one associations
78
- (resource.class.try(:reflect_on_all_associations, :has_one) || []).each do |association|
79
- attributes[association.name] = resource.send(association.name).to_s.presence
80
- end
81
-
82
- # Collects attributes for all accepts_as_nested_parameters has_many associations
83
- (resource.class.try(:reflect_on_all_autosave_associations) || []).each do |association|
84
- attributes[association.name] = {}
85
-
86
- Array(resource.send(association.name)).each_with_index do |child, index|
87
- attributes[association.name][index+1] = ActiveRecordLogger.new(child, options.merge(logger: logger)).attributes
88
- end
89
-
90
- attributes[association.name].presence
91
- end
92
-
93
- attributes
94
- end
95
-
96
- def changes
97
- changes = resource.changes
98
-
99
- # Log to_s changes on all belongs_to associations
100
- (resource.class.try(:reflect_on_all_associations, :belongs_to) || []).each do |association|
101
- if (change = changes.delete(association.foreign_key)).present?
102
- changes[association.name] = [(association.klass.find_by_id(change.first) if changes.first), resource.send(association.name)]
103
- end
104
- end
105
-
106
- changes
107
- end
108
-
109
71
  private
110
72
 
111
73
  def log(message, details: {})
@@ -113,8 +75,7 @@ module EffectiveLogging
113
75
  user: EffectiveLogging.current_user,
114
76
  status: EffectiveLogging.log_changes_status,
115
77
  message: "#{"\t" * depth}#{options[:prefix]}#{message}",
116
- associated: resource,
117
- associated_to_s: (resource.to_s rescue nil),
78
+ associated_to_s: (logger.to_s rescue nil),
118
79
  details: details
119
80
  ).tap { |log| log.save }
120
81
  end
@@ -130,7 +91,7 @@ module EffectiveLogging
130
91
  end
131
92
 
132
93
  (options[:additionally] || []).each do |attribute|
133
- value = (resource.send(attribute) rescue :effective_logging_nope)
94
+ value = (object.send(attribute) rescue :effective_logging_nope)
134
95
  next if attributes[attribute].present? || value == :effective_logging_nope
135
96
 
136
97
  atts[attribute] = value
@@ -1,3 +1,3 @@
1
1
  module EffectiveLogging
2
- VERSION = '1.10.2'.freeze
2
+ VERSION = '1.10.3'.freeze
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'haml-rails'
2
+ require 'effective_resources'
2
3
  require 'effective_logging/engine'
3
4
  require 'effective_logging/version'
4
5
 
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.10.2
4
+ version: 1.10.3
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: 2017-01-24 00:00:00.000000000 Z
11
+ date: 2017-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 2.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: effective_resources
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: coffee-rails
43
57
  requirement: !ruby/object:Gem::Requirement