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

#current_dynamic_groupObject

Returns the value of attribute current_dynamic_group.



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

def current_dynamic_group
  @current_dynamic_group
end

#model_nameObject (readonly)

See Panels::Form.field_name



4
5
6
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 4

def model_name
  @model_name
end

Class Method Details

.field_assert_respond_to(model, prop) ⇒ Object



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

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



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

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)
end

.field_name(model, prop, multiple, page) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 50

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



121
122
123
124
125
126
127
128
129
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 121

def self.field_validation(model, prop)
  validations = {}
  model.class.validators_on(prop).each do |validator|
    if validator.kind == :presence
      validations[:required] = { message: 'Required' }
    end
  end
  validations
end

.field_value(model, prop) ⇒ Object



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

def self.field_value(model, prop)
  if is_array_association?(model, prop)
    model.send(prop)&.map { |record| record.id }
  else
    model.send(prop)
  end
end

.hint_label(model_name, prop, args) ⇒ Object



105
106
107
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 105

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)


19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 19

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
end

.is_single_association?(model, prop) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 30

def self.is_single_association?(model, prop)
  # Not all model is ActiveRecord
  model.class.try(:reflect_on_association, prop)&.macro == :belongs_to
end

.placeholder_label(model_name, prop, args) ⇒ Object



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

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



139
140
141
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 139

def as(model_name)
  @model_name = model_name
end

#childViews(block) ⇒ Object



185
186
187
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 185

def childViews(block)
  @childViewsBlock = block
end

#createdObject

Override



148
149
150
151
152
153
154
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
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 148

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



35
36
37
38
39
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 35

def field_assert_respond_to(prop)
  self.class.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(prop, args) ⇒ Object



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

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

#field_name(prop, multiple) ⇒ Object



46
47
48
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 46

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

#field_validation(prop) ⇒ Object



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

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

#field_value(prop) ⇒ Object



77
78
79
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 77

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

#hint_label(prop, args) ⇒ Object



101
102
103
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 101

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

#method(method) ⇒ Object



135
136
137
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 135

def method(method)
  @method = method
end

#model(models) ⇒ Object



143
144
145
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 143

def model(models)
  @models = models
end

#nested_associationsObject



11
12
13
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 11

def nested_associations
  @nested_associations ||= []
end

#placeholder_label(prop, args) ⇒ Object



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

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

#url(url) ⇒ Object



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

def url(url)
  @url = url
end