Class: IronAdmin::ActionField

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_admin/action_field.rb

Overview

Value object representing a single field in an action form.

ActionFields define what inputs are shown to the user when an action requires parameters before execution (e.g., a reason, a date, a choice).

Examples:

Creating an action field

ActionField.new(name: :reason, type: :textarea, required: true)

Creating a select field with options

ActionField.new(name: :priority, type: :select, options: %w[low medium high])

Constant Summary collapse

TYPES =

Supported field types for action forms.

%i[text textarea number boolean date datetime select].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type: :text, label: nil, required: false, default: nil, placeholder: nil, options: nil) ⇒ ActionField

Returns a new instance of ActionField.

Parameters:

  • name (Symbol, String)

    The field name (converted to Symbol)

  • type (Symbol, String) (defaults to: :text)

    The field type (defaults to :text)

  • label (String, nil) (defaults to: nil)

    Custom label (inferred from name if nil)

  • required (Boolean) (defaults to: false)

    Whether the field is required (default: false)

  • default (Object, nil) (defaults to: nil)

    Default value for the field

  • placeholder (String, nil) (defaults to: nil)

    Placeholder text

  • options (Array, nil) (defaults to: nil)

    Options for select fields



46
47
48
49
50
51
52
53
54
# File 'lib/iron_admin/action_field.rb', line 46

def initialize(name:, type: :text, label: nil, required: false, default: nil, placeholder: nil, options: nil)
  @name = name.to_sym
  @type = TYPES.include?(type.to_sym) ? type.to_sym : :text
  @label = label || name.to_s.humanize
  @required = required
  @default = default
  @placeholder = placeholder
  @options = options
end

Instance Attribute Details

#defaultObject? (readonly)

Returns Default value for the field.

Returns:

  • (Object, nil)

    Default value for the field



31
32
33
# File 'lib/iron_admin/action_field.rb', line 31

def default
  @default
end

#labelString (readonly)

Returns Human-readable label for the field.

Returns:

  • (String)

    Human-readable label for the field



25
26
27
# File 'lib/iron_admin/action_field.rb', line 25

def label
  @label
end

#nameSymbol (readonly)

Returns The field name (used as the param key).

Returns:

  • (Symbol)

    The field name (used as the param key)



19
20
21
# File 'lib/iron_admin/action_field.rb', line 19

def name
  @name
end

#optionsArray? (readonly)

Returns Options for select fields.

Returns:

  • (Array, nil)

    Options for select fields



37
38
39
# File 'lib/iron_admin/action_field.rb', line 37

def options
  @options
end

#placeholderString? (readonly)

Returns Placeholder text for the input.

Returns:

  • (String, nil)

    Placeholder text for the input



34
35
36
# File 'lib/iron_admin/action_field.rb', line 34

def placeholder
  @placeholder
end

#requiredBoolean (readonly)

Returns Whether the field is required.

Returns:

  • (Boolean)

    Whether the field is required



28
29
30
# File 'lib/iron_admin/action_field.rb', line 28

def required
  @required
end

#typeSymbol (readonly)

Returns The field type.

Returns:

  • (Symbol)

    The field type



22
23
24
# File 'lib/iron_admin/action_field.rb', line 22

def type
  @type
end