glib-web 3.8.2 → 3.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adcc5121f8959025d9937146a154b771c1b5792019223b5845f5a3445a9b7e03
4
- data.tar.gz: e05846e84a368a702bb2d391436e69c94397432b3192946ddb723639ead60592
3
+ metadata.gz: 017c468688e16caca324d9eb790e5392cdcf220b5f259cfd926f7d05a0f2823f
4
+ data.tar.gz: 473404c0d90bf386e723e1c62f65456b4e0375932c144063d1c07526dad3e3f2
5
5
  SHA512:
6
- metadata.gz: 27c4de7b3dd80a009e9178511cd921884d217289b3a6c64b9980ac5c353d4a90be58eb727e59e067315b7d66ec74c4509741d6fdffcc3c9b5ef17bf63bdb9378
7
- data.tar.gz: 31c82c4e57b0449a52fd2327b3c8f8645ab8a5b2a0c72dcf72b76076f20c8531eab684bf9052d13e899869c78379682d84af6a27d4d5d95217e9c4b9c3b8c7ea
6
+ metadata.gz: e35b713b672704816511274ee46282209d190b067c4ae012cb4b0a00cf5d443d00345384f487e7afdb33526df82178bf621deaef3a04b001653ca772b651e028
7
+ data.tar.gz: 224571d4d294707c2f9b1967f8a444e0718b333a616923d62c69a808b9852111b870a1d9d30f9036ad2e950cf7fb0cde45acdad91eb592b643c1c7c2e77d90b3
@@ -48,15 +48,20 @@ class Glib::JsonUi::ViewBuilder
48
48
  # Override
49
49
  def created
50
50
  if @prop && (form = page.current_form)
51
- form.field_assert_respond_to(@prop)
51
+ dynamic_group = form.current_dynamic_group
52
+ context = dynamic_group || form
52
53
 
53
- @name ||= form.field_name(@prop, @multiple || false)
54
- @value ||= form.field_value(@prop)
55
- @label ||= form.field_label(@prop, @label_args || {})
56
- @hint ||= form.hint_label(@prop, @hint_args || {})
57
- @placeholder ||= form.placeholder_label(@prop, @placeholder_args || {})
54
+ context.field_assert_respond_to(@prop)
58
55
 
59
- @validation ||= form.field_validation(@prop)
56
+ @name ||= context.field_name(@prop, @multiple || false)
57
+ @label ||= context.field_label(@prop, @label_args || {})
58
+ @hint ||= context.hint_label(@prop, @hint_args || {})
59
+ @placeholder ||= context.placeholder_label(@prop, @placeholder_args || {})
60
+
61
+ if dynamic_group.nil?
62
+ @value ||= form.field_value(@prop)
63
+ @validation ||= form.field_validation(@prop)
64
+ end
60
65
  end
61
66
  json.name @name
62
67
  json.value @value if @value
@@ -197,16 +202,51 @@ class Glib::JsonUi::ViewBuilder
197
202
 
198
203
  class DynamicGroup < AbstractField
199
204
  string :titlePrefix
200
- panels_builder :content, :template
205
+ # panels_builder :content, :template
201
206
  hash :groupFieldProperties
202
207
 
203
208
  # NOTE: Consider using sub-panel instead (e.g. groupTemplate)
204
209
  # views :groupTemplateViews
210
+
211
+ attr_writer :model
212
+
213
+ def model(model)
214
+ @model = model
215
+ @model_name = @model.class.model_name.singular
216
+ end
217
+
218
+ def field_name(prop, multiple)
219
+ prop
220
+ end
221
+
222
+ def field_label(prop, args)
223
+ @delegate_class.field_label(@model, @model_name, prop, args)
224
+ end
225
+
226
+ def hint_label(prop, args)
227
+ @delegate_class.hint_label(@model_name, prop, args)
228
+ end
229
+
230
+ def placeholder_label(prop, args)
231
+ @delegate_class.placeholder_label(@model_name, prop, args)
232
+ end
233
+
234
+ def field_assert_respond_to(prop)
235
+ raise "Please specify a model for #{self.class.component_name} before using its property" unless @model
236
+ @delegate_class.field_assert_respond_to(@model, prop)
237
+ end
238
+
239
+ def content(value)
240
+ @delegate_class = Glib::JsonUi::ViewBuilder::Panels::Form
241
+ form = page.current_form
242
+
243
+ form.current_dynamic_group = self
244
+ value.call(page.content_builder([:template]))
245
+ form.current_dynamic_group = nil
246
+ end
205
247
  end
206
248
 
207
249
  class RadioGroup < AbstractField
208
- # string :name
209
- # string :value
210
250
  views :childViews
211
251
  bool :row
212
252
 
@@ -1,6 +1,8 @@
1
1
  class Glib::JsonUi::ViewBuilder
2
2
  module Panels
3
3
  class Form < View
