Module: Boba::ActiveRecord::AttributeService

Extended by:
T::Sig
Defined in:
lib/boba/active_record/attribute_service.rb

Class Method Summary collapse

Class Method Details

.has_non_null_database_constraint?(constant, column_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/boba/active_record/attribute_service.rb', line 29

def has_non_null_database_constraint?(constant, column_name)
  column = constant.columns_hash[column_name]
  return false if column.nil?

  !column.null
rescue StandardError
  false
end

.has_unconditional_presence_validator?(constant, attribute) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/boba/active_record/attribute_service.rb', line 39

def has_unconditional_presence_validator?(constant, attribute)
  return false unless constant.respond_to?(:validators_on)

  constant.validators_on(attribute).any? do |validator|
    unconditional_presence_validator?(validator)
  end
end

.nilable_attribute?(constant, attribute, column_name: attribute) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/boba/active_record/attribute_service.rb', line 17

def nilable_attribute?(constant, attribute, column_name: attribute)
  return false if has_non_null_database_constraint?(constant, column_name)

  !has_unconditional_presence_validator?(constant, attribute)
end

.virtual_attribute?(constant, column_name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/boba/active_record/attribute_service.rb', line 24

def virtual_attribute?(constant, column_name)
  constant.columns_hash[column_name].nil?
end