Skip to content

Commit

Permalink
Change terminology to ForbiddenMethods and AllowedMethods
Browse files Browse the repository at this point in the history
Use terminology which is more descriptive and is consistent with the direction of some upstream libraries:
rubocop/rubocop#6467
rails/rails#33681
  • Loading branch information
jcoyne committed Jun 23, 2020
1 parent 4c8dce3 commit dfbcac6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change log

## master (unreleased)
* [#263](https://github.com/rubocop-hq/rubocop-rails/pull/263): Change terminology to `ForbiddenMethods` and `AllowedMethods`. ([@jcoyne][])

## 2.6.0 (2020-06-08)

Expand Down Expand Up @@ -199,3 +200,4 @@
[@diogoosorio]: https://github.com/diogoosorio
[@tabuchi0919]: https://github.com/tabuchi0919
[@ghiculescu]: https://github.com/ghiculescu
[@jcoyne]: https://github.com/jcoyne
4 changes: 2 additions & 2 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ Rails/SkipsModelValidations:
Enabled: true
VersionAdded: '0.47'
VersionChanged: '0.60'
Blacklist:
ForbiddenMethods:
- decrement!
- decrement_counter
- increment!
Expand All @@ -518,7 +518,7 @@ Rails/SkipsModelValidations:
- update_column
- update_columns
- update_counters
Whitelist: []
AllowedMethods: []

Rails/TimeZone:
Description: 'Checks the correct usage of time zone aware methods.'
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3181,11 +3181,11 @@ user.touch
|===
| Name | Default value | Configurable values

| Blacklist
| ForbiddenMethods
| `decrement!`, `decrement_counter`, `increment!`, `increment_counter`, `toggle!`, `touch`, `update_all`, `update_attribute`, `update_column`, `update_columns`, `update_counters`
| Array

| Whitelist
| AllowedMethods
| `[]`
| Array
|===
Expand Down
4 changes: 2 additions & 2 deletions legacy-docs/cops_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -2499,8 +2499,8 @@ user.touch

Name | Default value | Configurable values
--- | --- | ---
Blacklist | `decrement!`, `decrement_counter`, `increment!`, `increment_counter`, `toggle!`, `touch`, `update_all`, `update_attribute`, `update_column`, `update_columns`, `update_counters` | Array
Whitelist | `[]` | Array
ForbiddenMethods | `decrement!`, `decrement_counter`, `increment!`, `increment_counter`, `toggle!`, `touch`, `update_all`, `update_attribute`, `update_column`, `update_columns`, `update_counters` | Array
AllowedMethods | `[]` | Array

### References

Expand Down
12 changes: 6 additions & 6 deletions lib/rubocop/cop/rails/skips_model_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class SkipsModelValidations < Cop
PATTERN

def on_send(node)
return if whitelist.include?(node.method_name.to_s)
return unless blacklist.include?(node.method_name.to_s)
return if allowed_methods.include?(node.method_name.to_s)
return unless forbidden_methods.include?(node.method_name.to_s)
return if allowed_method?(node)
return if good_touch?(node)

Expand All @@ -77,12 +77,12 @@ def allowed_method?(node)
!node.arguments?
end

def blacklist
cop_config['Blacklist'] || []
def forbidden_methods
cop_config['ForbiddenMethods'] || []
end

def whitelist
cop_config['Whitelist'] || []
def allowed_methods
cop_config['AllowedMethods'] || []
end
end
end
Expand Down
48 changes: 24 additions & 24 deletions spec/rubocop/cop/rails/skips_model_validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

RSpec.describe RuboCop::Cop::Rails::SkipsModelValidations, :config do
cop_config = {
'Blacklist' => %w[decrement!
decrement_counter
increment!
increment_counter
toggle!
touch
update_all
update_attribute
update_column
update_columns
update_counters]
'ForbiddenMethods' => %w[decrement!
decrement_counter
increment!
increment_counter
toggle!
touch
update_all
update_attribute
update_column
update_columns
update_counters]
}

subject(:cop) { described_class.new(config) }
Expand All @@ -22,8 +22,8 @@

methods_with_arguments = described_class::METHODS_WITH_ARGUMENTS

context 'with default blacklist' do
cop_config['Blacklist'].each do |method_name|
context 'with default forbidden methods' do
cop_config['ForbiddenMethods'].each do |method_name|
it "registers an offense for `#{method_name}`" do
inspect_source("User.#{method_name}(:attr)")
expect(cop.offenses.size).to eq(1)
Expand Down Expand Up @@ -54,7 +54,7 @@
end

context "with methods that don't require an argument" do
(cop_config['Blacklist'] - methods_with_arguments).each do |method_name|
(cop_config['ForbiddenMethods'] - methods_with_arguments).each do |method_name|
it "registers an offense for `#{method_name}`" do
inspect_source("User.#{method_name}")
expect(cop.offenses.size).to eq(1)
Expand All @@ -64,16 +64,16 @@
end
end

context 'with `update_attribute` method in blacklist' do
context 'with `update_attribute` method in forbidden methods' do
let(:cop_config) do
{ 'Blacklist' => %w[update_attribute] }
{ 'ForbiddenMethods' => %w[update_attribute] }
end

whitelist = cop_config['Blacklist'].reject do |val|
allowed_methods = cop_config['ForbiddenMethods'].reject do |val|
val == 'update_attribute'
end

whitelist.each do |method_name|
allowed_methods.each do |method_name|
it "accepts `#{method_name}`" do
expect_no_offenses("User.#{method_name}")
end
Expand All @@ -96,31 +96,31 @@
end
end

context 'with whitelist' do
context 'with allowed methods' do
let(:cop_config) do
{
'Blacklist' => %w[toggle! touch],
'Whitelist' => %w[touch]
'ForbiddenMethods' => %w[toggle! touch],
'AllowedMethods' => %w[touch]
}
end

it 'registers an offense for method not in whitelist' do
it 'registers an offense for method not in allowed methods' do
expect_offense(<<~RUBY)
user.toggle!(:active)
^^^^^^^ Avoid using `toggle!` because it skips validations.
RUBY
end

context 'when using safe navigation operator' do
it 'registers an offense for method not in whitelist' do
it 'registers an offense for method not in allowed methods' do
expect_offense(<<~RUBY)
user&.toggle!(:active)
^^^^^^^ Avoid using `toggle!` because it skips validations.
RUBY
end
end

it 'accepts method in whitelist, superseding the blacklist' do
it 'accepts method in allowed methods, superseding the forbidden methods' do
expect_no_offenses('User.touch(:attr)')
end
end
Expand Down

0 comments on commit dfbcac6

Please sign in to comment.