Skip to content

Commit

Permalink
Merge pull request #76 from rubocop/fix62
Browse files Browse the repository at this point in the history
Fix a false positive for `FactoryBot/FactoryNameStyle` when namespaced models
  • Loading branch information
ydah committed Sep 20, 2023
2 parents d548d77 + bb2e284 commit 0da664d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix a false positive for `FactoryBot/FactoryNameStyle` when namespaced models. ([@ydah])

## 2.24.0 (2023-09-18)

- Fix `FactoryBot/AssociationStyle` cop to ignore explicit associations with `strategy: :build`. ([@pirj])
Expand Down
3 changes: 3 additions & 0 deletions docs/modules/ROOT/pages/cops_factorybot.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ build "user", username: "NAME"
# good
create(:user)
build :user, username: "NAME"
# good - namespaced models
create('users/internal')
----

==== EnforcedStyle: string
Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/factory_bot/factory_name_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module FactoryBot
# create(:user)
# build :user, username: "NAME"
#
# # good - namespaced models
# create('users/internal')
#
# @example EnforcedStyle: string
# # bad
# create(:user)
Expand Down Expand Up @@ -76,13 +79,17 @@ def on_send(node)
private

def offense_for_symbol_style?(name)
name.str_type? && style == :symbol
name.str_type? && style == :symbol && !namespaced?(name)
end

def offense_for_string_style?(name)
name.sym_type? && style == :string
end

def namespaced?(name)
name.value.include?('/')
end

def register_offense(name, prefer)
add_offense(name,
message: format(MSG, prefer: style.to_s)) do |corrector|
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/factory_bot/factory_name_style_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@
build user: :foo
RUBY
end

it 'does not register an offense when using `create` ' \
'with a method call when string include /' do
expect_no_offenses(<<~RUBY)
create("users/internal")
RUBY
end
end

context 'when EnforcedStyle is :string' do
Expand Down

0 comments on commit 0da664d

Please sign in to comment.