Skip to content

Commit

Permalink
Exclude referenced IDs from fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jan 18, 2018
1 parent 71b9062 commit ca44523
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-01-17 21:47:30 -0500 using RuboCop version 0.48.1.
# on 2018-01-17 21:53:45 -0500 using RuboCop version 0.48.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -33,7 +33,7 @@ Lint/ParenthesesAsGroupedExpression:

# Offense count: 22
Metrics/AbcSize:
Max: 62
Max: 69

# Offense count: 114
# Configuration parameters: CountComments, ExcludedMethods.
Expand All @@ -43,13 +43,13 @@ Metrics/BlockLength:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 123
Max: 124

# Offense count: 6
Metrics/CyclomaticComplexity:
Max: 10

# Offense count: 461
# Offense count: 462
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Expand All @@ -58,7 +58,7 @@ Metrics/LineLength:
# Offense count: 16
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 25
Max: 26

# Offense count: 2
# Configuration parameters: CountComments.
Expand Down
22 changes: 17 additions & 5 deletions lib/mongoid/history/attributes/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ module Attributes
class Create < ::Mongoid::History::Attributes::Base
def attributes
@attributes = {}
insert_attributes
insert_embeds_one_changes
insert_embeds_many_changes
insert_has_or_belongs_to_many_changes
@attributes
end

private

def insert_attributes
trackable.attributes.each do |k, v|
next unless trackable_class.tracked_field?(k, :create)
modified = if changes[k]
Expand All @@ -13,13 +23,8 @@ def attributes
end
@attributes[k] = [nil, format_field(k, modified)]
end
insert_embeds_one_changes
insert_embeds_many_changes
@attributes
end

private

def insert_embeds_one_changes
trackable_class.tracked_embeds_one.each do |rel|
rel_class = trackable_class.relation_class_of(rel)
Expand All @@ -44,6 +49,13 @@ def insert_embeds_many_changes
.map { |obj| format_embeds_many_relation(rel, obj.attributes) }]
end
end

def insert_has_or_belongs_to_many_changes
trackable_class.referenced_relations.values.each do |rel|
k = rel.key
@attributes[k] = [nil, format_field(k, trackable.send(k))]
end
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/history/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def parse_tracked_fields_and_relations
@options[:on] = options[:on].reject { |opt| opt == :fields }
@options[:on] = options[:on] |
trackable.fields.keys.map(&:to_sym) -
reserved_fields.map(&:to_sym)
reserved_fields.map(&:to_sym) -
trackable.referenced_relations.values.map { |r| r.key.to_sym }
end

if options[:on].include?(:embedded_relations)
@options[:on] = options[:on].reject { |opt| opt == :embedded_relations }
p trackable.embedded_relations.keys
@options[:on] = options[:on] | trackable.embedded_relations.keys
end

Expand Down
8 changes: 4 additions & 4 deletions spec/unit/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
describe '#parse_tracked_fields_and_relations' do
context 'when options not passed' do
let(:expected_options) do
{ on: %i[foo b hatbm_ids],
{ on: %i[foo b],
except: %w[created_at updated_at],
tracker_class_name: nil,
modifier_field: :modifier,
Expand All @@ -147,7 +147,7 @@
track_create: true,
track_update: true,
track_destroy: true,
fields: %w[foo b hatbm_ids],
fields: %w[foo b],
dynamic: [],
relations: { embeds_one: {}, embeds_many: {}, has_and_belongs_to_many: {} },
format: {} }
Expand Down Expand Up @@ -185,12 +185,12 @@

context 'with :all' do
let(:value) { :all }
it { expect(subject[:on]).to eq %i[foo b hatbm_ids] }
it { expect(subject[:on]).to eq %i[foo b] }
end

context 'with :fields' do
let(:value) { :fields }
it { expect(subject[:on]).to eq %i[foo b hatbm_ids] }
it { expect(subject[:on]).to eq %i[foo b] }
end

describe '#categorize_tracked_option' do
Expand Down

0 comments on commit ca44523

Please sign in to comment.