diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 71771953..4dba96fc 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,92 +1,101 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2015-09-17 17:43:47 -0400 using RuboCop version 0.34.1. +# on 2017-04-09 18:25:15 -0400 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 # versions of RuboCop, may require this file to be generated again. +# Offense count: 6 +# Configuration parameters: Include. +# Include: **/Gemfile, **/gems.rb +Bundler/DuplicatedGem: + Exclude: + - 'Gemfile' + # Offense count: 3 Lint/HandleExceptions: Exclude: - 'spec/unit/trackable_spec.rb' -Metrics/ClassLength: - Max: 103 +# Offense count: 2 +Lint/ParenthesesAsGroupedExpression: + Exclude: + - 'spec/integration/integration_spec.rb' -# Offense count: 12 +# Offense count: 21 Metrics/AbcSize: - Max: 60 + Max: 50 + +# Offense count: 100 +# Configuration parameters: CountComments, ExcludedMethods. +Metrics/BlockLength: + Max: 801 + +# Offense count: 1 +# Configuration parameters: CountComments. +Metrics/ClassLength: + Max: 124 # Offense count: 4 Metrics/CyclomaticComplexity: Max: 10 -# Offense count: 199 -# Configuration parameters: AllowURI, URISchemes. +# Offense count: 435 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https Metrics/LineLength: Max: 688 -# Offense count: 7 +# Offense count: 13 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 33 + Max: 23 # Offense count: 2 # Configuration parameters: CountComments. Metrics/ModuleLength: - Max: 223 + Max: 176 # Offense count: 4 Metrics/PerceivedComplexity: Max: 12 -# Offense count: 44 +# Offense count: 12 Style/Documentation: Exclude: + - 'spec/**/*' + - 'test/**/*' - 'lib/mongoid/history.rb' - - 'lib/mongoid/history/options.rb' - - 'lib/mongoid/history/trackable.rb' - - 'lib/mongoid/history/tracker.rb' - - 'lib/mongoid/history/version.rb' - 'lib/mongoid/history/attributes/base.rb' - 'lib/mongoid/history/attributes/create.rb' - 'lib/mongoid/history/attributes/destroy.rb' - 'lib/mongoid/history/attributes/update.rb' - - 'spec/integration/embedded_in_polymorphic_spec.rb' - - 'spec/integration/integration_spec.rb' - - 'spec/integration/multi_relation_spec.rb' - - 'spec/integration/nested_embedded_documents_spec.rb' - - 'spec/integration/nested_embedded_polymorphic_documents_spec.rb' - - 'spec/integration/subclasses_spec.rb' - - 'spec/support/mongoid_history.rb' - - 'spec/unit/options_spec.rb' - - 'spec/unit/singleton_methods_spec.rb' - - 'spec/unit/trackable_spec.rb' - - 'spec/unit/tracker_spec.rb' - - 'spec/unit/attributes/base_spec.rb' - - 'spec/unit/attributes/create_spec.rb' - - 'spec/unit/attributes/destroy_spec.rb' - - 'spec/unit/attributes/update_spec.rb' + - 'lib/mongoid/history/options.rb' + - 'lib/mongoid/history/trackable.rb' + - 'lib/mongoid/history/tracker.rb' -# Offense count: 6 +# Offense count: 3 +# Cop supports --auto-correct. Style/EachWithObject: Exclude: - 'lib/mongoid/history/trackable.rb' - 'lib/mongoid/history/tracker.rb' -# Offense count: 1 -# Configuration parameters: Exclude. +# Offense count: 2 +# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms. +# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS Style/FileName: Exclude: + - 'Dangerfile' - 'lib/mongoid-history.rb' -# Offense count: 3 -Style/MultilineBlockChain: +# Offense count: 1 +Style/IfInsideElse: Exclude: - 'lib/mongoid/history/trackable.rb' - - 'lib/mongoid/history/tracker.rb' -Style/ClassLength: +# Offense count: 1 +Style/MultilineBlockChain: Exclude: - - 'lib/mongoid/history/options.rb' + - 'lib/mongoid/history/tracker.rb' diff --git a/Gemfile b/Gemfile index b5213309..b2ede633 100644 --- a/Gemfile +++ b/Gemfile @@ -20,16 +20,16 @@ else end group :development, :test do - gem 'rake', '< 11.0' gem 'bundler' + gem 'rake', '< 11.0' end group :test do + gem 'coveralls' + gem 'gem-release' gem 'mongoid-danger', '~> 0.1.0', require: false + gem 'request_store' gem 'rspec', '~> 3.1' - gem 'rubocop', '0.34.1' + gem 'rubocop', '0.48.1' gem 'yard' - gem 'gem-release' - gem 'coveralls' - gem 'request_store' end diff --git a/Rakefile b/Rakefile index 18b04cb3..e4649bab 100644 --- a/Rakefile +++ b/Rakefile @@ -18,7 +18,7 @@ end require 'rubocop/rake_task' RuboCop::RakeTask.new(:rubocop) -task default: [:rubocop, :spec] +task default: %i[rubocop spec] require 'yard' YARD::Rake::YardocTask.new diff --git a/lib/mongoid/history.rb b/lib/mongoid/history.rb index 05fcf1f5..5b0e1e54 100644 --- a/lib/mongoid/history.rb +++ b/lib/mongoid/history.rb @@ -11,7 +11,7 @@ module Mongoid module History - GLOBAL_TRACK_HISTORY_FLAG = 'mongoid_history_trackable_enabled' + GLOBAL_TRACK_HISTORY_FLAG = 'mongoid_history_trackable_enabled'.freeze class << self attr_accessor :tracker_class_name diff --git a/lib/mongoid/history/attributes/create.rb b/lib/mongoid/history/attributes/create.rb index 65a40778..d0992875 100644 --- a/lib/mongoid/history/attributes/create.rb +++ b/lib/mongoid/history/attributes/create.rb @@ -40,8 +40,8 @@ def insert_embeds_many_changes rel = aliased_fields.key(rel) || rel @attributes[rel] = [nil, trackable.send(rel) - .reject { |obj| obj.respond_to?(paranoia_field) && obj.public_send(paranoia_field).present? } - .map { |obj| format_embeds_many_relation(rel, obj.attributes) }] + .reject { |obj| obj.respond_to?(paranoia_field) && obj.public_send(paranoia_field).present? } + .map { |obj| format_embeds_many_relation(rel, obj.attributes) }] end end end diff --git a/lib/mongoid/history/attributes/destroy.rb b/lib/mongoid/history/attributes/destroy.rb index 45b44faf..07944609 100644 --- a/lib/mongoid/history/attributes/destroy.rb +++ b/lib/mongoid/history/attributes/destroy.rb @@ -14,19 +14,19 @@ def attributes def insert_embeds_one_changes trackable_class.tracked_embeds_one - .map { |rel| aliased_fields.key(rel) || rel } - .each do |rel| - obj = trackable.send(rel) - @attributes[rel] = [format_embeds_one_relation(rel, obj.attributes), nil] if obj - end + .map { |rel| aliased_fields.key(rel) || rel } + .each do |rel| + obj = trackable.send(rel) + @attributes[rel] = [format_embeds_one_relation(rel, obj.attributes), nil] if obj + end end def insert_embeds_many_changes trackable_class.tracked_embeds_many - .map { |rel| aliased_fields.key(rel) || rel } - .each do |rel| - @attributes[rel] = [trackable.send(rel).map { |obj| format_embeds_many_relation(rel, obj.attributes) }, nil] - end + .map { |rel| aliased_fields.key(rel) || rel } + .each do |rel| + @attributes[rel] = [trackable.send(rel).map { |obj| format_embeds_many_relation(rel, obj.attributes) }, nil] + end end end end diff --git a/lib/mongoid/history/attributes/update.rb b/lib/mongoid/history/attributes/update.rb index 2456351b..b1499285 100644 --- a/lib/mongoid/history/attributes/update.rb +++ b/lib/mongoid/history/attributes/update.rb @@ -33,9 +33,9 @@ def insert_embeds_many_changes(relation, value) paranoia_field = Mongoid::History.trackable_class_settings(relation_class)[:paranoia_field] @attributes[relation] = [] @attributes[relation][0] = value[0].reject { |rel| rel[paranoia_field].present? } - .map { |v_attrs| format_embeds_many_relation(relation, v_attrs) } + .map { |v_attrs| format_embeds_many_relation(relation, v_attrs) } @attributes[relation][1] = value[1].reject { |rel| rel[paranoia_field].present? } - .map { |v_attrs| format_embeds_many_relation(relation, v_attrs) } + .map { |v_attrs| format_embeds_many_relation(relation, v_attrs) } end end end diff --git a/lib/mongoid/history/options.rb b/lib/mongoid/history/options.rb index 730c903e..abad8f37 100644 --- a/lib/mongoid/history/options.rb +++ b/lib/mongoid/history/options.rb @@ -24,7 +24,7 @@ def parse(options = {}) def default_options @default_options ||= { on: :all, - except: [:created_at, :updated_at], + except: %i[created_at updated_at], tracker_class_name: nil, modifier_field: :modifier, version_field: :version, @@ -83,7 +83,7 @@ def parse_tracked_fields_and_relations Going forward, :all will track all the fields and relations for the class" end - @options[:on] = options[:on].map { |opt| (opt == :all) ? :fields : opt } + @options[:on] = options[:on].map { |opt| opt == :all ? :fields : opt } if options[:on].include?(:fields) @options[:on] = options[:on].reject { |opt| opt == :fields } @@ -163,7 +163,7 @@ def track_embeds_one(field, field_options) @options[:relations][:embeds_one][field] = if field_options.blank? relation_class.fields.keys else - %w(_id) | field_options.map { |opt| relation_class.database_field_name(opt) } + %w[_id] | field_options.map { |opt| relation_class.database_field_name(opt) } end end @@ -172,7 +172,7 @@ def track_embeds_many(field, field_options) @options[:relations][:embeds_many][field] = if field_options.blank? relation_class.fields.keys else - %w(_id) | field_options.map { |opt| relation_class.database_field_name(opt) } + %w[_id] | field_options.map { |opt| relation_class.database_field_name(opt) } end end diff --git a/lib/mongoid/history/trackable.rb b/lib/mongoid/history/trackable.rb index f706b021..ce2b296b 100644 --- a/lib/mongoid/history/trackable.rb +++ b/lib/mongoid/history/trackable.rb @@ -131,7 +131,7 @@ def get_versions_criteria(options_or_version) elsif options[:last] versions = history_tracks.limit(options[:last]) else - fail 'Invalid options, please specify (:from / :to) keys or :last key.' + raise 'Invalid options, please specify (:from / :to) keys or :last key.' end else options_or_version = options_or_version.to_a if options_or_version.is_a?(Range) @@ -178,7 +178,7 @@ def association_hash(node = self) relation.class_name == node.metadata.class_name.to_s && relation.name == node.metadata.name else relation.class_name == node.relation_metadata.class_name.to_s && - relation.name == node.relation_metadata.name + relation.name == node.relation_metadata.name end end end @@ -445,8 +445,8 @@ def tracked_embeds_one?(relation) def tracked_embeds_one @tracked_embeds_one ||= begin reflect_on_all_associations(:embeds_one) - .map(&:key) - .select { |rel| history_trackable_options[:relations][:embeds_one].include? rel } + .map(&:key) + .select { |rel| history_trackable_options[:relations][:embeds_one].include? rel } end end @@ -469,8 +469,8 @@ def tracked_embeds_many?(relation) def tracked_embeds_many @tracked_embeds_many ||= begin reflect_on_all_associations(:embeds_many) - .map(&:key) - .select { |rel| history_trackable_options[:relations][:embeds_many].include? rel } + .map(&:key) + .select { |rel| history_trackable_options[:relations][:embeds_many].include? rel } end end diff --git a/lib/mongoid/history/tracker.rb b/lib/mongoid/history/tracker.rb index f90fabbf..d7368e00 100644 --- a/lib/mongoid/history/tracker.rb +++ b/lib/mongoid/history/tracker.rb @@ -180,7 +180,7 @@ def create_on_parent elsif trackable_parent.class.embeds_many?(name) trackable_parent.get_embedded(name).create!(localize_keys(original)) else - fail 'This should never happen. Please report bug!' + raise 'This should never happen. Please report bug!' end end @@ -205,7 +205,7 @@ def traverse_association_chain elsif doc.class.embeds_many?(name) doc.get_embedded(name).unscoped.where(_id: node['id']).first else - fail 'This should never happen. Please report bug.' + raise 'This should never happen. Please report bug.' end documents << doc break if chain.empty? @@ -215,9 +215,11 @@ def traverse_association_chain def localize_keys(hash) klass = association_chain.first['name'].constantize - klass.localized_fields.keys.each do |name| - hash["#{name}_translations"] = hash.delete(name) if hash[name].present? - end if klass.respond_to?(:localized_fields) + if klass.respond_to?(:localized_fields) + klass.localized_fields.keys.each do |name| + hash["#{name}_translations"] = hash.delete(name) if hash[name].present? + end + end hash end diff --git a/lib/mongoid/history/version.rb b/lib/mongoid/history/version.rb index bade7bb2..8c7237d7 100644 --- a/lib/mongoid/history/version.rb +++ b/lib/mongoid/history/version.rb @@ -1,5 +1,5 @@ module Mongoid module History - VERSION = '0.6.2' + VERSION = '0.6.2'.freeze end end diff --git a/spec/integration/embedded_in_polymorphic_spec.rb b/spec/integration/embedded_in_polymorphic_spec.rb index 155ca686..93bbafa6 100644 --- a/spec/integration/embedded_in_polymorphic_spec.rb +++ b/spec/integration/embedded_in_polymorphic_spec.rb @@ -68,7 +68,7 @@ class Contact track_create: true, # track document creation, default is false track_update: true, # track document updates, default is true track_destroy: false, # track document destruction, default is false - scope: [:real_state, :company] + scope: %i[real_state company] end class User diff --git a/spec/integration/integration_spec.rb b/spec/integration/integration_spec.rb index 9f22b3cc..c8be28b6 100644 --- a/spec/integration/integration_spec.rb +++ b/spec/integration/integration_spec.rb @@ -18,7 +18,7 @@ class Post accepts_nested_attributes_for :tags, allow_destroy: true - track_history on: [:title, :body], track_destroy: true + track_history on: %i[title body], track_destroy: true end class Comment @@ -29,7 +29,7 @@ class Comment field :t, as: :title field :body embedded_in :commentable, polymorphic: true - track_history on: [:title, :body], scope: :post, track_create: true, track_destroy: true + track_history on: %i[title body], scope: :post, track_create: true, track_destroy: true end class Section @@ -54,7 +54,7 @@ class User field :city field :country field :aliases, type: Array - track_history except: [:email, :updated_at] + track_history except: %i[email updated_at] end class Tag @@ -229,14 +229,14 @@ class Foo < Comment it 'should track array changes' do aliases = user.aliases - user.update_attributes(aliases: %w(bob joe)) + user.update_attributes(aliases: %w[bob joe]) expect(user.history_tracks.first.original['aliases']).to eq(aliases) expect(user.history_tracks.first.modified['aliases']).to eq(user.aliases) end it 'should undo array changes' do aliases = user.aliases - user.update_attributes(aliases: %w(bob joe)) + user.update_attributes(aliases: %w[bob joe]) user.history_tracks.first.undo! nil expect(user.reload.aliases).to eq(aliases) end @@ -323,12 +323,12 @@ class Foo < Comment expect(subject[:array]).to eq({ aliases: { remove: ['bob'], add: ['', 'bill', 'james'] } }.with_indifferent_access) end it 'should not track unmodified field' do - %w(add modify remove array).each do |edit| + %w[add modify remove array].each do |edit| expect(subject[edit][:address]).to be_nil end end it 'should not track untracked fields' do - %w(add modify remove array).each do |edit| + %w[add modify remove array].each do |edit| expect(subject[edit][:email]).to be_nil end end @@ -639,7 +639,7 @@ class Foo < Comment describe 'undo' do { 'undo' => [nil], 'undo!' => [nil, :reload] }.each do |test_method, methods| methods.each do |method| - context "#{method || 'instance'}" do + context (method || 'instance').to_s do it 'recognizes :from, :to options' do comment.send test_method, user, from: 4, to: 2 comment.send(method) if method @@ -694,7 +694,7 @@ class Foo < Comment describe 'redo' do [nil, :reload].each do |method| - context "#{method || 'instance'}" do + context (method || 'instance').to_s do before :each do comment.update_attributes(title: 'Test5') end @@ -919,7 +919,7 @@ class OverriddenChangesMethod track_history on: [:foo], changes_method: :my_changes def my_changes - { foo: %w(bar baz) } + { foo: %w[bar baz] } end end end diff --git a/spec/integration/multi_relation_spec.rb b/spec/integration/multi_relation_spec.rb index 1a90bdd3..04ec371d 100644 --- a/spec/integration/multi_relation_spec.rb +++ b/spec/integration/multi_relation_spec.rb @@ -10,7 +10,7 @@ class Model belongs_to :user, inverse_of: :models has_and_belongs_to_many :external_users, class_name: 'User', inverse_of: :external_models - track_history on: [:name, :user, :external_user_ids], # track title and body fields only, default is :all + track_history on: %i[name user external_user_ids], # track title and body fields only, default is :all modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier modifier_field_inverse_of: nil, # no inverse modifier relationship version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version diff --git a/spec/integration/nested_embedded_polymorphic_documents_spec.rb b/spec/integration/nested_embedded_polymorphic_documents_spec.rb index 2527f8e5..d155bdc2 100644 --- a/spec/integration/nested_embedded_polymorphic_documents_spec.rb +++ b/spec/integration/nested_embedded_polymorphic_documents_spec.rb @@ -48,7 +48,7 @@ class OneEmbedded track_create: true, track_update: true, track_destroy: true, - scope: [:modelone, :modeltwo] + scope: %i[modelone modeltwo] end class EmbeddedTwo @@ -64,7 +64,7 @@ class EmbeddedTwo track_create: true, track_update: true, track_destroy: true, - scope: [:modelone, :modeltwo] + scope: %i[modelone modeltwo] end class User diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8812466d..11e4486a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,7 +19,7 @@ config.raise_errors_for_deprecations! config.before :all do Mongoid.logger.level = Logger::INFO - Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5? + Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5? || Mongoid::Compatibility::Version.mongoid6? Mongoid.belongs_to_required_by_default = false if Mongoid::Compatibility::Version.mongoid6? end end diff --git a/spec/unit/attributes/base_spec.rb b/spec/unit/attributes/base_spec.rb index 929b904c..63084e4c 100644 --- a/spec/unit/attributes/base_spec.rb +++ b/spec/unit/attributes/base_spec.rb @@ -108,7 +108,7 @@ class ModelTwo context 'with permitted attributes' do before do - model_one.track_history on: { model_two: %i(foo) } + model_one.track_history on: { model_two: %i[foo] } end it 'should select only permitted attributes' do @@ -119,7 +119,7 @@ class ModelTwo context 'with attributes formatted via string' do before do - model_one.track_history on: { model_two: %i(foo) }, format: { model_two: { foo: '&%s&' } } + model_one.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: '&%s&' } } end it 'should select obfuscate permitted attributes' do @@ -130,7 +130,7 @@ class ModelTwo context 'with attributes formatted via proc' do before do - model_one.track_history on: { model_two: %i(foo) }, format: { model_two: { foo: ->(v) { v.to_s * 2 } } } + model_one.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: ->(v) { v.to_s * 2 } } } end it 'should select obfuscate permitted attributes' do diff --git a/spec/unit/my_instance_methods_spec.rb b/spec/unit/my_instance_methods_spec.rb index f4f11af8..b7480c13 100644 --- a/spec/unit/my_instance_methods_spec.rb +++ b/spec/unit/my_instance_methods_spec.rb @@ -45,7 +45,7 @@ embedded_in :model_one end - ModelOne.track_history(on: %i(foo emb_one emb_threes)) + ModelOne.track_history(on: %i[foo emb_one emb_threes]) @persisted_history_options = Mongoid::History.trackable_class_options end before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options } diff --git a/spec/unit/options_spec.rb b/spec/unit/options_spec.rb index cf0a58a5..9df0241c 100644 --- a/spec/unit/options_spec.rb +++ b/spec/unit/options_spec.rb @@ -61,7 +61,7 @@ describe '#default_options' do let(:expected_options) do { on: :all, - except: [:created_at, :updated_at], + except: %i[created_at updated_at], tracker_class_name: nil, modifier_field: :modifier, version_field: :version, @@ -81,27 +81,27 @@ context 'with field' do let(:value) { :foo } - it { expect(subject[:except]).to eq %w(foo) } + it { expect(subject[:except]).to eq %w[foo] } end context 'with array of fields' do - let(:value) { %i(foo) } - it { expect(subject[:except]).to eq %w(foo) } + let(:value) { %i[foo] } + it { expect(subject[:except]).to eq %w[foo] } end context 'with field alias' do - let(:value) { %i(foo bar) } - it { expect(subject[:except]).to eq %w(foo b) } + let(:value) { %i[foo bar] } + it { expect(subject[:except]).to eq %w[foo b] } end context 'with duplicate values' do - let(:value) { %i(foo bar b) } - it { expect(subject[:except]).to eq %w(foo b) } + let(:value) { %i[foo bar b] } + it { expect(subject[:except]).to eq %w[foo b] } end context 'with blank values' do - let(:value) { %i(foo) | [nil] } - it { expect(subject[:except]).to eq %w(foo) } + let(:value) { %i[foo] | [nil] } + it { expect(subject[:except]).to eq %w[foo] } end end @@ -128,8 +128,8 @@ describe '#parse_tracked_fields_and_relations' do context 'when options not passed' do let(:expected_options) do - { on: %i(foo b), - except: %w(created_at updated_at), + { on: %i[foo b], + except: %w[created_at updated_at], tracker_class_name: nil, modifier_field: :modifier, version_field: :version, @@ -138,7 +138,7 @@ track_create: false, track_update: true, track_destroy: false, - fields: %w(foo b), + fields: %w[foo b], dynamic: [], relations: { embeds_one: {}, embeds_many: {} }, format: {} } @@ -154,114 +154,114 @@ context 'with field' do let(:value) { :foo } - it { expect(subject[:on]).to eq %i(foo) } - it { expect(subject[:fields]).to eq %w(foo) } + it { expect(subject[:on]).to eq %i[foo] } + it { expect(subject[:fields]).to eq %w[foo] } end context 'with array of fields' do - let(:value) { %i(foo) } - it { expect(subject[:on]).to eq %i(foo) } - it { expect(subject[:fields]).to eq %w(foo) } + let(:value) { %i[foo] } + it { expect(subject[:on]).to eq %i[foo] } + it { expect(subject[:fields]).to eq %w[foo] } end context 'with embeds_one relation attributes' do - let(:value) { { emb_one: %i(f_em_foo) } } - it { expect(subject[:on]).to eq [[:emb_one, %i(f_em_foo)]] } + let(:value) { { emb_one: %i[f_em_foo] } } + it { expect(subject[:on]).to eq [[:emb_one, %i[f_em_foo]]] } end context 'with fields and embeds_one relation attributes' do - let(:value) { [:foo, emb_one: %i(f_em_foo)] } - it { expect(subject[:on]).to eq [:foo, emb_one: %i(f_em_foo)] } + let(:value) { [:foo, emb_one: %i[f_em_foo]] } + it { expect(subject[:on]).to eq [:foo, emb_one: %i[f_em_foo]] } end context 'with :all' do let(:value) { :all } - it { expect(subject[:on]).to eq %i(foo b) } + 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) } + it { expect(subject[:on]).to eq %i[foo b] } end describe '#categorize_tracked_option' do context 'with skipped field' do - let(:options) { { on: %i(foo bar), except: :foo } } - it { expect(subject[:fields]).to eq %w(b) } + let(:options) { { on: %i[foo bar], except: :foo } } + it { expect(subject[:fields]).to eq %w[b] } end context 'with skipped embeds_one relation' do - let(:options) { { on: %i(fields emb_one emb_two), except: :emb_one } } - it { expect(subject[:relations][:embeds_one]).to eq('emtw' => %w(_id f_em_baz)) } + let(:options) { { on: %i[fields emb_one emb_two], except: :emb_one } } + it { expect(subject[:relations][:embeds_one]).to eq('emtw' => %w[_id f_em_baz]) } end context 'with skipped embeds_many relation' do - let(:options) { { on: %i(fields emb_threes emb_fours), except: :emb_threes } } - it { expect(subject[:relations][:embeds_many]).to eq('emfs' => %w(_id f_em_baz)) } + let(:options) { { on: %i[fields emb_threes emb_fours], except: :emb_threes } } + it { expect(subject[:relations][:embeds_many]).to eq('emfs' => %w[_id f_em_baz]) } end context 'with reserved field' do - let(:options) { { on: %i(_id _type foo deleted_at) } } - it { expect(subject[:fields]).to eq %w(foo) } + let(:options) { { on: %i[_id _type foo deleted_at] } } + it { expect(subject[:fields]).to eq %w[foo] } end context 'when embeds_one attribute passed' do let(:options) { { on: { emb_one: :f_em_foo } } } - it { expect(subject[:relations][:embeds_one]).to eq('emb_one' => %w(_id f_em_foo)) } + it { expect(subject[:relations][:embeds_one]).to eq('emb_one' => %w[_id f_em_foo]) } end context 'when embeds_one attributes array passed' do - let(:options) { { on: { emb_one: %i(f_em_foo) } } } - it { expect(subject[:relations][:embeds_one]).to eq('emb_one' => %w(_id f_em_foo)) } + let(:options) { { on: { emb_one: %i[f_em_foo] } } } + it { expect(subject[:relations][:embeds_one]).to eq('emb_one' => %w[_id f_em_foo]) } end context 'when embeds_many attribute passed' do let(:options) { { on: { emb_threes: :f_em_foo } } } - it { expect(subject[:relations][:embeds_many]).to eq('emb_threes' => %w(_id f_em_foo)) } + it { expect(subject[:relations][:embeds_many]).to eq('emb_threes' => %w[_id f_em_foo]) } end context 'when embeds_many attributes array passed' do - let(:options) { { on: { emb_threes: %i(f_em_foo) } } } - it { expect(subject[:relations][:embeds_many]).to eq('emb_threes' => %w(_id f_em_foo)) } + let(:options) { { on: { emb_threes: %i[f_em_foo] } } } + it { expect(subject[:relations][:embeds_many]).to eq('emb_threes' => %w[_id f_em_foo]) } end context 'when embeds_one attributes not passed' do let(:options) { { on: :emb_one } } - it { expect(subject[:relations][:embeds_one]).to eq('emb_one' => %w(_id f_em_foo fmb)) } + it { expect(subject[:relations][:embeds_one]).to eq('emb_one' => %w[_id f_em_foo fmb]) } end context 'when embeds_many attributes not passed' do let(:options) { { on: :emb_threes } } - it { expect(subject[:relations][:embeds_many]).to eq('emb_threes' => %w(_id f_em_foo fmb)) } + it { expect(subject[:relations][:embeds_many]).to eq('emb_threes' => %w[_id f_em_foo fmb]) } end context 'when embeds_one attribute alias passed' do - let(:options) { { on: { emb_one: %i(f_em_bar) } } } - it { expect(subject[:relations][:embeds_one]).to eq('emb_one' => %w(_id fmb)) } + let(:options) { { on: { emb_one: %i[f_em_bar] } } } + it { expect(subject[:relations][:embeds_one]).to eq('emb_one' => %w[_id fmb]) } end context 'when embeds_many attribute alias passed' do - let(:options) { { on: { emb_threes: %i(f_em_bar) } } } - it { expect(subject[:relations][:embeds_many]).to eq('emb_threes' => %w(_id fmb)) } + let(:options) { { on: { emb_threes: %i[f_em_bar] } } } + it { expect(subject[:relations][:embeds_many]).to eq('emb_threes' => %w[_id fmb]) } end context 'with field alias' do let(:options) { { on: :bar } } - it { expect(subject[:fields]).to eq %w(b) } + it { expect(subject[:fields]).to eq %w[b] } end context 'with dynamic field name' do let(:options) { { on: :my_field } } - it { expect(subject[:dynamic]).to eq %w(my_field) } + it { expect(subject[:dynamic]).to eq %w[my_field] } end context 'with relations' do let(:options) { { on: :embedded_relations } } it do - expect(subject[:relations]).to eq(embeds_many: { 'emb_threes' => %w(_id f_em_foo fmb), - 'emfs' => %w(_id f_em_baz) }, - embeds_one: { 'emb_one' => %w(_id f_em_foo fmb), - 'emtw' => %w(_id f_em_baz) }) + expect(subject[:relations]).to eq(embeds_many: { 'emb_threes' => %w[_id f_em_foo fmb], + 'emfs' => %w[_id f_em_baz] }, + embeds_one: { 'emb_one' => %w[_id f_em_foo fmb], + 'emtw' => %w[_id f_em_baz] }) end end end @@ -308,8 +308,8 @@ end describe '#remove_reserved_fields' do - let(:options) { { on: [:_id, :_type, :foo, :version, :modifier_id] } } - it { expect(subject[:fields]).to eq %w(foo) } + let(:options) { { on: %i[_id _type foo version modifier_id] } } + it { expect(subject[:fields]).to eq %w[foo] } it { expect(subject[:dynamic]).to eq [] } end end diff --git a/spec/unit/singleton_methods_spec.rb b/spec/unit/singleton_methods_spec.rb index 0189f302..1c7b6b2b 100644 --- a/spec/unit/singleton_methods_spec.rb +++ b/spec/unit/singleton_methods_spec.rb @@ -31,7 +31,7 @@ embedded_in :my_trackable_model end - MyTrackableModel.track_history(on: [:foo, :my_embed_one_model, :my_embed_many_models, :my_dynamic_field]) + MyTrackableModel.track_history(on: %i[foo my_embed_one_model my_embed_many_models my_dynamic_field]) end describe '#tracked?' do @@ -143,7 +143,7 @@ class EmbOne describe '#tracked_fields' do it 'should include fields and dynamic fields' do - expect(MyTrackableModel.tracked_fields).to eq %w(foo my_dynamic_field) + expect(MyTrackableModel.tracked_fields).to eq %w[foo my_dynamic_field] end end @@ -199,17 +199,17 @@ class EmbOne context 'when relation tracked' do before(:each) { ModelOne.track_history(on: :emb_one) } - it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to eq %w(_id em_foo em_bar) } + it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to eq %w[_id em_foo em_bar] } end context 'when relation tracked with alias' do before(:each) { ModelOne.track_history(on: :emb_two) } - it { expect(ModelOne.tracked_embeds_one_attributes('emb_two')).to eq %w(_id em_bar) } + it { expect(ModelOne.tracked_embeds_one_attributes('emb_two')).to eq %w[_id em_bar] } end context 'when relation tracked with attributes' do before(:each) { ModelOne.track_history(on: { emb_one: :em_foo }) } - it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to eq %w(_id em_foo) } + it { expect(ModelOne.tracked_embeds_one_attributes('emb_one')).to eq %w[_id em_foo] } end context 'when relation not tracked' do @@ -268,17 +268,17 @@ class EmbOne context 'when relation tracked' do before(:each) { ModelOne.track_history(on: :emb_ones) } - it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to eq %w(_id em_foo em_bar) } + it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to eq %w[_id em_foo em_bar] } end context 'when relation tracked with alias' do before(:each) { ModelOne.track_history(on: :emb_twos) } - it { expect(ModelOne.tracked_embeds_many_attributes('emb_twos')).to eq %w(_id em_bar) } + it { expect(ModelOne.tracked_embeds_many_attributes('emb_twos')).to eq %w[_id em_bar] } end context 'when relation tracked with attributes' do before(:each) { ModelOne.track_history(on: { emb_ones: :em_foo }) } - it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to eq %w(_id em_foo) } + it { expect(ModelOne.tracked_embeds_many_attributes('emb_ones')).to eq %w[_id em_foo] } end context 'when relation not tracked' do @@ -309,12 +309,12 @@ class EmbOne describe '#clear_trackable_memoization' do before do - MyTrackableModel.instance_variable_set(:@reserved_tracked_fields, %w(_id _type)) - MyTrackableModel.instance_variable_set(:@history_trackable_options, on: %w(fields)) + MyTrackableModel.instance_variable_set(:@reserved_tracked_fields, %w[_id _type]) + MyTrackableModel.instance_variable_set(:@history_trackable_options, on: %w[fields]) MyTrackableModel.instance_variable_set(:@trackable_settings, paranoia_field: 'deleted_at') - MyTrackableModel.instance_variable_set(:@tracked_fields, %w(foo)) - MyTrackableModel.instance_variable_set(:@tracked_embeds_one, %w(my_embed_one_model)) - MyTrackableModel.instance_variable_set(:@tracked_embeds_many, %w(my_embed_many_models)) + MyTrackableModel.instance_variable_set(:@tracked_fields, %w[foo]) + MyTrackableModel.instance_variable_set(:@tracked_embeds_one, %w[my_embed_one_model]) + MyTrackableModel.instance_variable_set(:@tracked_embeds_many, %w[my_embed_many_models]) MyTrackableModel.clear_trackable_memoization end diff --git a/spec/unit/trackable_spec.rb b/spec/unit/trackable_spec.rb index 481668f0..f866176b 100644 --- a/spec/unit/trackable_spec.rb +++ b/spec/unit/trackable_spec.rb @@ -34,8 +34,8 @@ class HistoryTracker end before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options } let(:expected_option) do - { on: %i(foo), - except: %w(created_at updated_at), + { on: %i[foo], + except: %w[created_at updated_at], tracker_class_name: nil, modifier_field: :modifier, version_field: :version, @@ -44,13 +44,13 @@ class HistoryTracker track_create: false, track_update: true, track_destroy: false, - fields: %w(foo), + fields: %w[foo], relations: { embeds_one: {}, embeds_many: {} }, dynamic: [], format: {} } end let(:regular_fields) { ['foo'] } - let(:reserved_fields) { %w(_id version modifier_id) } + let(:reserved_fields) { %w[_id version modifier_id] } it 'should have default options' do expect(Mongoid::History.trackable_class_options[:my_model]).to eq(expected_option) @@ -217,7 +217,7 @@ class MySubModel < MyModel it 'should be rescued if an exception occurs' do begin MyModel.disable_tracking do - fail 'exception' + raise 'exception' end rescue end @@ -260,7 +260,7 @@ class MyModel2 Mongoid::History.disable do begin MyModel.disable_tracking do - fail 'exception' + raise 'exception' end rescue end @@ -289,7 +289,7 @@ class MyModel2 begin Mongoid::History.disable do MyModel.disable_tracking do - fail 'exception' + raise 'exception' end end rescue