Class: EasyAdmin::Field

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Field

Returns a new instance of Field.



5
6
7
8
9
10
11
12
13
# File 'lib/easy_admin/field.rb', line 5

def initialize(name, type, options = {})
  @name = name
  @type = type
  @label = options[:label] || name.to_s.humanize
  @editable = options[:editable]
  @options_adapter = options[:options_adapter]
  @permitted_attached_attributes = options[:permitted_attached_attributes] || []
  @attached_param_key = options[:attached_param_key]
end

Instance Attribute Details

#attached_param_keyObject

Returns the value of attribute attached_param_key.



3
4
5
# File 'lib/easy_admin/field.rb', line 3

def attached_param_key
  @attached_param_key
end

#editableObject

Returns the value of attribute editable.



3
4
5
# File 'lib/easy_admin/field.rb', line 3

def editable
  @editable
end

#labelObject

Returns the value of attribute label.



3
4
5
# File 'lib/easy_admin/field.rb', line 3

def label
  @label
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/easy_admin/field.rb', line 3

def name
  @name
end

#options_adapterObject

Returns the value of attribute options_adapter.



3
4
5
# File 'lib/easy_admin/field.rb', line 3

def options_adapter
  @options_adapter
end

#permitted_attached_attributesObject

Returns the value of attribute permitted_attached_attributes.



3
4
5
# File 'lib/easy_admin/field.rb', line 3

def permitted_attached_attributes
  @permitted_attached_attributes
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/easy_admin/field.rb', line 3

def type
  @type
end

Class Method Details

.render(field_type, action:, **options) ⇒ Object



82
83
84
85
# File 'lib/easy_admin/field.rb', line 82

def render(field_type, action:, **options)
  component_class = find_component_class(field_type, action)
  component_class.new(**options)
end

Instance Method Details

#editable?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/easy_admin/field.rb', line 15

def editable?
  editable.present?
end

#editable_via_menu?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/easy_admin/field.rb', line 19

def editable_via_menu?
  type == :belongs_to && editable&.dig(:via) == :menu_or_modal
end

#editable_via_modal?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/easy_admin/field.rb', line 23

def editable_via_modal?
  editable? && !editable_via_menu?
end

#foreign_keyObject



36
37
38
# File 'lib/easy_admin/field.rb', line 36

def foreign_key
  @foreign_key ||= :"#{name}_id"
end

#normalize_input!(attrs) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/easy_admin/field.rb', line 40

def normalize_input!(attrs)
  key = permitted_param_key
  value = attrs[key]

  attrs[key] = case type
  when :boolean
    ActiveModel::Type::Boolean.new.cast(value)
  when :date
    if value.present?
      Date.parse(value) rescue value
    else
      value
    end
  when :datetime
    if value.present?
      Time.zone.parse(value) rescue value
    else
      value
    end
  when :enum
    # Validate against allowed values if needed
    validate_enum_value(value)
  when :json
    # Parse JSON string to ensure valid JSON
    if value.present? && value.is_a?(String)
      begin
        JSON.parse(value)
        value
      rescue JSON::ParserError => e
        raise "Invalid JSON: #{e.message}"
      end
    else
      value
    end
  else
    value # string, email, number, etc.
  end

  attrs
end

#permitted_param_keyObject



27
28
29
30
31
32
33
34
# File 'lib/easy_admin/field.rb', line 27

def permitted_param_key
  case type
  when :belongs_to
    foreign_key || :"#{name}_id"
  else
    name
  end
end