forked from rubocop/rubocop-rails
-
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.
…updating schema.rb Fixes rubocop#227. This PR makes `Rails/UniqueValidationWithoutIndex` aware of updating db/schema.rb `Rails/UniqueValidationWithoutIndex` cop needs to know both model and db/schema.rb changes to register an offense. However, with default RuboCop, only changes to the model affect cache behavior. This PR ensures that changes to db/schema.rb affect the cache by overriding the following method: ```ruby # This method should be overridden when a cop's behavior depends # on state that lives outside of these locations: # # (1) the file under inspection # (2) the cop's source code # (3) the config (eg a .rubocop.yml file) # # For example, some cops may want to look at other parts of # the codebase being inspected to find violations. A cop may # use the presence or absence of file `foo.rb` to determine # whether a certain violation exists in `bar.rb`. # # Overriding this method allows the cop to indicate to RuboCop's # ResultCache system when those external dependencies change, # ie when the ResultCache should be invalidated. def external_dependency_checksum nil end ``` https://github.com/rubocop-hq/rubocop/blob/v0.81.0/lib/rubocop/cop/cop.rb#L222-L239
- Loading branch information
Showing
5 changed files
with
80 additions
and
14 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
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::ActiveRecordHelper, :config do | ||
include FileHelper | ||
|
||
module RuboCop | ||
module Cop | ||
class Test < Cop | ||
include ActiveRecordHelper | ||
end | ||
end | ||
end | ||
|
||
let(:cop) do | ||
RuboCop::Cop::Test.new | ||
end | ||
|
||
let(:schema_path) { 'db/schema.rb' } | ||
|
||
after { FileUtils.rm_rf(schema_path) } | ||
|
||
describe '#external_dependency_checksum' do | ||
subject { cop.external_dependency_checksum } | ||
|
||
context 'with db/schema.rb' do | ||
before do | ||
create_file(schema_path, <<~RUBY) | ||
ActiveRecord::Schema.define(version: 2020_04_08_082625) do | ||
create_table "tests" do |t| | ||
t.string "foo" | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to eq 'eb198d7aa709c189fec9f41bc74da07843b4d67a' } | ||
end | ||
|
||
context 'with empty db/schema.rb' do | ||
before { create_empty_file(schema_path) } | ||
|
||
it { is_expected.to eq 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc' } | ||
end | ||
|
||
context 'without db/schema.rb' do | ||
it { is_expected.to be_nil } | ||
end | ||
end | ||
end |