Class: Glib::JsonUi::ViewBuilder::Panels::Form

Inherits:
View show all
Defined in:
app/helpers/glib/json_ui/view_builder/panels.rb

Instance Attribute Summary collapse

Attributes inherited from JsonUiElement

#json, #page

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from View

component_name

Methods inherited from JsonUiElement

#initialize, #props

Constructor Details

This class inherits a constructor from Glib::JsonUi::JsonUiElement

Instance Attribute Details

#_autoValidateObject (readonly)

Returns the value of attribute _autoValidate.



7
8
9
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 7

def _autoValidate
  @_autoValidate
end

#current_dynamic_groupObject

Returns the value of attribute current_dynamic_group.



8
9
10
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 8

def current_dynamic_group
  @current_dynamic_group
end

#model_nameObject (readonly)

See Panels::Form.field_name



6
7
8
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 6

def model_name
  @model_name
end

Class Method Details

.cast_to_js_regex(format_validation) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 143

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



48
49
50
51
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 48

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



100
101
102
103
104
105
106
107
108
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 100

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



57
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
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 57

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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 155

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.options.except(:if, :unless)
    validations[validator.kind][:message] = validator.options[:message]
    case validator.kind
    when :absence
      validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'present')
    when :presence
      validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'blank')
    when :acceptance
      validations[validator.kind][:message] ||= lookup_error_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 => lookup_error_message(model.to_s.underscore, prop, curr)) }
    when :format
      cast_to_js_regex(validations[validator.kind])
      validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'invalid')
    when :inclusion
      validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'inclusion')
    when :exclusion
      validations[validator.kind][:message] ||= lookup_error_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 => lookup_error_message(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



88
89
90
91
92
93
94
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 88

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



114
115
116
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 114

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

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 28

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

Returns:

  • (Boolean)


39
40
41
42
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 39

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



134
135
136
137
138
139
140
141
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 134

def self.lookup_error_message(model_name, attribute_name, key)
  message = I18n.t("activerecord.errors.models.#{model_name}.attributes.#{attribute_name}.#{key}", default: nil) if model_name.present? && attribute_name.present?
  message ||= I18n.t("activerecord.errors.models.#{model_name}.#{key}", default: nil) if model_name.present?
  message ||= I18n.t("activerecord.errors.messages.#{key}", default: nil)
  message ||= I18n.t("errors.attributes.#{attribute_name}.#{key}", default: nil) if attribute_name.present?
  message ||= I18n.t("errors.messages.#{key}", default: nil)
  message
end

.placeholder_label(model_name, prop, args) ⇒ Object



122
123
124
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 122

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



205
206
207
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 205

def as(model_name)
  @model_name = model_name
end

#autoValidate(autoValidate) ⇒ Object



16
17
18
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 16

def autoValidate(autoValidate)
  @_autoValidate = autoValidate
end

#cast_to_js_regex(format_validation) ⇒ Object



130
131
132
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 130

def cast_to_js_regex(format_validation)
  self.class.cast_to_js_regex(format_validation)
end

#childViews(block) ⇒ Object



251
252
253
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 251

def childViews(block)
  @childViewsBlock = block
end

#createdObject

Override



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
242
243
244
245
246
247
248
249
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 214

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

  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

#field_assert_respond_to(prop) ⇒ Object



44
45
46
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 44

def field_assert_respond_to(prop)
  self.class.field_assert_respond_to(@model, prop)
end

#field_label(prop, args) ⇒ Object



96
97
98
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 96

def field_label(prop, args)
  self.class.field_label(@model, @model_name, prop, args)
end

#field_name(prop, multiple) ⇒ Object



53
54
55
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 53

def field_name(prop, multiple)
  self.class.field_name(@model, prop, multiple, page)
end

#field_validation(prop) ⇒ Object



126
127
128
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 126

def field_validation(prop)
  self.class.field_validation(@model, prop)
end

#field_value(prop, collect_ids: true) ⇒ Object



84
85
86
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 84

def field_value(prop, collect_ids: true)
  self.class.field_value(@model, prop, collect_ids: collect_ids)
end

#hint_label(prop, args) ⇒ Object



110
111
112
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 110

def hint_label(prop, args)
  self.class.hint_label(@model_name, prop, args)
end

#method(method) ⇒ Object



201
202
203
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 201

def method(method)
  @method = method
end

#model(models) ⇒ Object



209
210
211
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 209

def model(models)
  @models = models
end

#nested_associationsObject



20
21
22
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 20

def nested_associations
  @nested_associations ||= []
end

#placeholder_label(prop, args) ⇒ Object



118
119
120
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 118

def placeholder_label(prop, args)
  self.class.placeholder_label(@model_name, prop, args)
end

#url(url) ⇒ Object



197
198
199
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 197

def url(url)
  @url = url
end