Module: FlexiAdmin::Components::Resource::FormMixin

Includes:
Helpers::ResourceHelper
Included in:
FormComponent, FlexiAdmin::Components::Resources::BulkAction::ModalComponent
Defined in:
lib/flexi_admin/components/resource/form_mixin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ResourceHelper

#autocomplete_path, #bulk_action_path, #datalist_path, #edit_resource_path, #paginate, #resource__path, #resource_input_name, #resource_path, #resources_path, #scope, #scope_plural, #scope_singular

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



28
29
30
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 28

def disabled
  @disabled
end

#inlineObject (readonly)

Returns the value of attribute inline.



28
29
30
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 28

def inline
  @inline
end

#parent_resourceObject (readonly)

Returns the value of attribute parent_resource.



28
29
30
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 28

def parent_resource
  @parent_resource
end

#resourceObject (readonly)

Returns the value of attribute resource.



28
29
30
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 28

def resource
  @resource
end

Instance Method Details

#button_select_field(attr_name, options:, label: nil, value: nil, **html_options) ⇒ Object



46
47
48
49
50
51
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 46

def button_select_field(attr_name, options:, label: nil, value: nil, **html_options)
  field = render_button_select(attr_name, options:, value:, disabled:, **html_options)
  field_wrapper = render_field_wrapper(field, attr_name)

  inline ? field_wrapper : render_form_row(attr_name, field_wrapper, label:)
end

#checkbox_field(attr_name, label: nil, checked: nil, **html_options) ⇒ Object



68
69
70
71
72
73
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 68

def checkbox_field(attr_name, label: nil, checked: nil, **html_options)
  checkbox = checkbox_field_tag(attr_name, checked:, **html_options)
  field_wrapper = render_field_wrapper(checkbox, attr_name)

  inline ? field_wrapper : render_form_row(attr_name, field_wrapper, label:)
end

#checkbox_field_tag(attr_name, checked: nil, **html_options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 75

def checkbox_field_tag(attr_name, checked: nil, **html_options)
  val = checked.is_a?(Proc) ? checked.call : resource.try(attr_name) || checked
  hidden__field = hidden_field(attr_name, value: 0)

  options = {
    type: 'checkbox',
    name: attr_name,
    class: 'form-check-input',
    value: 1,
    checked: val,
    disabled:
  }.merge(html_options)

  checkbox = (:input, nil, options)

  hidden__field + checkbox
end

#custom_field(view_component_instance, label: nil, **html_options, &block) ⇒ Object



127
128
129
130
131
132
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 127

def custom_field(view_component_instance, label: nil, **html_options, &block)
  label ||= view_component_instance.class.name.demodulize.humanize unless label == false
  value = block_given? ? capture(&block) : view_component_instance

  render_custom_field(view_component_instance, label, html_options, value)
end

#date_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object



98
99
100
101
102
103
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 98

def date_field(attr_name, label: nil, value: nil, **html_options)
  field = render_standard_field(:date, attr_name, value, html_options.merge(style: 'max-width: 180px;'))
  field_wrapper = render_field_wrapper(field, attr_name)

  inline ? field_wrapper : render_form_row(attr_name, field_wrapper, label:)
end

#datetime_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object



105
106
107
108
109
110
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 105

def datetime_field(attr_name, label: nil, value: nil, **html_options)
  field = render_standard_field(:datetime, attr_name, value, html_options.merge(style: 'max-width: 180px;'))
  field_wrapper = render_field_wrapper(field, attr_name)

  inline ? field_wrapper : render_form_row(attr_name, field_wrapper, label:)
end

#form(url: resource_path(resource), css_class: 'myForm section', method: :patch, **html_options, &block) ⇒ Object

Form does not render; fields do.



31
32
33
34
35
36
37
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 31

def form(url: resource_path(resource), css_class: 'myForm section', method: :patch, **html_options, &block)
  render FlexiAdmin::Components::Resource::FormElementComponent.new(resource, url:, css_class:, method:, **html_options) do |component|
    component.with_fields do
      capture(&block)
    end
  end
end

#header(label, description: nil) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 117

def header(label, description: nil)
  header_and_description = []
  header_and_description << (:div, class: 'header') { label }
  header_and_description << (:div, class: 'description') { description } if description.present?

  (:div, class: 'form-row d-flex flex-column gap-2') do
    concat (:div, class: 'col-12') { header_and_description.join.html_safe }
  end
end

#hidden_field(attr_name, value: nil, **html_options) ⇒ Object



93
94
95
96
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 93

def hidden_field(attr_name, value: nil, **html_options)
  html_options = { type: 'hidden', name: attr_name, value: }.merge(html_options)
  (:input, nil, html_options)
end

#html_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object

Trix field



113
114
115
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 113

def html_field(attr_name, label: nil, value: nil, **html_options)
  custom_field(FlexiAdmin::Components::Shared::TrixComponent.new(attr_name:, value:, disabled:), label:, **html_options)
end

#number_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object



64
65
66
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 64

def number_field(attr_name, label: nil, value: nil, **html_options)
  render_standard_field(:number, attr_name, value, html_options)
end

#select_field(attr_name, options:, label: nil, value: nil, **html_options) ⇒ Object



39
40
41
42
43
44
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 39

def select_field(attr_name, options:, label: nil, value: nil, **html_options)
  field = render_select(attr_name, value:, options:, **html_options)
  field_wrapper = render_field_wrapper(field, attr_name)

  inline ? field_wrapper : render_form_row(attr_name, field_wrapper, label:)
end

#submit(label = 'Uložit', cancel_button: true, cancel_button_url: nil, icon: nil, classes: '') ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 134

def submit(label = 'Uložit', cancel_button: true, cancel_button_url: nil, icon: nil, classes: '')
  submit_button = if icon.present?
    helpers.(:button, class: 'btn btn-primary ' + classes, disabled: disabled) do
      content = []
      content << helpers.(:i, '', class: "bi bi-#{icon} me-2")
      content << label
      content.join.html_safe
    end
  else
    helpers.submit_tag(label, class: 'btn btn-primary ' + classes, disabled:)
  end

  if cancel_button
    cancel_btn_path = cancel_button_url || edit_resource_path(resource, fa_form_disabled: true)
    cancel_btn = (:button, 'Zrušit', class: 'btn btn-outline-secondary',
                                                disabled:,
                                                data: { controller: 'form',
                                                        action: 'click->form#disable',
                                                        'form-resource-path-value': cancel_btn_path })
  end

  (:div, class: 'form-row d-flex gap-3') do
    concat submit_button
    concat cancel_btn if cancel_button
  end
end

#text_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object



53
54
55
56
57
58
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 53

def text_field(attr_name, label: nil, value: nil, **html_options)
  field = text_field_tag(attr_name, value:, **html_options)
  field_wrapper = render_field_wrapper(field, attr_name)

  inline ? field_wrapper : render_form_row(attr_name, field_wrapper, label:)
end

#text_field_tag(attr_name, value: nil, **html_options) ⇒ Object



60
61
62
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 60

def text_field_tag(attr_name, value: nil, **html_options)
  render_standard_field(:text, attr_name, value, html_options)
end

#with_resource(resource) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/flexi_admin/components/resource/form_mixin.rb', line 161

def with_resource(resource)
  previous_resource = @resource
  @resource = resource
  yield
ensure
  @resource = previous_resource
end