mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Arrest lines of an unsavory length
We had configured RuboCop to allow lines with up to 142 characters. This PR fixes a few of the worst offenders so we can bring that number down to 110.
This commit is contained in:
parent
df761d7a48
commit
cb9eb551c8
7 changed files with 40 additions and 9 deletions
|
@ -25,4 +25,4 @@ Style/FormatStringToken:
|
||||||
# Let's not open a big PR to fix all of these at once -
|
# 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
|
# we can fix gradually if we happen to be editing a file that has a violation
|
||||||
Metrics/LineLength:
|
Metrics/LineLength:
|
||||||
Max: 142
|
Max: 110
|
||||||
|
|
|
@ -68,7 +68,11 @@ module FactoryBot
|
||||||
end
|
end
|
||||||
|
|
||||||
def attribute_names_to_assign
|
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
|
end
|
||||||
|
|
||||||
def non_ignored_attribute_names
|
def non_ignored_attribute_names
|
||||||
|
@ -94,9 +98,15 @@ module FactoryBot
|
||||||
def alias_names_to_ignore
|
def alias_names_to_ignore
|
||||||
@attribute_list.non_ignored.flat_map do |attribute|
|
@attribute_list.non_ignored.flat_map do |attribute|
|
||||||
override_names.map do |override|
|
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
|
||||||
end.compact
|
end.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def ignorable_alias?(attribute, override)
|
||||||
|
attribute.alias_for?(override) &&
|
||||||
|
attribute.name != override &&
|
||||||
|
!ignored_attribute_names.include?(override)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -54,7 +54,8 @@ module FactoryBot
|
||||||
|
|
||||||
def ensure_attribute_not_self_referencing!(attribute)
|
def ensure_attribute_not_self_referencing!(attribute)
|
||||||
if attribute.respond_to?(:factory) && attribute.factory == @name
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,19 @@
|
||||||
module FactoryBot
|
module FactoryBot
|
||||||
class DefinitionProxy
|
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|
|
(instance_methods + private_instance_methods).each do |method|
|
||||||
undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s)
|
undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s)
|
||||||
|
|
|
@ -61,7 +61,8 @@ module FactoryBot
|
||||||
|
|
||||||
DISABLED_PERSISTENCE_METHODS.each do |write_method|
|
DISABLED_PERSISTENCE_METHODS.each do |write_method|
|
||||||
define_singleton_method(write_method) do |*args|
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -46,7 +46,8 @@ describe "assigning overrides that are also private methods on object" do
|
||||||
its(:some_funky_method) { should eq "foobar!" }
|
its(:some_funky_method) { should eq "foobar!" }
|
||||||
end
|
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
|
before do
|
||||||
define_model("Website", more_format: :string) do
|
define_model("Website", more_format: :string) do
|
||||||
def format
|
def format
|
||||||
|
|
|
@ -2,7 +2,7 @@ describe "defining methods inside FactoryBot" do
|
||||||
it "raises with a meaningful message" do
|
it "raises with a meaningful message" do
|
||||||
define_model("User")
|
define_model("User")
|
||||||
|
|
||||||
expect do
|
bad_factory_definition = -> do
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :user do
|
factory :user do
|
||||||
def generate_name
|
def generate_name
|
||||||
|
@ -10,6 +10,11 @@ describe "defining methods inside FactoryBot" do
|
||||||
end
|
end
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue