Class: Pakyow::Presenter::Presenters::Form
- Inherits:
-
Pakyow::Presenter::Presenter
- Object
- Pakyow::Presenter::Presenter
- Pakyow::Presenter::Presenters::Form
- Includes:
- Support::SafeStringHelpers
- Defined in:
- lib/pakyow/presenter/presenters/form.rb
Constant Summary collapse
- SUPPORTED_ACTIONS =
%i(create update replace delete).freeze
- ACTION_METHODS =
{ create: "post", update: "patch", replace: "put", delete: "delete" }.freeze
- ID_LABEL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
:__form_id
Instance Attribute Summary
Attributes inherited from Pakyow::Presenter::Presenter
#app, #logger, #presentables, #view
Class Method Summary collapse
- .connect_input_to_label(input, label) ⇒ Object private
- .pluralize_field_name(field) ⇒ Object private
Instance Method Summary collapse
-
#action=(action) ⇒ Object
Sets the form action (where it submits to).
-
#create(object = {}) {|_self| ... } ⇒ Object
Setup the form for creating an object.
-
#delete(object) {|_self| ... } ⇒ Object
Setup the form for removing an object.
-
#grouped_options_for(field, options = nil) ⇒ Object
Populates a select field with grouped options.
- #id ⇒ Object
-
#method=(method) ⇒ Object
Sets the form method.
-
#method_missing(method_name, *args, &block) ⇒ Object
Delegate private methods.
- #object_for_form ⇒ Object
-
#options_for(field, options = nil) ⇒ Object
Populates a select field with options.
-
#pp(*args) ⇒ Object
Fixes an issue using pp inside a delegator.
-
#replace(object) {|_self| ... } ⇒ Object
Setup the form for replacing an object.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #setup(object = {}) ⇒ Object
- #setup? ⇒ Boolean
-
#update(object) {|_self| ... } ⇒ Object
Setup the form for updating an object.
Methods inherited from Pakyow::Presenter::Presenter
#==, #after, #append, attach, #before, #bind, #clear, #component, #components, #endpoint, #endpoint_action, #find, #find_all, #form, #forms, #initialize, make, options_for, #prepend, #present, present, #presenter_for, #remove, render, #title, #title=, #to_html, #to_s, #transform, #use, #use_implicit_version, version, #versioned, #with, #wrap_data_in_binder
Constructor Details
This class inherits a constructor from Pakyow::Presenter::Presenter
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Delegate private methods.
188 189 190 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 188 def method_missing(method_name, *args, &block) __getobj__.send(method_name, *args, &block) end |
Class Method Details
.connect_input_to_label(input, label) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 25 def connect_input_to_label(input, label) if false || input.attributes[:id].to_s.empty? id = SecureRandom.hex(4) input.attributes[:id] = id else id = input.attributes[:id] end label.attributes[:for] = id end |
.pluralize_field_name(field) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
18 19 20 21 22 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 18 def pluralize_field_name(field) unless field.attributes[:name].to_s.end_with?("[]") || field.attributes[:name].to_s.empty? field.attributes[:name] = "#{field.attributes[:name]}[]" end end |
Instance Method Details
#action=(action) ⇒ Object
Sets the form action (where it submits to).
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 59 def action=(action) if action.is_a?(Symbol) if endpoint = app.endpoints.find(name: action) view.object.set_label(:endpoint, action) view.object.set_label(:endpoint_object, endpoint) view.object.set_label(:endpoint_params, {}) Endpoint.new(__getobj__).setup end else attrs[:action] = action end end |
#create(object = {}) {|_self| ... } ⇒ Object
Setup the form for creating an object.
154 155 156 157 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 154 def create(object = {}) yield self if block_given? setup_form_for_binding(:create, object) end |
#delete(object) {|_self| ... } ⇒ Object
Setup the form for removing an object.
175 176 177 178 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 175 def delete(object) yield self if block_given? setup_form_for_binding(:delete, object) end |
#grouped_options_for(field, options = nil) ⇒ Object
Populates a select field with grouped options.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 115 def (field, = nil) unless field_presenter = find(field) raise ArgumentError.new("could not find field named `#{field}'") end unless (field_presenter) raise ArgumentError.new("expected `#{field}' to be a select field") end = || yield case field_presenter.object.tagname when "select" (, field_presenter) end end |
#id ⇒ Object
53 54 55 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 53 def id label(ID_LABEL) end |
#method=(method) ⇒ Object
Sets the form method. Automatically handles method overrides by prepending a hidden field named ‘pw-http-method` when method
is not get or post, setting the form method to post
.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 75 def method=(method) method = method.to_s.downcase if method_override_required?(method) attrs[:method] = "post" find_or_create_method_override_input.attributes[:value] = method else attrs[:method] = method end end |
#object_for_form ⇒ Object
47 48 49 50 51 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 47 def object_for_form if labeled?(:binding) presentables[channeled_binding_name] end end |
#options_for(field, options = nil) ⇒ Object
Populates a select field with options.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 88 def (field, = nil) unless field_presenter = find(Support.inflector.singularize(field)) || find(Support.inflector.pluralize(field)) raise ArgumentError.new("could not find field named `#{field}'") end unless (field_presenter) raise ArgumentError.new("expected `#{field}' to be a select field, checkbox, radio button, or binding") end = if block_given? yield(field_presenter) else end case field_presenter.object.tagname when "select" (, field_presenter) when "input" (, field_presenter) else (, field_presenter) end end |
#pp(*args) ⇒ Object
Fixes an issue using pp inside a delegator.
182 183 184 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 182 def pp(*args) Kernel.pp(*args) end |
#replace(object) {|_self| ... } ⇒ Object
Setup the form for replacing an object.
168 169 170 171 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 168 def replace(object) yield self if block_given? setup_form_for_binding(:replace, object) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
192 193 194 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 192 def respond_to_missing?(method_name, include_private = false) super || __getobj__.respond_to?(method_name, true) end |
#setup(object = {}) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 131 def setup(object = {}) use_binding_nodes if block_given? yield self end bind(object) setup! self end |
#setup? ⇒ Boolean
144 145 146 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 144 def setup? view.object.labeled?(:__form_setup) end |
#update(object) {|_self| ... } ⇒ Object
Setup the form for updating an object.
161 162 163 164 |
# File 'lib/pakyow/presenter/presenters/form.rb', line 161 def update(object) yield self if block_given? setup_form_for_binding(:update, object) end |