4
+ attr_accessor :current_dynamic_group
5
+
4
6
  action :onSubmit
5
7
  string :paramNameForFormData
6
8
  bool :local
@@ -9,7 +11,7 @@ class Glib::JsonUi::ViewBuilder
9
11
  # Even for pure client-side apps, this is required because form.validate() requires a URL to construct form data.
10
12
  # required :url
11
13
 
12
- def is_array_association?(prop)
14
+ def self.is_array_association?(model, prop)
13
15
  # # Not all model is ActiveRecord
14
16
  # if @model.class.respond_to?(:reflect_on_association)
15
17
  # return @model.class.reflect_on_association(prop).macro
@@ -17,21 +19,36 @@ class Glib::JsonUi::ViewBuilder
17
19
  # false
18
20
 
19
21
  # Not all model is ActiveRecord
20
- @model.class.try(:reflect_on_association, prop)&.macro == :has_many
22
+ model.class.try(:reflect_on_association, prop)&.macro == :has_many
23
+ end
24
+
25
+ def self.is_single_association?(model, prop)
26
+ # Not all model is ActiveRecord
27
+ model.class.try(:reflect_on_association, prop)&.macro == :belongs_to
21
28
  end
22
29
 
23
30
  def field_assert_respond_to(prop)
31
+ self.class.field_assert_respond_to(@model, prop)
32
+ # # Identify a prop being used on a nil model or incorrect model.
33
+ # raise "Invalid property `#{prop}` on '#{@model.class}'" unless @model.respond_to?(prop)
34
+ end
35
+
36
+ def self.field_assert_respond_to(model, prop)
24
37
  # Identify a prop being used on a nil model or incorrect model.
25
- raise "Invalid property `#{prop}` on '#{@model.class}'" unless @model.respond_to?(prop)
38
+ raise "Invalid property `#{prop}` on '#{model.class}'" unless model.respond_to?(prop)
26
39
  end
27
40
 
28
41
  def field_name(prop, multiple)
29
- suffix = is_array_association?(prop) || multiple ? '[]' : ''
30
- "#{@model_name}[#{prop}]#{suffix}"
42
+ self.class.field_name(@model, @model_name, prop, multiple)
43
+ end
44
+
45
+ def self.field_name(model, model_name, prop, multiple)
46
+ suffix = is_array_association?(model, prop) || multiple ? '[]' : ''
47
+ "#{model_name}[#{prop}]#{suffix}"
31
48
  end
32
49
 
33
50
  def field_value(prop)
34
- if is_array_association?(prop)
51
+ if self.class.is_array_association?(@model, prop)
35
52
  @model.send(prop)&.map { |record| record.id }
36
53
  else
37
54
  @model.send(prop)
@@ -39,15 +56,31 @@ class Glib::JsonUi::ViewBuilder
39
56
  end
40
57
 
41
58
  def field_label(prop, args)
42
- I18n.t("dt_models.#{@model_name}.#{prop}.label", **args.merge(default: nil)) || I18n.t("activerecord.attributes.#{@model_name}.#{prop}", **args)
59
+ self.class.field_label(@model, @model_name, prop, args)
60
+ end
61
+
62
+ def self.field_label(model, model_name, prop, args)
63
+ name = prop.to_s
64
+ if name.ends_with?('_id') && is_single_association?(model, name.chomp('_id'))
65
+ name = name.chomp('_id') # Always uses non-ID property name in i18n files
66
+ end
67
+ I18n.t("dt_models.#{model_name}.#{name}.label", **args.merge(default: nil)) || I18n.t("activerecord.attributes.#{model_name}.#{name}", **args)
43
68
  end
44
69
 
45
70
  def hint_label(prop, args)
46
- I18n.t("dt_models.#{@model_name}.#{prop}.hint", **args.merge(default: nil))
71
+ self.class.hint_label(@model_name, prop, args)
72
+ end
73
+
74
+ def self.hint_label(model_name, prop, args)
75
+ I18n.t("dt_models.#{model_name}.#{prop}.hint", **args.merge(default: nil))
47
76
  end
48
77
 
49
78
  def placeholder_label(prop, args)
50
- I18n.t("dt_models.#{@model_name}.#{prop}.placeholder", **args.merge(default: nil))
79
+ self.class.placeholder_label(@model_name, prop, args)
80
+ end
81
+
82
+ def self.placeholder_label(model_name, prop, args)
83
+ I18n.t("dt_models.#{model_name}.#{prop}.placeholder", **args.merge(default: nil))
51
84
  end
52
85
 
53
86
  def field_validation(prop)
@@ -47,6 +47,9 @@ module Glib
47
47
  # hash :tooltip
48
48
  array :extensions
49
49
 
50
+ def self.component_name
51
+ @component_name ||= self.name.sub('Glib::JsonUi::ViewBuilder::', '')
52
+ end
50
53
 
51
54
  # def initialize(json, page)
52
55
  # super(json, page)
@@ -19,7 +19,12 @@ module Glib
19
19
  module ClassMethods
20
20
  def glib_enum_humanize(enum_name, enum_value, default_value = nil)
21
21
  if enum_value
22
- I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{enum_value}", default: default_value)
22
+ if default_value.nil? && Rails.env.development?
23
+ i18n_key = "activerecord.attributes.#{model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{enum_value}"
24
+ I18n.t(i18n_key, raise: I18n::MissingTranslationData)
25
+ else
26
+ I18n.t(i18n_key, default: default_value)
27
+ end
23
28
  end
24
29
  end
25
30
  end
@@ -15,7 +15,7 @@ page.form url: json_ui_garage_url(path: 'forms/generic_post'), method: 'post', p
15
15
  [
16
16
  { name: 'question', value: 'Quality of work' },
17
17
  { name: 'type', value: 'rating' },
18
- { name: 'enabled', value: '1', styleClasses: ['success'] },
18
+ { name: 'enabled', value: '1' },
19
19
  ],
20
20
  [
21
21
  { name: 'question', value: 'Satisfied?' },
@@ -26,9 +26,21 @@ page.form url: json_ui_garage_url(path: 'forms/generic_post'), method: 'post', p
26
26
  group.template padding: { left: 32 }, childViews: ->(template) do
27
27
  template.spacer height: 10
28
28
  template.fields_text width: 'matchParent', name: 'question', label: 'Question', placeholder: 'Question'
29
+
29
30
  options = [ :rating, :yes_no ]
30
- template.fields_select width: 'matchParent', name: 'type', label: 'Answer Type', placeholder: 'Answer Type', options: options.map { |o| { text: o.to_s.humanize, value: o } }
31
- template.fields_check width: 'matchParent', name: 'enabled', label: 'Enable', checkValue: '1'
31
+ template.fields_select \
32
+ width: 'matchParent',
33
+ name: 'type',
34
+ label: 'Answer Type',
35
+ placeholder: 'Answer Type',
36
+ options: options.map { |o| { text: o.to_s.humanize, value: o } }
37
+
38
+ template.fields_check \
39
+ width: 'matchParent',
40
+ name: 'enabled',
41
+ label: 'Enable',
42
+ checkValue: '1',
43
+ showIf: { "==": [{ "var": 'user[evaluation][{{index}}][type]' }, 'rating'] }
32
44
 
33
45
  template.spacer height: 14
34
46
  end
@@ -99,7 +99,13 @@ page.form url: json_ui_garage_url(path: 'forms/generic_post'), method: 'post', p
99
99
 
100
100
  form.spacer height: 20
101
101
  form.h1 text: 'Select'
102
- options = ['', 'show', 'hide']
102
+ # options = ['', 'show', 'hide']
103
+ options = {
104
+ '' => '<EMPTY>',
105
+ 'show' => 'Show',
106
+ 'hide' => 'Hide',
107
+ nil => '<NULL>'
108
+ }.map { |k, v| { value: k, text: v } }
103
109
  form.fields_select name: 'user[select1]', width: 'matchParent', label: 'Select "show"', options: options, value: ''
104
110
  form.label text: 'Selected', showIf: {
105
111
  "==": [
@@ -117,6 +123,22 @@ page.form url: json_ui_garage_url(path: 'forms/generic_post'), method: 'post', p
117
123
  ''
118
124
  ]
119
125
  }
126
+ form.label text: 'Null', showIf: {
127
+ "==": [
128
+ {
129
+ "var": 'user[select1]'
130
+ },
131
+ nil
132
+ ]
133
+ }
134
+ form.label text: 'Any', showIf: {
135
+ "!=": [
136
+ {
137
+ "var": 'user[select1]'
138
+ },
139
+ nil
140
+ ]
141
+ }
120
142
 
121
143
  form.spacer height: 20
122
144
  form.h1 text: 'Combined conditions'
@@ -7,11 +7,6 @@ render "#{@path_prefix}/nav_menu", json: json, page: page
7
7
  small_image_url = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSGQpSWjtELISLBlmugOZ6wzl1JamYXQvbFeYywpfg3E8b8DrO0Kg&s'
8
8
 
9
9
  page.scroll padding: glib_json_padding_body, childViews: ->(scroll) do
10
- scroll.panels_column lg: { cols: 3 }, md: { cols: 1 }, sm: { cols: 1 }, xs: { cols: 1 }
11
- scroll.panels_column backgroundColor: '#333333', height: 'matchParent', lg: { cols: 3 }, xs: { cols: 4 }, childViews: ->(column) do
12
- column.image width: 'matchParent', url: glib_json_image_standard_url
13
- end
14
-
15
10
  scroll.h2 text: 'Avatar'
16
11
  scroll.spacer height: 6
17
12
  scroll.avatar \
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glib-web
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.8.2
4
+ version: 3.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''