Class: IronAdmin::Field

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

Overview

Represents a field configuration for a resource.

Fields are typically inferred from the database schema via FieldInferrer, then customized via the Resource.field DSL. Each Field instance holds the configuration for how a column/attribute should be displayed and edited.

Examples:

Field from database schema

# Automatically inferred for a string column
Field.new(:name, type: :string)

Field with visibility control

Field.new(:salary, type: :integer, visible: ->(user) { user.hr? })

Field with multiple options

Field.new(:status, type: :select, options: %w[pending active completed])

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **options) ⇒ Field

Creates a new Field instance.

Examples:

Basic field

Field.new(:email, type: :string)

Conditional visibility

Field.new(:ssn, type: :string, visible: ->(user) { user.admin? })

Select field with options

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

Parameters:

  • name (Symbol)

    The field/column name

  • options (Hash)

    Configuration options

Options Hash (**options):

  • :type (Symbol)

    The field type

  • :visible (Boolean, Proc)

    Whether the field is visible (default: true). If a Proc, receives the current user.

  • :readonly (Boolean, Proc)

    Whether the field is read-only (default: false). If a Proc, receives the current user.

  • :label (String)

    Custom display label

  • :options (Array)

    For select fields, available choices

  • :autocomplete (Boolean)

    For belongs_to, force autocomplete mode

  • :format (Proc)

    Custom formatting proc for display



61
62
63
64
65
66
67
# File 'lib/iron_admin/field.rb', line 61

def initialize(name, **options)
  @name = name
  @type = options.delete(:type)
  @visible = options.delete(:visible) { true }
  @readonly = options.delete(:readonly) { false }
  @options = options
end

Instance Attribute Details

#nameSymbol (readonly)

Returns The field/column name.

Returns:

  • (Symbol)

    The field/column name



24
25
26
# File 'lib/iron_admin/field.rb', line 24

def name
  @name
end

#optionsHash (readonly)

Returns Additional field options (e.g., :options, :format, :autocomplete).

Returns:

  • (Hash)

    Additional field options (e.g., :options, :format, :autocomplete)



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

def options
  @options
end

#readonlyBoolean, Proc (readonly)

Returns Whether the field is read-only.

Returns:

  • (Boolean, Proc)

    Whether the field is read-only



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

def readonly
  @readonly
end

#typeSymbol (readonly)

Returns The field type (:string, :text, :integer, :boolean, :date, :datetime, :select, :belongs_to, etc.).

Returns:

  • (Symbol)

    The field type (:string, :text, :integer, :boolean, :date, :datetime, :select, :belongs_to, etc.)



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

def type
  @type
end

#visibleBoolean, Proc (readonly)

Returns Whether the field is visible.

Returns:

  • (Boolean, Proc)

    Whether the field is visible



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

def visible
  @visible
end

Instance Method Details

#readonly?(user) ⇒ Boolean

Checks if the field is read-only for the given user.

Examples:

field.readonly?(current_user)  #=> false

Parameters:

  • user (Object)

    The current user object

Returns:

  • (Boolean)

    True if the field should be non-editable



87
88
89
# File 'lib/iron_admin/field.rb', line 87

def readonly?(user)
  evaluate(@readonly, user)
end

#visible?(user) ⇒ Boolean

Checks if the field is visible for the given user.

Examples:

field.visible?(current_user)  #=> true

Parameters:

  • user (Object)

    The current user object

Returns:

  • (Boolean)

    True if the field should be displayed



76
77
78
# File 'lib/iron_admin/field.rb', line 76

def visible?(user)
  evaluate(@visible, user)
end