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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Component.

Raises:

  • (ArgumentError)


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

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

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



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

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

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

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



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

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

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

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



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

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

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