Skip to content

Commit

Permalink
Allow to use frozen scope
Browse files Browse the repository at this point in the history
  • Loading branch information
krim committed Jul 31, 2020
1 parent d9a41e5 commit 850037c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Bug fixes

* [#315](https://github.com/rubocop-hq/rubocop-rails/pull/315): Allow to use frozen scope for `Rails/UniqueValidationWithoutIndex`. ([@krim][])
* [#313](https://github.com/rubocop-hq/rubocop-rails/pull/313): Fix `Rails/ActiveRecordCallbacksOrder` to preserve the original callback execution order. ([@eugeneius][])

### Changes
Expand Down Expand Up @@ -256,3 +257,4 @@
[@jcoyne]: https://github.com/jcoyne
[@fatkodima]: https://github.com/fatkodima
[@taylorthurlow]: https://github.com/taylorthurlow
[@krim]: https://github.com/krim
6 changes: 6 additions & 0 deletions lib/rubocop/cop/rails/unique_validation_without_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def column_names_from_scope(node)
scope = find_scope(uniq)
return unless scope

scope = unfreeze_scope(scope)

case scope.type
when :sym, :str
[scope.value]
Expand All @@ -114,6 +116,10 @@ def find_scope(pairs)
end
end

def unfreeze_scope(scope)
scope.send_type? && scope.method?(:freeze) ? scope.children.first : scope
end

def class_node(node)
node.each_ancestor.find(&:class_type?)
end
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/rails/unique_validation_without_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ class WrittenArticles
end
RUBY
end

context 'when scope is frozen' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class WrittenArticles
validates :a_id, uniqueness: { scope: [:b_id, :c_id].freeze }
end
RUBY
end
end
end
end

Expand Down

0 comments on commit 850037c

Please sign in to comment.