Class: EasyAdmin::Field
- Inherits:
-
Object
- Object
- EasyAdmin::Field
- Defined in:
- lib/easy_admin/field.rb
Instance Attribute Summary collapse
-
#attached_param_key ⇒ Object
Returns the value of attribute attached_param_key.
-
#editable ⇒ Object
Returns the value of attribute editable.
-
#label ⇒ Object
Returns the value of attribute label.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options_adapter ⇒ Object
Returns the value of attribute options_adapter.
-
#permitted_attached_attributes ⇒ Object
Returns the value of attribute permitted_attached_attributes.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #editable? ⇒ Boolean
- #editable_via_menu? ⇒ Boolean
- #editable_via_modal? ⇒ Boolean
- #foreign_key ⇒ Object
-
#initialize(name, type, options = {}) ⇒ Field
constructor
A new instance of Field.
- #normalize_input!(attrs) ⇒ Object
- #permitted_param_key ⇒ Object
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, = {}) @name = name @type = type @label = [:label] || name.to_s.humanize @editable = [:editable] @options_adapter = [:options_adapter] @permitted_attached_attributes = [:permitted_attached_attributes] || [] @attached_param_key = [:attached_param_key] end |
Instance Attribute Details
#attached_param_key ⇒ Object
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 |
#editable ⇒ Object
Returns the value of attribute editable.
3 4 5 |
# File 'lib/easy_admin/field.rb', line 3 def editable @editable end |
#label ⇒ Object
Returns the value of attribute label.
3 4 5 |
# File 'lib/easy_admin/field.rb', line 3 def label @label end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/easy_admin/field.rb', line 3 def name @name end |
#options_adapter ⇒ Object
Returns the value of attribute options_adapter.
3 4 5 |
# File 'lib/easy_admin/field.rb', line 3 def @options_adapter end |
#permitted_attached_attributes ⇒ Object
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 |
#type ⇒ Object
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:, **) component_class = find_component_class(field_type, action) component_class.new(**) end |
Instance Method Details
#editable? ⇒ Boolean
15 16 17 |
# File 'lib/easy_admin/field.rb', line 15 def editable? editable.present? end |
#editable_via_menu? ⇒ Boolean
19 20 21 |
# File 'lib/easy_admin/field.rb', line 19 def type == :belongs_to && editable&.dig(:via) == :menu_or_modal end |
#editable_via_modal? ⇒ Boolean
23 24 25 |
# File 'lib/easy_admin/field.rb', line 23 def editable_via_modal? editable? && ! end |
#foreign_key ⇒ Object
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.}" end else value end else value # string, email, number, etc. end attrs end |
#permitted_param_key ⇒ Object
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 |