Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix an incorrect autocorrect for
Rails/ContentTag
cop
Follow up to rubocop#242. This PR allows `content_tag` when the first argument is a variable because `content_tag(name)` is simpler rather than `tag.public_send(name)`. When `public_send` is used, it becomes complicated as follows. First, `content_tag(name)` cannot be corrected to `tag.name` when the first argument is a variable. So use `public_send` and prevent the following errors in that case: ## Case 1: Using `public_send` to prevent the following `NoMethodError` Original code: ```ruby content_tag(name, 'foo', class: 'bar') ``` Auto-corrected code (before): ```ruby tag(name, 'foo', class: 'bar') #=> NoMethodError (undefined method `each_pair' for "foo":String) ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', class: 'bar') ``` ## Case 2: Using `symbolize_keys` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', {'class' => 'bar'}) ``` Auto-corrected code (before): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}.symbolize_keys) ``` The `symbolize_keys` may not be needed, but for safe auto-correction it will be added if optional argument keys are not all symbols. ## Case 3: Using `o ? o.symbolize_keys : {}` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', options) ``` Auto-corrected code (before): When the third argument is `nil`. ```ruby options = nil tag.public_send(name, 'foo', options) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', options ? options.symbolize_keys : {}) ``` Guard with the empty hash in case the third argument is `nil`.
- Loading branch information