diff --git a/.rubocop.yml b/.rubocop.yml index da26fda..5fd2372 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -25,4 +25,4 @@ Style/FormatStringToken: # Let's not open a big PR to fix all of these at once - # we can fix gradually if we happen to be editing a file that has a violation Metrics/LineLength: - Max: 142 + Max: 110 diff --git a/lib/factory_bot/attribute_assigner.rb b/lib/factory_bot/attribute_assigner.rb index 08490b7..1d53a44 100644 --- a/lib/factory_bot/attribute_assigner.rb +++ b/lib/factory_bot/attribute_assigner.rb @@ -68,7 +68,11 @@ module FactoryBot end def attribute_names_to_assign - @attribute_names_to_assign ||= non_ignored_attribute_names + override_names - ignored_attribute_names - alias_names_to_ignore + @attribute_names_to_assign ||= + non_ignored_attribute_names + + override_names - + ignored_attribute_names - + alias_names_to_ignore end def non_ignored_attribute_names @@ -94,9 +98,15 @@ module FactoryBot def alias_names_to_ignore @attribute_list.non_ignored.flat_map do |attribute| override_names.map do |override| - attribute.name if attribute.alias_for?(override) && attribute.name != override && !ignored_attribute_names.include?(override) + attribute.name if ignorable_alias?(attribute, override) end end.compact end + + def ignorable_alias?(attribute, override) + attribute.alias_for?(override) && + attribute.name != override && + !ignored_attribute_names.include?(override) + end end end diff --git a/lib/factory_bot/attribute_list.rb b/lib/factory_bot/attribute_list.rb index d74c18d..18b998f 100644 --- a/lib/factory_bot/attribute_list.rb +++ b/lib/factory_bot/attribute_list.rb @@ -54,7 +54,8 @@ module FactoryBot def ensure_attribute_not_self_referencing!(attribute) if attribute.respond_to?(:factory) && attribute.factory == @name - raise AssociationDefinitionError, "Self-referencing association '#{attribute.name}' in '#{attribute.factory}'" + message = "Self-referencing association '#{attribute.name}' in '#{attribute.factory}'" + raise AssociationDefinitionError, message end end diff --git a/lib/factory_bot/definition_proxy.rb b/lib/factory_bot/definition_proxy.rb index b762556..18abbf2 100644 --- a/lib/factory_bot/definition_proxy.rb +++ b/lib/factory_bot/definition_proxy.rb @@ -1,6 +1,19 @@ module FactoryBot class DefinitionProxy - UNPROXIED_METHODS = %w(__send__ __id__ nil? send object_id extend instance_eval initialize block_given? raise caller method).freeze + UNPROXIED_METHODS = %w( + __send__ + __id__ + nil? + send + object_id + extend + instance_eval + initialize + block_given? + raise + caller + method + ).freeze (instance_methods + private_instance_methods).each do |method| undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s) diff --git a/lib/factory_bot/strategy/stub.rb b/lib/factory_bot/strategy/stub.rb index 1f9ab14..d18b28f 100644 --- a/lib/factory_bot/strategy/stub.rb +++ b/lib/factory_bot/strategy/stub.rb @@ -61,7 +61,8 @@ module FactoryBot DISABLED_PERSISTENCE_METHODS.each do |write_method| define_singleton_method(write_method) do |*args| - raise "stubbed models are not allowed to access the database - #{self.class}##{write_method}(#{args.join(',')})" + raise "stubbed models are not allowed to access the database - "\ + "#{self.class}##{write_method}(#{args.join(',')})" end end end diff --git a/spec/acceptance/attribute_existing_on_object_spec.rb b/spec/acceptance/attribute_existing_on_object_spec.rb index e69e560..0b58760 100644 --- a/spec/acceptance/attribute_existing_on_object_spec.rb +++ b/spec/acceptance/attribute_existing_on_object_spec.rb @@ -46,7 +46,8 @@ describe "assigning overrides that are also private methods on object" do its(:some_funky_method) { should eq "foobar!" } end -describe "accessing methods from the instance within a dynamic attribute that is also a private method on object" do +describe "accessing methods from the instance within a dynamic attribute "\ + "that is also a private method on object" do before do define_model("Website", more_format: :string) do def format diff --git a/spec/acceptance/defining_methods_inside_a_factory_spec.rb b/spec/acceptance/defining_methods_inside_a_factory_spec.rb index 0f9a6aa..1350a68 100644 --- a/spec/acceptance/defining_methods_inside_a_factory_spec.rb +++ b/spec/acceptance/defining_methods_inside_a_factory_spec.rb @@ -2,7 +2,7 @@ describe "defining methods inside FactoryBot" do it "raises with a meaningful message" do define_model("User") - expect do + bad_factory_definition = -> do FactoryBot.define do factory :user do def generate_name @@ -10,6 +10,11 @@ describe "defining methods inside FactoryBot" do end end end - end.to raise_error FactoryBot::MethodDefinitionError, /Defining methods in blocks \(trait or factory\) is not supported \(generate_name\)/ + end + + expect(bad_factory_definition).to raise_error( + FactoryBot::MethodDefinitionError, + /Defining methods in blocks \(trait or factory\) is not supported \(generate_name\)/, + ) end end