Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuboCop Rails supports Rails 4 or higher #74

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* [#43](https://github.com/rubocop-hq/rubocop-rails/issues/43): Remove `change_column_null` method from `BulkChangeTable` cop offenses. ([@anthony-robin][])

### Changes

* [#74](https://github.com/rubocop-hq/rubocop-rails/pull/74): Drop Rails 3 support. ([@koic][])

## 2.0.1 (2019-06-08)

### Changes
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ Rails/FindBy:
- lib/example.rb
```

## Compatibility

Rails cops support the following versions:

- Rails 4.0+

## Contributing

Checkout the [contribution guidelines](CONTRIBUTING.md).
Expand Down
3 changes: 0 additions & 3 deletions lib/rubocop/cop/rails/action_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ module Rails
# append_around_filter :do_stuff
# skip_after_filter :do_stuff
class ActionFilter < Cop
extend TargetRailsVersion
include ConfigurableEnforcedStyle

MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
Expand Down Expand Up @@ -70,8 +69,6 @@ class ActionFilter < Cop
skip_action_callback
].freeze

minimum_target_rails_version 4.0

def on_block(node)
check_method_node(node.send_node)
end
Expand Down
7 changes: 1 addition & 6 deletions lib/rubocop/cop/rails/reversible_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,7 @@ def check_remove_foreign_key_node(node)

def check_change_table_node(node, block)
change_table_call(node) do |arg|
if target_rails_version < 4.0
add_offense(
node,
message: format(MSG, action: 'change_table')
)
elsif block.send_type?
if block.send_type?
check_change_table_offense(arg, block)
else
block.each_child_node(:send) do |child_node|
Expand Down
138 changes: 42 additions & 96 deletions spec/rubocop/cop/rails/action_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::ActionFilter, :config do
subject(:cop) { described_class.new(config) }

let(:cop_config) { { 'Include' => nil } }

describe '::FILTER_METHODS' do
it 'contains all of the filter methods' do
expect(described_class::FILTER_METHODS).to eq(%i[
Expand Down Expand Up @@ -41,121 +45,63 @@
end
end

context 'Rails <= 4.0', :rails3 do
subject(:cop) { described_class.new(config) }

let(:cop_config) { { 'Include' => nil } }

context 'when using action methods' do
described_class::FILTER_METHODS.each do |method|
it "does not register an offense for #{method}" do
expect_no_offenses("#{method} :name")
end

it "does not register an offense for #{method} with block" do
expect_no_offenses("#{method} { |controller| something }")
end
end
context 'when style is action' do
before do
cop_config['EnforcedStyle'] = 'action'
end

described_class::ACTION_METHODS.each do |method|
it "accepts #{method}" do
expect_no_offenses("#{method} :something")
end
described_class::FILTER_METHODS.each do |method|
it "registers an offense for #{method}" do
inspect_source_file("#{method} :name")
expect(cop.offenses.size).to eq(1)
end

it 'does not auto-correct to preferred method' do
new_source = autocorrect_source_file('before_filter :test')
expect(new_source).to eq('before_filter :test')
it "registers an offense for #{method} with block" do
inspect_source_file("#{method} { |controller| something }")
expect(cop.offenses.size).to eq(1)
end
end

context 'when using filter methods' do
described_class::ACTION_METHODS.each do |method|
it "does not register an offense for #{method}" do
expect_no_offenses("#{method} :name")
end

it "does not register an offense for #{method} with block" do
expect_no_offenses("#{method} { |controller| something }")
end
end

described_class::FILTER_METHODS.each do |method|
it "accepts #{method}" do
expect_no_offenses("#{method} :something")
end
described_class::ACTION_METHODS.each do |method|
it "accepts #{method}" do
inspect_source_file("#{method} :something")
expect(cop.offenses.empty?).to be(true)
end
end

it 'does not auto-correct to preferred method' do
new_source = autocorrect_source_file('before_action :test')
expect(new_source).to eq('before_action :test')
end
it 'auto-corrects to preferred method' do
new_source = autocorrect_source_file('before_filter :test')
expect(new_source).to eq('before_action :test')
end
end

context 'Rails >= 4.0', :rails4 do
subject(:cop) { described_class.new(config) }

let(:cop_config) { { 'Include' => nil } }

context 'when style is action' do
before do
cop_config['EnforcedStyle'] = 'action'
end

described_class::FILTER_METHODS.each do |method|
it "registers an offense for #{method}" do
inspect_source_file("#{method} :name")
expect(cop.offenses.size).to eq(1)
end

it "registers an offense for #{method} with block" do
inspect_source_file("#{method} { |controller| something }")
expect(cop.offenses.size).to eq(1)
end
end

described_class::ACTION_METHODS.each do |method|
it "accepts #{method}" do
inspect_source_file("#{method} :something")
expect(cop.offenses.empty?).to be(true)
end
end

it 'auto-corrects to preferred method' do
new_source = autocorrect_source_file('before_filter :test')
expect(new_source).to eq('before_action :test')
end
context 'when style is filter' do
before do
cop_config['EnforcedStyle'] = 'filter'
end

context 'when style is filter' do
before do
cop_config['EnforcedStyle'] = 'filter'
described_class::ACTION_METHODS.each do |method|
it "registers an offense for #{method}" do
inspect_source_file("#{method} :name")
expect(cop.offenses.size).to eq(1)
end

described_class::ACTION_METHODS.each do |method|
it "registers an offense for #{method}" do
inspect_source_file("#{method} :name")
expect(cop.offenses.size).to eq(1)
end

it "registers an offense for #{method} with block" do
inspect_source_file("#{method} { |controller| something }")
expect(cop.offenses.size).to eq(1)
end
it "registers an offense for #{method} with block" do
inspect_source_file("#{method} { |controller| something }")
expect(cop.offenses.size).to eq(1)
end
end

described_class::FILTER_METHODS.each do |method|
it "accepts #{method}" do
inspect_source_file("#{method} :something")
expect(cop.offenses.empty?).to be(true)
end
described_class::FILTER_METHODS.each do |method|
it "accepts #{method}" do
inspect_source_file("#{method} :something")
expect(cop.offenses.empty?).to be(true)
end
end

it 'auto-corrects to preferred method' do
new_source = autocorrect_source_file('before_action :test')
expect(new_source).to eq('before_filter :test')
end
it 'auto-corrects to preferred method' do
new_source = autocorrect_source_file('before_action :test')
expect(new_source).to eq('before_filter :test')
end
end
end
74 changes: 22 additions & 52 deletions spec/rubocop/cop/rails/reversible_migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,60 +151,30 @@ def change
end

context 'change_table' do
context 'Rails < 4.0', :rails3 do
it_behaves_like 'offense', 'change_table', <<-RUBY
change_table :users do |t|
t.column :name, :string
t.text :description
t.boolean :authorized
end
RUBY

it_behaves_like 'offense', 'change_table', <<-RUBY
change_table :users do |t|
t.change :description, :text
end
RUBY

it_behaves_like 'offense', 'change_table', <<-RUBY
change_table :users do |t|
t.change_default :authorized, 1
end
RUBY

it_behaves_like 'offense', 'change_table', <<-RUBY
change_table :users do |t|
t.remove :qualification
end
RUBY
end

context 'Rails >= 4.0', :rails4 do
it_behaves_like 'accepts', 'change_table(with reversible calls)', <<-RUBY
change_table :users do |t|
t.column :name, :string
t.text :description
t.boolean :authorized
end
RUBY
it_behaves_like 'accepts', 'change_table(with reversible calls)', <<-RUBY
change_table :users do |t|
t.column :name, :string
t.text :description
t.boolean :authorized
end
RUBY

it_behaves_like 'offense', 'change_table(with change)', <<-RUBY
change_table :users do |t|
t.change :description, :text
end
RUBY
it_behaves_like 'offense', 'change_table(with change)', <<-RUBY
change_table :users do |t|
t.change :description, :text
end
RUBY

it_behaves_like 'offense', 'change_table(with change_default)', <<-RUBY
change_table :users do |t|
t.change_default :authorized, 1
end
RUBY
it_behaves_like 'offense', 'change_table(with change_default)', <<-RUBY
change_table :users do |t|
t.change_default :authorized, 1
end
RUBY

it_behaves_like 'offense', 'change_table(with remove)', <<-RUBY
change_table :users do |t|
t.remove :qualification
end
RUBY
end
it_behaves_like 'offense', 'change_table(with remove)', <<-RUBY
change_table :users do |t|
t.remove :qualification
end
RUBY
end
end
4 changes: 0 additions & 4 deletions spec/support/shared_contexts.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# frozen_string_literal: true

RSpec.shared_context 'with Rails 3', :rails3 do
let(:rails_version) { 3.0 }
end

RSpec.shared_context 'with Rails 4', :rails4 do
let(:rails_version) { 4.0 }
end
Expand Down