Module: FlexiAdmin::Components::Resource::FormMixin
- Includes:
- Helpers::ResourceHelper
- Defined in:
- lib/flexi_admin/components/resource/form_mixin.rb
Instance Attribute Summary collapse
-
#disabled ⇒ Object
readonly
Returns the value of attribute disabled.
-
#inline ⇒ Object
readonly
Returns the value of attribute inline.
-
#parent_resource ⇒ Object
readonly
Returns the value of attribute parent_resource.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #button_select_field(attr_name, options:, label: nil, value: nil, **html_options) ⇒ Object
- #checkbox_field(attr_name, label: nil, checked: nil, **html_options) ⇒ Object
- #checkbox_field_tag(attr_name, checked: nil, **html_options) ⇒ Object
- #custom_field(view_component_instance, label: nil, **html_options, &block) ⇒ Object
- #date_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object
- #datetime_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object
-
#form(url: resource_path(resource), css_class: 'myForm section', method: :patch, **html_options, &block) ⇒ Object
Form does not render; fields do.
- #header(label, description: nil) ⇒ Object
- #hidden_field(attr_name, value: nil, **html_options) ⇒ Object
-
#html_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object
Trix field.
- #number_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object
- #select_field(attr_name, options:, label: nil, value: nil, **html_options) ⇒ Object
- #submit(label = 'Uložit', cancel_button: true, cancel_button_url: nil, icon: nil, classes: '') ⇒ Object
- #text_field(attr_name, label: nil, value: nil, **html_options) ⇒ Object
- #text_field_tag(attr_name, value: nil, **html_options) ⇒ Object
- #with_resource(resource) ⇒ Object
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
#disabled ⇒ Object (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 |
#inline ⇒ Object (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_resource ⇒ Object (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 |
#resource ⇒ Object (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 (attr_name, options:, label: nil, value: nil, **) field = (attr_name, options:, value:, disabled:, **) 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, **) checkbox = checkbox_field_tag(attr_name, checked:, **) 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, **) val = checked.is_a?(Proc) ? checked.call : resource.try(attr_name) || checked hidden__field = hidden_field(attr_name, value: 0) = { type: 'checkbox', name: attr_name, class: 'form-check-input', value: 1, checked: val, disabled: }.merge() checkbox = content_tag(:input, nil, ) 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, **, &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, , 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, **) field = render_standard_field(:date, attr_name, value, .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, **) field = render_standard_field(:datetime, attr_name, value, .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, **, &block) render FlexiAdmin::Components::Resource::FormElementComponent.new(resource, url:, css_class:, method:, **) 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 << content_tag(:div, class: 'header') { label } header_and_description << content_tag(:div, class: 'description') { description } if description.present? content_tag(:div, class: 'form-row d-flex flex-column gap-2') do concat content_tag(: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, **) = { type: 'hidden', name: attr_name, value: }.merge() content_tag(:input, nil, ) 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, **) custom_field(FlexiAdmin::Components::Shared::TrixComponent.new(attr_name:, value:, disabled:), label:, **) 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, **) render_standard_field(:number, attr_name, value, ) 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, **) field = render_select(attr_name, value:, 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: '') = if icon.present? helpers.content_tag(:button, class: 'btn btn-primary ' + classes, disabled: disabled) do content = [] content << helpers.content_tag(: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_btn_path = || edit_resource_path(resource, fa_form_disabled: true) cancel_btn = content_tag(:button, 'Zrušit', class: 'btn btn-outline-secondary', disabled:, data: { controller: 'form', action: 'click->form#disable', 'form-resource-path-value': cancel_btn_path }) end content_tag(:div, class: 'form-row d-flex gap-3') do concat concat cancel_btn if 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, **) field = text_field_tag(attr_name, value:, **) 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, **) render_standard_field(:text, attr_name, value, ) 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 |