class RuboCop::Cop::Rails::AttributeDefaultBlockValue
end
attribute :custom_attribute, :integer, default: FOO
FOO = 123
class User < ApplicationRecord
# good
end
attribute :login_count, :integer, default: 0
class User < ApplicationRecord
# good
end
attribute :activated, :boolean, default: false
class User < ApplicationRecord
# good
end
attribute :role, :string, default: :customer
class User < ApplicationRecord
# good
end
attribute :configuration, default: -> { {} }
class User < ApplicationRecord
# good
end
attribute :configuration, default: {}
class User < ApplicationRecord
# bad
end
attribute :roles, :string, array: true, default: -> { [] }
class User < ApplicationRecord
# good
end
attribute :roles, :string, array: true, default: []
class User < ApplicationRecord
# bad
end
attribute :confirmed_at, :datetime, default: -> { Time.zone.now }
class User < ApplicationRecord
# good
end
attribute :confirmed_at, :datetime, default: Time.zone.now
class User < ApplicationRecord
# bad
@example
as well as constants.
It will accept all other values, such as string, symbol, integer and float literals
which value is an array, string literal or method call without a block.
Looks for ‘attribute` class methods that specify a `:default` option
def on_send(node)
def on_send(node) default_attribute(node) do |attribute| value = attribute.children.last return unless TYPE_OFFENDERS.any?(value.type) add_offense(value) do |corrector| expression = default_attribute(node).children.last corrector.replace(value, "-> { #{expression.source} }") end end end