Class: RuboCop::Cop::Platanus::NoRenderJson
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Platanus::NoRenderJson
- Defined in:
- lib/rubocop/cop/platanus/no_render_json.rb
Overview
‘render json:` shouldn’t be used in controllers because it’ll skip
the use of ApiResponder and serializers. Use `respond_with` instead.
Constant Summary collapse
- RENDER_JSON_MSG =
'Use `respond_with` instead of `render json:`.'
- RENDER_HASH_MSG =
'Don\'t use `respond_with` with a hash. Use a value and serializer instead.'
- RESTRICT_ON_SEND =
%i[render respond_with].freeze
Instance Method Summary collapse
- #on_send(node) ⇒ Object
-
#respond_with_hash?(node) ⇒ Boolean
Determines if the node corresponds to a call of respond_with with a hash as it’s argument.
-
#var_is_hash?(node, var_name) ⇒ Boolean
Determines if a variable name corresponds to a hash within it’s most immediate scope.
Instance Method Details
#on_send(node) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/rubocop/cop/platanus/no_render_json.rb', line 85 def on_send(node) if render_json?(node) add_offense(node, message: RENDER_JSON_MSG) elsif respond_with_hash?(node) add_offense(node, message: RENDER_HASH_MSG) end end |
#respond_with_hash?(node) ⇒ Boolean
Determines if the node corresponds to a call of respond_with with a hash as it’s argument.
95 96 97 98 99 100 101 102 103 |
# File 'lib/rubocop/cop/platanus/no_render_json.rb', line 95 def respond_with_hash?(node) hash_node = respond_with(node) return true if is_hash?(hash_node) var_name = var_name(hash_node) return false if var_name.nil? var_is_hash?(node, var_name) end |
#var_is_hash?(node, var_name) ⇒ Boolean
Determines if a variable name corresponds to a hash within it’s most immediate scope.
107 108 109 110 111 112 113 114 115 |
# File 'lib/rubocop/cop/platanus/no_render_json.rb', line 107 def var_is_hash?(node, var_name) scope = node.parent return false unless scope pattern = "`(lvasgn :#{var_name} (hash ...))" matcher = RuboCop::AST::NodePattern.new(pattern) matcher.match(scope) end |