diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a697da1c..ff9267d6 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -33,7 +33,7 @@ Lint/ParenthesesAsGroupedExpression: # Offense count: 22 Metrics/AbcSize: - Max: 62 + Max: 69 # Offense count: 114 # Configuration parameters: CountComments, ExcludedMethods. @@ -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: @@ -58,7 +58,7 @@ Metrics/LineLength: # Offense count: 16 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 25 + Max: 26 # Offense count: 2 # Configuration parameters: CountComments. diff --git a/lib/mongoid/history/attributes/create.rb b/lib/mongoid/history/attributes/create.rb index 31c123ca..6b334e0c 100644 --- a/lib/mongoid/history/attributes/create.rb +++ b/lib/mongoid/history/attributes/create.rb @@ -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] @@ -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) @@ -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 diff --git a/lib/mongoid/history/options.rb b/lib/mongoid/history/options.rb index a446b3bf..86319bbb 100644 --- a/lib/mongoid/history/options.rb +++ b/lib/mongoid/history/options.rb @@ -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 diff --git a/spec/unit/options_spec.rb b/spec/unit/options_spec.rb index aeab0e5d..3057439f 100644 --- a/spec/unit/options_spec.rb +++ b/spec/unit/options_spec.rb @@ -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, @@ -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: {} } @@ -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