Created
August 27, 2020 15:15
-
-
Save cilim/1353f469663a3595e7edb48fe4e3beb4 to your computer and use it in GitHub Desktop.
Rubocop Rails/AttributeDefaultBlockValue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module RuboCop | |
module Cop | |
module Rails | |
# This cop looks for `attribute` class methods that don't | |
# specify a `:default` option inside a block. | |
# | |
# @example | |
# # bad | |
# class User < ApplicationRecord | |
# attribute :confirmed_at, :datetime, default: Time.zone.now | |
# end | |
# | |
# # good | |
# class User < ActiveRecord::Base | |
# attribute :confirmed_at, :datetime, default: -> { Time.zone.now } | |
# end | |
class AttributeDefaultBlockValue < Cop | |
MSG = 'Pass a block to `:default` option.' | |
def_node_search :active_resource_class?, <<~PATTERN | |
(const (const nil? :ActiveResource) :Base) | |
PATTERN | |
def_node_matcher :attribute?, '(send nil? :attribute _ _ $hash)' | |
def on_send(node) | |
return if active_resource?(node.parent) | |
attribute?(node) do |third_arg| | |
default_attribute = default_attribute(third_arg) | |
unless [:block, :true, :false].include?(default_attribute.children.last.type) # rubocop:disable Lint/BooleanSymbol | |
add_offense(node, location: default_attribute) | |
end | |
end | |
end | |
private | |
def active_resource?(node) | |
return false if node.nil? | |
active_resource_class?(node) | |
end | |
def default_attribute(node) | |
node.children.first | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment