Skip to content

Commit ca44523

Browse files
committed
Exclude referenced IDs from fields.
1 parent 71b9062 commit ca44523

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

.rubocop_todo.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2018-01-17 21:47:30 -0500 using RuboCop version 0.48.1.
3+
# on 2018-01-17 21:53:45 -0500 using RuboCop version 0.48.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -33,7 +33,7 @@ Lint/ParenthesesAsGroupedExpression:
3333

3434
# Offense count: 22
3535
Metrics/AbcSize:
36-
Max: 62
36+
Max: 69
3737

3838
# Offense count: 114
3939
# Configuration parameters: CountComments, ExcludedMethods.
@@ -43,13 +43,13 @@ Metrics/BlockLength:
4343
# Offense count: 1
4444
# Configuration parameters: CountComments.
4545
Metrics/ClassLength:
46-
Max: 123
46+
Max: 124
4747

4848
# Offense count: 6
4949
Metrics/CyclomaticComplexity:
5050
Max: 10
5151

52-
# Offense count: 461
52+
# Offense count: 462
5353
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
5454
# URISchemes: http, https
5555
Metrics/LineLength:
@@ -58,7 +58,7 @@ Metrics/LineLength:
5858
# Offense count: 16
5959
# Configuration parameters: CountComments.
6060
Metrics/MethodLength:
61-
Max: 25
61+
Max: 26
6262

6363
# Offense count: 2
6464
# Configuration parameters: CountComments.

lib/mongoid/history/attributes/create.rb

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ module Attributes
44
class Create < ::Mongoid::History::Attributes::Base
55
def attributes
66
@attributes = {}
7+
insert_attributes
8+
insert_embeds_one_changes
9+
insert_embeds_many_changes
10+
insert_has_or_belongs_to_many_changes
11+
@attributes
12+
end
13+
14+
private
15+
16+
def insert_attributes
717
trackable.attributes.each do |k, v|
818
next unless trackable_class.tracked_field?(k, :create)
919
modified = if changes[k]
@@ -13,13 +23,8 @@ def attributes
1323
end
1424
@attributes[k] = [nil, format_field(k, modified)]
1525
end
16-
insert_embeds_one_changes
17-
insert_embeds_many_changes
18-
@attributes
1926
end
2027

21-
private
22-
2328
def insert_embeds_one_changes
2429
trackable_class.tracked_embeds_one.each do |rel|
2530
rel_class = trackable_class.relation_class_of(rel)
@@ -44,6 +49,13 @@ def insert_embeds_many_changes
4449
.map { |obj| format_embeds_many_relation(rel, obj.attributes) }]
4550
end
4651
end
52+
53+
def insert_has_or_belongs_to_many_changes
54+
trackable_class.referenced_relations.values.each do |rel|
55+
k = rel.key
56+
@attributes[k] = [nil, format_field(k, trackable.send(k))]
57+
end
58+
end
4759
end
4860
end
4961
end

lib/mongoid/history/options.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ def parse_tracked_fields_and_relations
8484
@options[:on] = options[:on].reject { |opt| opt == :fields }
8585
@options[:on] = options[:on] |
8686
trackable.fields.keys.map(&:to_sym) -
87-
reserved_fields.map(&:to_sym)
87+
reserved_fields.map(&:to_sym) -
88+
trackable.referenced_relations.values.map { |r| r.key.to_sym }
8889
end
8990

9091
if options[:on].include?(:embedded_relations)
9192
@options[:on] = options[:on].reject { |opt| opt == :embedded_relations }
92-
p trackable.embedded_relations.keys
9393
@options[:on] = options[:on] | trackable.embedded_relations.keys
9494
end
9595

spec/unit/options_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
describe '#parse_tracked_fields_and_relations' do
138138
context 'when options not passed' do
139139
let(:expected_options) do
140-
{ on: %i[foo b hatbm_ids],
140+
{ on: %i[foo b],
141141
except: %w[created_at updated_at],
142142
tracker_class_name: nil,
143143
modifier_field: :modifier,
@@ -147,7 +147,7 @@
147147
track_create: true,
148148
track_update: true,
149149
track_destroy: true,
150-
fields: %w[foo b hatbm_ids],
150+
fields: %w[foo b],
151151
dynamic: [],
152152
relations: { embeds_one: {}, embeds_many: {}, has_and_belongs_to_many: {} },
153153
format: {} }
@@ -185,12 +185,12 @@
185185

186186
context 'with :all' do
187187
let(:value) { :all }
188-
it { expect(subject[:on]).to eq %i[foo b hatbm_ids] }
188+
it { expect(subject[:on]).to eq %i[foo b] }
189189
end
190190

191191
context 'with :fields' do
192192
let(:value) { :fields }
193-
it { expect(subject[:on]).to eq %i[foo b hatbm_ids] }
193+
it { expect(subject[:on]).to eq %i[foo b] }
194194
end
195195

196196
describe '#categorize_tracked_option' do

0 commit comments

Comments
 (0)