Skip to content

Commit

Permalink
[Fix #4196] Handle properly lambdas and procs in Rails/RelativeDateCo…
Browse files Browse the repository at this point in the history
…nstant (#4234)
  • Loading branch information
smakagon authored and bbatsov committed Apr 3, 2017
1 parent 67ca736 commit 212f68b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* [#4217](https://github.com/bbatsov/rubocop/pull/4217): Fix false positive in `Rails/FilePath` cop with non string argument. ([@soutaro][])
* [#4106](https://github.com/bbatsov/rubocop/pull/4106): Make `Style/TernaryParentheses` unsafe autocorrect detector aware of literals and constants. ([@drenmi][])
* [#4228](https://github.com/bbatsov/rubocop/pull/4228): Fix false positive in `Lint/AmbiguousBlockAssociation` cop. ([@smakagon][])
* [#4234](https://github.com/bbatsov/rubocop/pull/4234): Fix false positive in `Rails/RelativeDate` for lambdas and procs. ([@smakagon][])

## 0.48.0 (2017-03-26)

Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rails/relative_date_constant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class RelativeDateConstant < Cop
RELATIVE_DATE_METHODS = %i[ago from_now since until].freeze

def on_casgn(node)
return if node.children.last.lambda_or_proc?

bad_node = node.descendants.find { |n| bad_method?(n) }
return unless bad_node

Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/rails/relative_date_constant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
expect(cop.offenses).to be_empty
end

it 'accepts a lambda' do
inspect_source(cop,
['class SomeClass',
' EXPIRED_AT = -> { 1.year.ago }',
'end'])
expect(cop.offenses).to be_empty
end

it 'accepts a proc' do
inspect_source(cop,
['class SomeClass',
' EXPIRED_AT = Proc.new { 1.year.ago }',
'end'])
expect(cop.offenses).to be_empty
end

it 'registers an offense for exclusive end range' do
inspect_source(cop,
['class SomeClass',
Expand Down

0 comments on commit 212f68b

Please sign in to comment.