Class: Glib::JsonUi::ViewBuilder::Panels::Form
- Inherits:
-
View
- Object
- JsonUiElement
- View
- Glib::JsonUi::ViewBuilder::Panels::Form
- Defined in:
- app/helpers/glib/json_ui/view_builder/panels.rb
Overview
Form panel for collecting user input and submitting data.
A form panel automatically handles CSRF tokens, form validation, and data submission. It supports nested associations and dynamic field groups.
Instance Attribute Summary collapse
-
#_autoValidate ⇒ Object
readonly
Returns the value of attribute _autoValidate.
-
#current_dynamic_group ⇒ Object
Returns the value of attribute current_dynamic_group.
-
#disable_dirty_check ⇒ Object
readonly
Returns the value of attribute disable_dirty_check.
-
#model_name ⇒ Object
readonly
See Panels::Form.field_name.
Attributes inherited from JsonUiElement
Class Method Summary collapse
- .cast_to_js_regex(format_validation) ⇒ Object
- .field_assert_respond_to(model, prop) ⇒ Object
- .field_label(model, model_name, prop, args) ⇒ Object
- .field_name(model, prop, multiple, page) ⇒ Object
- .field_validation(model, prop) ⇒ Object
- .field_value(model, prop, collect_ids:) ⇒ Object
- .hint_label(model_name, prop, args) ⇒ Object
-
.is_array_association?(model, prop) ⇒ Boolean
TODO: Enable this when we know it won’t break existing apps.
- .is_single_association?(model, prop) ⇒ Boolean
- .lookup_error_message(model_name, attribute_name, key) ⇒ Object
- .placeholder_label(model_name, prop, args) ⇒ Object
Instance Method Summary collapse
- #as(model_name) ⇒ Object
- #autofocus(value) ⇒ Object
- #autoValidate(autoValidate) ⇒ Object
- #cast_to_js_regex(format_validation) ⇒ Object
- #childViews(block) ⇒ Object
-
#created ⇒ Object
Override.
- #disableDirtyCheck(value) ⇒ Object
- #field_assert_respond_to(prop) ⇒ Object
- #field_label(prop, args) ⇒ Object
- #field_name(prop, multiple) ⇒ Object
- #field_validation(prop) ⇒ Object
- #field_value(prop, collect_ids: true) ⇒ Object
- #hint_label(prop, args) ⇒ Object
- #method(method) ⇒ Object
- #model(models) ⇒ Object
- #nested_associations ⇒ Object
- #placeholder_label(prop, args) ⇒ Object
- #url(url) ⇒ Object
Methods inherited from View
Methods inherited from JsonUiElement
Constructor Details
This class inherits a constructor from Glib::JsonUi::JsonUiElement
Instance Attribute Details
#_autoValidate ⇒ Object (readonly)
Returns the value of attribute _autoValidate.
34 35 36 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 34 def _autoValidate @_autoValidate end |
#current_dynamic_group ⇒ Object
Returns the value of attribute current_dynamic_group.
36 37 38 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 36 def current_dynamic_group @current_dynamic_group end |
#disable_dirty_check ⇒ Object (readonly)
Returns the value of attribute disable_dirty_check.
35 36 37 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 35 def disable_dirty_check @disable_dirty_check end |
#model_name ⇒ Object (readonly)
See Panels::Form.field_name
33 34 35 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 33 def model_name @model_name end |
Class Method Details
.cast_to_js_regex(format_validation) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 189 def self.cast_to_js_regex(format_validation) if format_validation[:with].instance_of?(Regexp) format_validation[:with] = JsRegex.new(format_validation[:with]).source end if format_validation[:without].instance_of?(Regexp) format_validation[:without] = JsRegex.new(format_validation[:without]).source end if format_validation[:regex].instance_of?(Regexp) format_validation[:regex] = JsRegex.new(format_validation[:regex]).source end end |
.field_assert_respond_to(model, prop) ⇒ Object
94 95 96 97 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 94 def self.field_assert_respond_to(model, prop) # Identify a prop being used on a nil model or incorrect model. raise "Invalid property `#{prop}` on '#{model.class}'" unless model.respond_to?(prop) end |
.field_label(model, model_name, prop, args) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 146 def self.field_label(model, model_name, prop, args) name = prop.to_s if name.ends_with?('_id') && is_single_association?(model, name.chomp('_id')) name = name.chomp('_id') # Always uses non-ID property name in i18n files end I18n.t("dt_models.#{model_name}.#{name}.label", **args.merge(default: nil)) || I18n.t("activerecord.attributes.#{model_name}.#{name}", **args.merge(default: nil)) || I18n.t("activemodel.attributes.#{model_name}.#{name}", **args) end |
.field_name(model, prop, multiple, page) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 103 def self.field_name(model, prop, multiple, page) form = page.current_form include_form_model = true reversed_model_panels = [] form.nested_associations.reverse.each do |panel| if panel.is_a?(Fields::DynamicGroup) include_form_model = false break # Remove anything before Fields::DynamicGroup end reversed_model_panels << panel end name = include_form_model ? form.model_name : '' reversed_model_panels.reverse.each do |panel| index_exists = !panel.assoc_order_index.nil? field_name = index_exists ? panel.model_name.pluralize : panel.model_name name += "[#{field_name}_attributes]" if index_exists index = panel.assoc_order_index == :auto ? '' : panel.assoc_order_index name += "[#{index}]" end end suffix = is_array_association?(model, prop) || multiple ? '[]' : '' "#{name}[#{prop}]#{suffix}" end |
.field_validation(model, prop) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 201 def self.field_validation(model, prop) validations = {} ignored = ['confirmation', 'comparison', 'uniqueness', 'validates_associated', 'validates_each', 'validates_with'] model.class.validators_on(prop).each do |validator| next if ignored.include?(validator.kind.to_s) validations[validator.kind] = validator..except(:if, :unless) validations[validator.kind][:message] = validator.[:message] case validator.kind when :absence validations[validator.kind][:message] ||= (model.to_s.underscore, prop, 'present') when :presence validations[validator.kind][:message] ||= (model.to_s.underscore, prop, 'blank') when :acceptance validations[validator.kind][:message] ||= (model.to_s.underscore, prop, 'accepted') validations[validator.kind][:accept] ||= ['1', true] when :numericality validations[validator.kind][:message] ||= [ 'not_a_number', 'not_an_integer', 'greater_than', 'greater_than_or_equal_to', 'equal_to', 'less_than', 'less_than_or_equal_to', 'other_than', 'in', 'odd', 'even' ].inject({}) { |prev, curr| prev.merge(curr => (model.to_s.underscore, prop, curr)) } when :format cast_to_js_regex(validations[validator.kind]) validations[validator.kind][:message] ||= (model.to_s.underscore, prop, 'invalid') when :inclusion validations[validator.kind][:message] ||= (model.to_s.underscore, prop, 'inclusion') when :exclusion validations[validator.kind][:message] ||= (model.to_s.underscore, prop, 'exclusion') when :length validations[validator.kind][:message] ||= ['too_long', 'wrong_length', 'too_short'].inject({}) { |prev, curr| prev.merge(curr => (model.to_s.underscore, prop, curr)) } end end if validations[:presence].present? validations[:required] = validations[:presence] validations.delete(:presence) end validations end |
.field_value(model, prop, collect_ids:) ⇒ Object
134 135 136 137 138 139 140 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 134 def self.field_value(model, prop, collect_ids:) if is_array_association?(model, prop) && collect_ids model.send(prop)&.map { |record| record.id } else model.send(prop) end end |
.hint_label(model_name, prop, args) ⇒ Object
160 161 162 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 160 def self.hint_label(model_name, prop, args) I18n.t("dt_models.#{model_name}.#{prop}.hint", **args.merge(default: nil)) end |
.is_array_association?(model, prop) ⇒ Boolean
TODO: Enable this when we know it won’t break existing apps. Even for pure client-side apps, this is required because form.validate() requires a URL to construct form data. required :url
74 75 76 77 78 79 80 81 82 83 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 74 def self.is_array_association?(model, prop) # # Not all model is ActiveRecord # if @model.class.respond_to?(:reflect_on_association) # return @model.class.reflect_on_association(prop).macro # end # false # Not all model is ActiveRecord model.class.try(:reflect_on_association, prop)&.macro == :has_many || model.class.try(:reflect_on_attachment, prop)&.macro == :has_many_attached end |
.is_single_association?(model, prop) ⇒ Boolean
85 86 87 88 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 85 def self.is_single_association?(model, prop) # Not all model is ActiveRecord model.class.try(:reflect_on_association, prop)&.macro == :belongs_to end |
.lookup_error_message(model_name, attribute_name, key) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 180 def self.(model_name, attribute_name, key) = I18n.t("activerecord.errors.models.#{model_name}.attributes.#{attribute_name}.#{key}", default: nil) if model_name.present? && attribute_name.present? ||= I18n.t("activerecord.errors.models.#{model_name}.#{key}", default: nil) if model_name.present? ||= I18n.t("activerecord.errors.messages.#{key}", default: nil) ||= I18n.t("errors.attributes.#{attribute_name}.#{key}", default: nil) if attribute_name.present? ||= I18n.t("errors.messages.#{key}", default: nil) end |
.placeholder_label(model_name, prop, args) ⇒ Object
168 169 170 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 168 def self.placeholder_label(model_name, prop, args) I18n.t("dt_models.#{model_name}.#{prop}.placeholder", **args.merge(default: nil)) end |
Instance Method Details
#as(model_name) ⇒ Object
251 252 253 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 251 def as(model_name) @model_name = model_name end |
#autofocus(value) ⇒ Object
259 260 261 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 259 def autofocus(value) @autofocus = value end |
#autoValidate(autoValidate) ⇒ Object
58 59 60 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 58 def autoValidate(autoValidate) @_autoValidate = autoValidate end |
#cast_to_js_regex(format_validation) ⇒ Object
176 177 178 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 176 def cast_to_js_regex(format_validation) self.class.cast_to_js_regex(format_validation) end |
#childViews(block) ⇒ Object
304 305 306 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 304 def childViews(block) @childViewsBlock = block end |
#created ⇒ Object
Override
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 264 def created if @models @model = @models.is_a?(Array) ? @models.last : @models @model_name ||= @model.class.model_name.singular @url ||= page.context.polymorphic_url(@models) @method ||= if @model.persisted? :patch else :post end end @method = @method&.to_sym || :get json.url @url json.method @method @autofocus = true if @autofocus.nil? json.autofocus @autofocus json.childViews do if @method != :get json.child! do json.view 'fields/hidden' json.name 'authenticity_token' json.value page.context.form_authenticity_token end end if page.current_form raise 'Nested form is not allowed' end # NOTE: this pattern will not work for views that can be nested. Luckily forms shouldn't be nested anyway. page.current_form = self @childViewsBlock.call(page.view_builder) page.current_form = nil end end |
#disableDirtyCheck(value) ⇒ Object
66 67 68 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 66 def disableDirtyCheck(value) @disable_dirty_check = value end |
#field_assert_respond_to(prop) ⇒ Object
90 91 92 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 90 def field_assert_respond_to(prop) self.class.field_assert_respond_to(@model, prop) end |
#field_label(prop, args) ⇒ Object
142 143 144 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 142 def field_label(prop, args) self.class.field_label(@model, @model_name, prop, args) end |
#field_name(prop, multiple) ⇒ Object
99 100 101 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 99 def field_name(prop, multiple) self.class.field_name(@model, prop, multiple, page) end |
#field_validation(prop) ⇒ Object
172 173 174 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 172 def field_validation(prop) self.class.field_validation(@model, prop) end |
#field_value(prop, collect_ids: true) ⇒ Object
130 131 132 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 130 def field_value(prop, collect_ids: true) self.class.field_value(@model, prop, collect_ids: collect_ids) end |
#hint_label(prop, args) ⇒ Object
156 157 158 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 156 def hint_label(prop, args) self.class.hint_label(@model_name, prop, args) end |
#method(method) ⇒ Object
247 248 249 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 247 def method(method) @method = method end |
#model(models) ⇒ Object
255 256 257 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 255 def model(models) @models = models end |
#nested_associations ⇒ Object
62 63 64 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 62 def nested_associations @nested_associations ||= [] end |
#placeholder_label(prop, args) ⇒ Object
164 165 166 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 164 def placeholder_label(prop, args) self.class.placeholder_label(@model_name, prop, args) end |
#url(url) ⇒ Object
243 244 245 |
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 243 def url(url) @url = url end |