Class: SolidusAdmin::UI::Forms::Field::Component

Inherits:
BaseComponent
  • Object
show all
Extended by:
ComponentsHelper
Defined in:
app/components/solidus_admin/ui/forms/field/component.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ComponentsHelper

component

Constructor Details

#initialize(label:, hint: nil, tip: nil, error: nil, input_attributes: nil, **attributes) ⇒ Component

Returns a new instance of Component.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
# File 'app/components/solidus_admin/ui/forms/field/component.rb', line 6

def initialize(label:, hint: nil, tip: nil, error: nil, input_attributes: nil, **attributes)
  @label = label
  @hint = hint
  @tip = tip
  @error = [error] if error.present?
  @attributes = attributes
  @input_attributes = input_attributes

  raise ArgumentError, "provide either a block or input_attributes" if content? && input_attributes
end

Class Method Details

.extract_form_details(form, object, method) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/components/solidus_admin/ui/forms/field/component.rb', line 94

def self.extract_form_details(form, object, method)
  if form.is_a?(String)
    object_name = form
    raise ArgumentError, "Object must be provided when form name is a string" unless object
  elsif form.respond_to?(:object)
    object_name = form.object_name
    object = form.object
  else
    raise ArgumentError, "Invalid arguments: expected a form object or form.object_name and form.object"
  end

  errors = object.errors.messages_for(method).presence if object.respond_to?(:errors)
  label = object.class.human_attribute_name(method)

  [object_name, object, label, errors]
end

.select(form, method, choices, object: nil, hint: nil, tip: nil, size: :m, **attributes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/components/solidus_admin/ui/forms/field/component.rb', line 36

def self.select(form, method, choices, object: nil, hint: nil, tip: nil, size: :m, **attributes)
  object_name, object, label, errors = extract_form_details(form, object, method)

  new(
    label:,
    hint:,
    tip:,
    error: errors,
    input_attributes: {
      name: "#{object_name}[#{method}]",
      tag: :select,
      choices:,
      size:,
      value: object.public_send(method),
      error: (errors.to_sentence.capitalize if errors),
      **attributes,
    }
  )
end

.text_area(form, method, object: nil, hint: nil, tip: nil, size: :m, **attributes) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/components/solidus_admin/ui/forms/field/component.rb', line 56

def self.text_area(form, method, object: nil, hint: nil, tip: nil, size: :m, **attributes)
  object_name, object, label, errors = extract_form_details(form, object, method)

  new(
    label:,
    hint:,
    tip:,
    error: errors,
    input_attributes: {
      name: "#{object_name}[#{method}]",
      size:,
      tag: :textarea,
      value: object.public_send(method),
      error: (errors.to_sentence.capitalize if errors),
      **attributes,
    }
  )
end

.text_field(form, method, object: nil, hint: nil, tip: nil, size: :m, **attributes) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/components/solidus_admin/ui/forms/field/component.rb', line 17

def self.text_field(form, method, object: nil, hint: nil, tip: nil, size: :m, **attributes)
  object_name, object, label, errors = extract_form_details(form, object, method)

  new(
    label:,
    hint:,
    tip:,
    error: errors,
    input_attributes: {
      name: "#{object_name}[#{method}]",
      tag: :input,
      size:,
      value: object.public_send(method),
      error: (errors.to_sentence.capitalize if errors),
      **attributes,
    }
  )
end

.toggle(form, method, object: nil, hint: nil, tip: nil, size: :m, **attributes) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/components/solidus_admin/ui/forms/field/component.rb', line 75

def self.toggle(form, method, object: nil, hint: nil, tip: nil, size: :m, **attributes)
  object_name, object, label, errors = extract_form_details(form, object, method)

  new(
    label:,
    hint:,
    tip:,
    error: errors,
  ).with_content(
    component('ui/forms/switch').new(
      name: "#{object_name}[#{method}]",
      size:,
      checked: object.public_send(method),
      include_hidden: true,
      **attributes,
    )
  )
end