models_auditor 0.2.0 → 1.0.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: 8eefac2c75092c811395c67fbf5731b6bbf9cae0
4
- data.tar.gz: fc29fe418d8f1943acb71da82388b802e7e15cd7
3
+ metadata.gz: c5052949bdc7e490427a00314160a258c9723482
4
+ data.tar.gz: b8e74cd2f69767f64068ca54fbab58cf9d1f04d1
5
5
  SHA512:
6
- metadata.gz: a0fd677022be453b8d61bcde43aa491186c8e61f0accc2e4cbc4d17c50484be725d9366ca93354e03bf90ff23cd9ba18d171afc9ec2cf7ecb70eb1497d126e55
7
- data.tar.gz: 06457a6d6ac6c0a9ef8e23e5b85153ecf82f606d77ef8e58af0411feaeca7d892c6f0ae1b4eed049cf99d88f1be6d74f5ab5e03458dcc4b4c4c2a3ec9900123d
6
+ metadata.gz: 1dfacc7dba0be169d3c815af3ffae7cb7cf4a52239ad73653098b29831f6a6f7ba55a63eeec2e6e7e8c7dbfd82b6e3724b07a598a5ef98a8afe34a8e27468010
7
+ data.tar.gz: 8403c277ebd5d53911f70d37c2113df4b07fd364da7d14bfe6d8e72e4afab72f4dde78eb278480de1ceab63cfa95642004cd93fed32e3e0e16879eb6190491fc
data/README.md CHANGED
@@ -76,11 +76,11 @@
76
76
 
77
77
  3. Add to each association models
78
78
 
79
- `enable_audit ModelsAuditor::Audit::AUDIT_MODE_JSON, bridge: {Author.name => :author_id, Post.name => :post_id}`
79
+ `enable_audit ModelsAuditor::Audit::AUDIT_MODE_JSON, bridge: {author_id: Author.name, post_id: Post.name}`
80
80
 
81
81
  ```ruby
82
82
  class AuthorsPost < ActiveRecord::Base
83
- enable_audit ModelsAuditor::Audit::AUDIT_MODE_JSON, bridge: {Author.name => :author_id, Post.name => :post_id}
83
+ enable_audit ModelsAuditor::Audit::AUDIT_MODE_JSON, bridge: {author_id: Author.name, post_id: Post.name}
84
84
  end
85
85
  ```
86
86
 
@@ -76,14 +76,34 @@ module ModelsAuditor
76
76
  end
77
77
 
78
78
  def get_relations(record, records)
79
- rel_records = records.select { |r| !r.bridge.nil? && r.bridge.keys.include?(record.object_type) }
79
+ rel_records = records.select do |r|
80
+ !r.bridge.nil? && r.bridge.any? do |_, v|
81
+ v_type, v_id = v.to_a[0]
82
+ v_type.to_s == record.object_type && v_id.to_i == record.object_id.to_i
83
+ end
84
+ end
80
85
  rel_records.map do |i|
81
- target_class = i.bridge.except(record.object_type).keys.first
82
- next unless target_class
83
- i.attributes.slice('id', 'object_type', 'object_id').merge(target: {target_class => i.bridge[target_class].values.first})
86
+ target_info = except_target_class(i.bridge, record.object_type, record.object_id)
87
+ next if target_info.empty?
88
+ t_key, klass_with_id = target_info.to_a[0]
89
+ target_klass, target_id = klass_with_id.to_a[0]
90
+ i.attributes.slice('id', 'object_type', 'object_id').merge(target: {
91
+ class: target_klass,
92
+ foreign_key: t_key,
93
+ foreign_id: target_id,
94
+ })
84
95
  end.compact
85
96
  end
86
97
 
98
+ # @return [Array]
99
+ def except_target_class(bridge, target_class, target_id)
100
+ target_id = target_id.to_i
101
+ bridge.select do |_, v|
102
+ klass, id = v.to_a[0]
103
+ !(klass.to_s == target_class.to_s && id.to_i == target_id.to_i)
104
+ end
105
+ end
106
+
87
107
  # @param [ActiveRecord::Relation|ModelsAuditor::AuditRequest|Array] data
88
108
  def structure_requests_data(data)
89
109
  requests =
@@ -50,7 +50,7 @@ module ModelsAuditor
50
50
 
51
51
  bridge =
52
52
  if options[:bridge]
53
- options[:bridge].each_with_object({}) { |(model_name, key), o| o[model_name] = {key => __send__(key)} }
53
+ options[:bridge].each_with_object({}) { |(key, model_name), o| o[key] = {model_name => __send__(key)} }
54
54
  end
55
55
 
56
56
  Thread.new do
@@ -1,3 +1,3 @@
1
1
  module ModelsAuditor
2
- VERSION = '0.2.0'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: models_auditor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Gorbunov