Skip to content

Commit

Permalink
[Fix rubocop#227] Make Rails/UniqueValidationWithoutIndex aware of …
Browse files Browse the repository at this point in the history
…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
koic committed Apr 8, 2020
1 parent 02e73e4 commit 7bdbb67
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#227](https://github.com/rubocop-hq/rubocop-rails/issues/227): Make `Rails/UniqueValidationWithoutIndex` aware of updating db/schema.rb. ([@koic][])

## 2.5.1 (2020-04-02)

### Bug fixes
Expand Down
14 changes: 14 additions & 0 deletions lib/rubocop/cop/mixin/active_record_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ module ActiveRecordHelper
(send nil? :belongs_to {str sym} ...)
PATTERN

def external_dependency_checksum
if defined?(@external_dependency_checksum)
return @external_dependency_checksum
end

schema_code = File.read(RuboCop::Rails::SchemaLoader.db_schema_path)

@external_dependency_checksum ||= Digest::SHA1.hexdigest(schema_code)
end

def schema
RuboCop::Rails::SchemaLoader.load(target_ruby_version)
end

def table_name(class_node)
table_name = find_set_table_name(class_node).to_a.last&.first_argument
return table_name.value.to_s if table_name
Expand Down
4 changes: 0 additions & 4 deletions lib/rubocop/cop/rails/unique_validation_without_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ def array_node_to_array(node)
end
end
end

def schema
RuboCop::Rails::SchemaLoader.load(target_ruby_version)
end
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/rubocop/rails/schema_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ def reset!
remove_instance_variable(:@schema)
end

private

def load!(target_ruby_version)
path = db_schema_path
return unless path

ast = parse(path, target_ruby_version)
Schema.new(ast)
end

def db_schema_path
path = Pathname.pwd
until path.root?
Expand All @@ -46,6 +36,16 @@ def db_schema_path
nil
end

private

def load!(target_ruby_version)
path = db_schema_path
return unless path

ast = parse(path, target_ruby_version)
Schema.new(ast)
end

def parse(path, target_ruby_version)
klass_name = :"Ruby#{target_ruby_version.to_s.sub('.', '')}"
klass = ::Parser.const_get(klass_name)
Expand Down

0 comments on commit 7bdbb67

Please sign in to comment.