-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Chewy/FieldType * Changelog
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Chewy | ||
# This cop force to specify a type for Chewy field | ||
# | ||
# # bad | ||
# field :name | ||
# | ||
# # good | ||
# field :name, type: 'text' | ||
class FieldType < Base | ||
MSG = 'Specify a `type` for Chewy field.' | ||
|
||
RESTRICT_ON_SEND = %i[field].freeze | ||
|
||
# @!method options_has_field?(node) | ||
def_node_matcher :options_has_field?, <<~PATTERN | ||
(send nil? :field (sym _)+ (hash <(pair (sym :type) {str sym}) ...>)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return if options_has_field?(node) | ||
|
||
add_offense(node) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Chewy::FieldType, :config do | ||
it 'registers an offense when no specifying a field' do | ||
expect_offense(<<~RUBY) | ||
field :name | ||
^^^^^^^^^^^ Specify a `type` for Chewy field. | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
field :name, value: 'text' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify a `type` for Chewy field. | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
field :name, value: lambda { |a| a.name.upcase } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify a `type` for Chewy field. | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
defined_type User do | ||
field :name | ||
^^^^^^^^^^^ Specify a `type` for Chewy field. | ||
end | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
field :name, :email | ||
^^^^^^^^^^^^^^^^^^^ Specify a `type` for Chewy field. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when a type is specified' do | ||
expect_no_offenses(<<~RUBY) | ||
field :name, type: 'text' | ||
RUBY | ||
|
||
expect_no_offenses(<<~RUBY) | ||
field :name, type: :text | ||
RUBY | ||
|
||
expect_no_offenses(<<~RUBY) | ||
field :name, :email, type: 'text' | ||
RUBY | ||
end | ||
end |