Module: Axn::Internal::FieldConfig

Defined in:
lib/axn/internal/field_config.rb

Overview

Pure utility functions for inspecting field configuration objects.

Class Method Summary collapse

Class Method Details

.boolean?(config) ⇒ Boolean

Determines if a field is declared as boolean type.

Parameters:

  • config (Object)

    A field configuration object with a ‘validations` method

Returns:

  • (Boolean)

    true if the field is typed :boolean



28
29
30
# File 'lib/axn/internal/field_config.rb', line 28

def boolean?(config)
  Array(config.validations.dig(:type, :klass)) == [:boolean]
end

.optional?(config) ⇒ Boolean

Determines if a field is optional based on its validations. A field is optional if:

  1. It doesn’t have presence: true validation

  2. Any validator has allow_blank: true

Parameters:

  • config (Object)

    A field configuration object with a ‘validations` method

Returns:

  • (Boolean)

    true if the field is optional



16
17
18
19
20
21
22
# File 'lib/axn/internal/field_config.rb', line 16

def optional?(config)
  validations = config.validations
  return true unless validations.key?(:presence) && validations[:presence] == true

  # Check if any validator has allow_blank: true
  validations.values.any? { |v| v.is_a?(Hash) && v[:allow_blank] == true }
end