class RuboCop::Cop::Rails::ReadWriteAttribute

This cop checks for the use of the `read_attribute` or `write_attribute` methods and recommends square brackets instead.

If an attribute is missing from the instance (for example, when initialized by a partial `select`) then `read_attribute` will return nil, but square brackets will raise an `ActiveModel::MissingAttributeError`.

Explicitly raising an error in this situation is preferable, and that is why rubocop recommends using square brackets.

@example

# bad
x = read_attribute(:attr)
write_attribute(:attr, val)

# good
x = self[:attr]
self[:attr] = val

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rails/read_write_attribute.rb, line 39
def on_send(node)
  return unless read_write_attribute?(node)

  add_offense(node.loc.selector, message: message(node)) do |corrector|
    case node.method_name
    when :read_attribute
      replacement = read_attribute_replacement(node)
    when :write_attribute
      replacement = write_attribute_replacement(node)
    end

    corrector.replace(node.source_range, replacement)
  end
end

Private Instance Methods

message(node) click to toggle source
# File lib/rubocop/cop/rails/read_write_attribute.rb, line 56
def message(node)
  if node.method?(:read_attribute)
    format(MSG, prefer: 'self[:attr]', current: 'read_attribute(:attr)')
  else
    format(MSG, prefer: 'self[:attr] = val',
                current: 'write_attribute(:attr, val)')
  end
end
read_attribute_replacement(node) click to toggle source
# File lib/rubocop/cop/rails/read_write_attribute.rb, line 65
def read_attribute_replacement(node)
  "self[#{node.first_argument.source}]"
end
write_attribute_replacement(node) click to toggle source
# File lib/rubocop/cop/rails/read_write_attribute.rb, line 69
def write_attribute_replacement(node)
  "self[#{node.first_argument.source}] = #{node.last_argument.source}"
end