Class: ModelFormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- ModelFormBuilder
- Includes:
- Pxs::Forms::LinksHelper, Pxs::Forms::ModelsHelper
- Defined in:
- lib/pxs/forms/model_form_builder.rb
Instance Attribute Summary collapse
-
#input_id_prefix ⇒ Object
readonly
Returns the value of attribute input_id_prefix.
-
#input_name_prefix ⇒ Object
readonly
Returns the value of attribute input_name_prefix.
Instance Method Summary collapse
- #associated_model_subform(association) ⇒ Object
-
#association_select_or_create_subform(association, options = {}) ⇒ Object
if the object is associated to other models, they can have their own subsection of a form the subsection typically includes: * a selection input for the submodel: usually a Select or Radio Buttons for single associations, select or checkboxes or custom components for multiple for now this is kept outside of this form builder, could be added here * an “add [model]” button in case the user can add the given model and the intended model does not exist yet * a space for the submodel’s form, which is displayed only as necessary.
-
#cancel_form_button(association, label = nil) ⇒ Object
cancel subform button.
- #check_boxes_input(attribute, options = {}) ⇒ Object
- #field(attribute, options = {}) ⇒ Object
-
#initialize(object_name, object, template, options, input_name_prefix = nil, input_id_prefix = nil) ⇒ ModelFormBuilder
constructor
A new instance of ModelFormBuilder.
- #radio_buttons_input(attribute, options = {}) ⇒ Object
-
#range_input(min_attribute, max_attribute, options = {}) ⇒ Object
range inputs, by definition, accept two attributes, both assumed to be numerical.
- #set_association_prefixes(name, id = "") ⇒ Object
- #slider_input(attribute, options = {}) ⇒ Object
Methods included from Pxs::Forms::LinksHelper
#link_button_to, #turbo_link_button, #turbo_link_to
Methods included from Pxs::Forms::ModelsHelper
#input_name_to_id, #input_name_to_select_id, #model_association_form, #model_button_id, #model_cancel_form_button, #model_delete_action, #model_edit_action, #model_edit_delete_actions, #model_form_for, #model_form_id, #model_form_with, #model_html_id, #model_new_action, #model_string, #model_submit_button, #model_title_id
Constructor Details
#initialize(object_name, object, template, options, input_name_prefix = nil, input_id_prefix = nil) ⇒ ModelFormBuilder
Returns a new instance of ModelFormBuilder.
10 11 12 13 14 15 16 |
# File 'lib/pxs/forms/model_form_builder.rb', line 10 def initialize(object_name, object, template, , input_name_prefix = nil, input_id_prefix = nil) # if input name isn't specified (which by default it isn't in the Rails vanilla form_with/for), set it from the object name @input_name_prefix = input_name_prefix || object_name.to_s.underscore @input_id_prefix = input_id_prefix || object_name.to_s.underscore.kebabcase super(object_name, object, template, ) end |
Instance Attribute Details
#input_id_prefix ⇒ Object (readonly)
Returns the value of attribute input_id_prefix.
8 9 10 |
# File 'lib/pxs/forms/model_form_builder.rb', line 8 def input_id_prefix @input_id_prefix end |
#input_name_prefix ⇒ Object (readonly)
Returns the value of attribute input_name_prefix.
8 9 10 |
# File 'lib/pxs/forms/model_form_builder.rb', line 8 def input_name_prefix @input_name_prefix end |
Instance Method Details
#associated_model_subform(association) ⇒ Object
94 95 96 97 98 |
# File 'lib/pxs/forms/model_form_builder.rb', line 94 def associated_model_subform association fields_for association do |association_form| @template.render("#{association_class(association).model_name.collection}/fields", form: association_form) end end |
#association_select_or_create_subform(association, options = {}) ⇒ Object
if the object is associated to other models, they can have their own subsection of a form the subsection typically includes:
* a selection input for the submodel:
usually a Select or Radio Buttons for single associations, select or checkboxes or custom components for multiple
for now this is kept outside of this form builder, could be added here
* an "add [model]" in case the user can add the given model and the intended model does not exist yet
* a space for the submodel's form, which is displayed only as necessary
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pxs/forms/model_form_builder.rb', line 58 def association_select_or_create_subform(association, = {}) # assert that the options hash passed contains only valid keys .assert_valid_keys(:add, :select, :collection, :title) # merge with the default options = (association).merge() # use the provided collection, if any collection = if [:collection].present? then [:collection] # otherwise use Association.for_select, if that method is provided elsif [:association_class].methods.include? :for_select [:association_class].for_select # otherwise use Association.all else [:association_class].all end # the container for the input and whatever subform might appear @template.tag.div id: model_html_id(association), class: "form-section" do safe_join [ # subform title, acting as the field label @template.tag.h3([:title] || association.to_s.humanize, id: model_html_id(association, :title)), # a select input, to choose an existing model field("#{association.to_s}_id", collection: collection, text_method: [:select][:text_method], value_method: [:select][:value_method], label: [:select][:label]), # an empty "client-form" ID div, to be filled with the subform fields once expanded @template.tag.div(id: model_html_id(association, :form)), # an "Add Client" button, to expand the subform when clicked in order to allow the user to create a new submodel alongside the original model @template.link_to([:add][:label], [:add][:path], class: "button#{options[:add][:class] ? " #{options[:add][:class]}" : ""}", id: (association, :new), data: { turbo_stream: true }), # A `cancel` button to close the subform, initially hidden (association) ] end end |
#cancel_form_button(association, label = nil) ⇒ Object
cancel subform button
101 102 103 |
# File 'lib/pxs/forms/model_form_builder.rb', line 101 def association, label = nil @template.link_to(label ? label.to_.humanize : "Use Existing", new_associated_model_path(association, name: @input_name_prefix, args: {cancel: :true}), class: "button hidden", id: "cancel-#{association_class(association).model_name.singular.kebabcase}-button", data: {turbo_stream: true}) end |
#check_boxes_input(attribute, options = {}) ⇒ Object
47 48 49 |
# File 'lib/pxs/forms/model_form_builder.rb', line 47 def check_boxes_input(attribute, = {}) collection_of(:check_boxes, attribute, ) end |
#field(attribute, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pxs/forms/model_form_builder.rb', line 23 def field(attribute, = {}) = object_type = object_type_for_attribute(attribute) input_type = case object_type when :date then :string when :integer then :string when :decimal then :string else object_type end override_input_type = if [:as] [:as] elsif [:collection] :select end send("#{override_input_type || input_type}_input", attribute, ) end |
#radio_buttons_input(attribute, options = {}) ⇒ Object
43 44 45 |
# File 'lib/pxs/forms/model_form_builder.rb', line 43 def (attribute, = {}) collection_of(:radio_buttons, attribute, ) end |
#range_input(min_attribute, max_attribute, options = {}) ⇒ Object
range inputs, by definition, accept two attributes, both assumed to be numerical
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/pxs/forms/model_form_builder.rb', line 115 def range_input(min_attribute, max_attribute, = {}) # default to lower boundary #? worth forcing an error by requiring this parameter, instead of stashing in options; same for the min/max options, which are hard to give default values to attribute_name = ([:attribute_name] || min_attribute).to_s field_block(attribute_name, ) do safe_join [ (field_label(attribute_name, [:label]) unless [:label] == false), tag.input(type: :range, id: "#{base_input_name}-#{attribute_name.kebabcase}", name: attribute_name.kebabcase, min: [:min] || 0, max: [:max] || 10, step: [:step] || 1, value: [:value], list: [:datalist_id]) ] end end |
#set_association_prefixes(name, id = "") ⇒ Object
18 19 20 21 |
# File 'lib/pxs/forms/model_form_builder.rb', line 18 def set_association_prefixes(name, id = "") @input_name_prefix = name @input_id_prefix = id end |
#slider_input(attribute, options = {}) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/pxs/forms/model_form_builder.rb', line 105 def (attribute, = {}) field_block(attribute, ) do safe_join [ (field_label(attribute, [:label]) unless [:label] == false), tag.input(type: :range, id: "#{base_input_name}-#{attribute.to_s.kebabcase}", name: attribute.to_s.kebabcase, min: [:min] || 0, max: [:max] || 10, step: [:step] || 1, value: [:value], list: [:datalist_id]) ] end end |