Class: Glib::JsonUi::ViewBuilder::Panels::Form
- Inherits:
-
View
show all
- Defined in:
- app/helpers/glib/json_ui/view_builder/panels.rb
Instance Attribute Summary
#json, #page
Instance Method Summary
collapse
#initialize, #props
Instance Method Details
#as(model_name) ⇒ Object
71
72
73
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 71
def as(model_name)
@model_name = model_name
end
|
#childViews(block) ⇒ Object
117
118
119
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 117
def childViews(block)
@childViewsBlock = block
end
|
#created ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 80
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
page.current_form = self
@childViewsBlock.call(page.view_builder)
page.current_form = nil
end
end
|
#field_assert_respond_to(prop) ⇒ Object
23
24
25
26
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 23
def field_assert_respond_to(prop)
raise "Invalid property `#{prop}` on '#{@model.class}'" unless @model.respond_to?(prop)
end
|
#field_label(prop, args) ⇒ Object
41
42
43
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 41
def field_label(prop, args)
I18n.t("dt_models.#{@model_name}.#{prop}.label", args.merge(default: nil)) || I18n.t("activerecord.attributes.#{@model_name}.#{prop}", args)
end
|
#field_name(prop, multiple) ⇒ Object
28
29
30
31
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 28
def field_name(prop, multiple)
suffix = is_array_association?(prop) || multiple ? '[]' : ''
"#{@model_name}[#{prop}]#{suffix}"
end
|
#field_validation(prop) ⇒ Object
53
54
55
56
57
58
59
60
61
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 53
def field_validation(prop)
validations = {}
@model.class.validators_on(prop).each do |validator|
if validator.kind == :presence
validations[:required] = { message: 'Required' }
end
end
validations
end
|
#field_value(prop) ⇒ Object
33
34
35
36
37
38
39
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 33
def field_value(prop)
if is_array_association?(prop)
@model.send(prop)&.map { |record| record.id }
else
@model.send(prop)
end
end
|
#hint_label(prop, args) ⇒ Object
45
46
47
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 45
def hint_label(prop, args)
I18n.t("dt_models.#{@model_name}.#{prop}.hint", args.merge(default: nil))
end
|
#is_array_association?(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
12
13
14
15
16
17
18
19
20
21
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 12
def is_array_association?(prop)
@model.class.try(:reflect_on_association, prop)&.macro == :has_many
end
|
#method(method) ⇒ Object
67
68
69
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 67
def method(method)
@method = method
end
|
#model(models) ⇒ Object
75
76
77
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 75
def model(models)
@models = models
end
|
#placeholder_label(prop, args) ⇒ Object
49
50
51
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 49
def placeholder_label(prop, args)
I18n.t("dt_models.#{@model_name}.#{prop}.placeholder", args.merge(default: nil))
end
|
#url(url) ⇒ Object
63
64
65
|
# File 'app/helpers/glib/json_ui/view_builder/panels.rb', line 63
def url(url)
@url = url
end
|