glib-web 4.1.4 → 4.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d36f47f5f0236550243e1344d2bdee89559141b549575142afbd1ea5a13464d1
4
- data.tar.gz: c772a7b58eb06904c442d220a7eae112eaeb5416774a8db28623caa770ec95e5
3
+ metadata.gz: 74c8034dd7dd38d81a358b836e90332f2cd1b9445a3a765de052141ee9c26aaf
4
+ data.tar.gz: 31f47b9867591f3459ddf8fbd14ca8e8ef7227c89ee9cea4edde81a7cefd710d
5
5
  SHA512:
6
- metadata.gz: 176d31f10a75945b693d0e0f26497d19a95ee63863d9cf585004cfe4b9011c64d5908ee622b3a0a83dd01b3a7d4c8babee9cd84af5c2ec3315aaac8e860dc536
7
- data.tar.gz: e75efdc5c2b79a11c380d3905cb2d8d495c74cb01c88264dc0b362c0393007037d3bb814af0bb70ec0c82ce6779db51ca9ecd2be2f7ebb79a2880636645508ae
6
+ metadata.gz: f39ee60b1d47d4b630940c039735f90cd729635d4cc8eeb93b0a1ede965dd52546d274748b9037fa0beee9129537a8282386fb424000730728b5c2023657a9c1
7
+ data.tar.gz: 9423dbc97f08201803f4bbf2f91fdaf5505124f402aa2d718405d29940824ab38bffbe09aeabf293c6ff657d42837564be463d5ffd828b70b180fe6581861cb9
@@ -66,7 +66,7 @@ class Glib::JsonUi::ViewBuilder
66
66
  @label ||= context.field_label(@prop, @label_args || {})
67
67
  @hint ||= context.hint_label(@prop, @hint_args || {})
68
68
  @placeholder ||= context.placeholder_label(@prop, @placeholder_args || {})
69
- @validation ||= context.field_validation(@prop)
69
+ @validation ||= context.field_validation(@prop) if @autoValidate
70
70
 
71
71
  if form.current_dynamic_group.nil?
72
72
  # This is not relevant inside a dynamic group
@@ -83,7 +83,7 @@ class Glib::JsonUi::ViewBuilder
83
83
  # - Placeholder competes with label for space
84
84
  json.placeholder @placeholder if @placeholder
85
85
 
86
- json.validation @validation if @autoValidate
86
+ json.validation @validation if @validation.present?
87
87
  end
88
88
 
89
89
  # To be overridden
@@ -118,35 +118,45 @@ class Glib::JsonUi::ViewBuilder
118
118
  self.class.field_validation(@model, prop)
119
119
  end
120
120
 
121
+ def self.lookup_error_message(model_name, attribute_name, key)
122
+ message = I18n.t("activerecord.errors.models.#{model_name}.attributes.#{attribute_name}.#{key}", default: nil) if model_name.present? && attribute_name.present?
123
+ message ||= I18n.t("activerecord.errors.models.#{model_name}.#{key}", default: nil) if model_name.present?
124
+ message ||= I18n.t("activerecord.errors.messages.#{key}", default: nil)
125
+ message ||= I18n.t("errors.attributes.#{attribute_name}.#{key}", default: nil) if attribute_name.present?
126
+ message ||= I18n.t("errors.messages.#{key}", default: nil)
127
+ message
128
+ end
129
+
121
130
  def self.field_validation(model, prop)
122
131
  validations = {}
132
+ ignored = ['confirmation', 'comparison', 'uniqueness', 'validates_associated', 'validates_each', 'validates_with', 'format']
123
133
  model.class.validators_on(prop).each do |validator|
134
+ next if ignored.include?(validator.kind.to_s)
135
+
124
136
  validations[validator.kind] = validator.options.except(:if, :unless)
125
137
  validations[validator.kind][:message] = validator.options[:message]
126
138
  case validator.kind
139
+ when :absence
140
+ validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'present')
127
141
  when :presence
128
- validations[validator.kind][:message] ||= I18n.t('errors.messages.present')
142
+ validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'blank')
129
143
  when :acceptance
130
- validations[validator.kind][:message] ||= I18n.t('errors.messages.accepted')
131
- when :confirmation
132
- # raise NotImplemented
144
+ validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'accepted')
145
+ validations[validator.kind][:accept] ||= ['1', true]
133
146
  when :numericality
134
- # raise NotImplemented
135
- when :comparison
136
- comparison = validator.options.first[0]
137
- validations[validator.kind][:message] ||= I18n.t("errors.messages.#{comparison}")
147
+ validations[validator.kind][:message] ||= [
148
+ 'not_a_number', 'not_an_integer', 'greater_than',
149
+ 'greater_than_or_equal_to', 'equal_to', 'less_than',
150
+ 'less_than_or_equal_to', 'other_than', 'in', 'odd', 'even'
151
+ ].inject({}) { |prev, curr| prev.merge(curr => lookup_error_message(model.to_s.underscore, prop, curr)) }
138
152
  when :format
139
- validations[validator.kind][:message] ||= I18n.t('errors.messages.invalid')
153
+ validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'invalid')
140
154
  when :inclusion
141
- validations[validator.kind][:message] ||= I18n.t('errors.messages.inclusion')
155
+ validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'inclusion')
142
156
  when :exclusion
143
- validations[validator.kind][:message] ||= I18n.t('errors.messages.exclusion')
157
+ validations[validator.kind][:message] ||= lookup_error_message(model.to_s.underscore, prop, 'exclusion')
144
158
  when :length
145
- validations[validator.kind][:message] = {
146
- too_long: I18n.t('errors.messages.too_long'),
147
- wrong_length: I18n.t('errors.messages.wrong_length'),
148
- too_short: I18n.t('errors.messages.too_short')
149
- }
159
+ 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)) }
150
160
  end
151
161
  end
152
162
 
@@ -0,0 +1,16 @@
1
+ module Glib
2
+ class DummyJobApplication
3
+ include ActiveModel::Validations
4
+ include ActiveModel::AttributeMethods
5
+
6
+ attr_accessor :name, :age, :position, :words, :accept
7
+
8
+ validates :name, :position, presence: true
9
+ validates :accept, acceptance: {}, allow_nil: false
10
+ validates :words, length: { maximum: 100, minimum: 1 }
11
+ validates :age, numericality: { only_integer: true, less_than_or_equal_to: 30, greater_than_or_equal_to: 18 }, allow_blank: true
12
+ # validates :name, format: { with: /Doe/, message: 'This job is for person named Doe' }
13
+ validates :position, inclusion: { in: ['programmer', 'devops', 'designer'] }, allow_blank: true
14
+
15
+ end
16
+ end
@@ -4,15 +4,17 @@ page = json_ui_page json
4
4
  render "#{@path_prefix}/nav_menu", json: json, page: page
5
5
 
6
6
  page.form \
7
+ model: Glib::DummyJobApplication.new,
7
8
  url: json_ui_garage_url(path: 'forms/generic_post'),
8
9
  method: 'post',
9
10
  padding: glib_json_padding_body,
10
11
  childViews: ->(form) do
11
12
  form.fields_text \
13
+ prop: :name,
12
14
  name: 'user[name]',
13
15
  width: 'matchParent',
14
16
  label: 'Name',
15
- validation: { required: { message: 'Required' }, format: { regex: 'Doe$', message: 'The name has to end with "Doe"' } }
17
+ validation: { format: { regex: 'Doe', message: 'This job is only for person named Doe' } }
16
18
 
17
19
  form.fields_email \
18
20
  name: 'user[email]',
@@ -25,12 +27,11 @@ page.form \
25
27
  label: 'URL'
26
28
 
27
29
  form.fields_number \
28
- name: 'user[number]',
30
+ prop: :age,
31
+ name: 'user[age]',
29
32
  width: 'matchParent',
30
- label: 'Number',
31
- validation: { required: { message: 'Required' } },
32
- leftText: 'USD',
33
- rightText: '.00'
33
+ label: 'Age',
34
+ autoValidate: true
34
35
 
35
36
  form.fields_phone \
36
37
  name: 'user[phone1]',
@@ -52,32 +53,21 @@ page.form \
52
53
  leftIcon: 'lock'
53
54
 
54
55
  form.fields_textarea \
55
- name: 'user[textarea]',
56
+ prop: :words,
57
+ name: 'user[words]',
56
58
  width: 'matchParent',
57
59
  label: 'Textarea with maxLength',
58
- maxLength: 50,
59
- validation: { required: { message: 'Required' } }
60
-
61
- # options = ['male', 'female'].map { |i| { text: i.humanize, value: i } }
62
- # form.fields_select \
63
- # name: 'user[gender]',
64
- # width: 'matchParent',
65
- # label: 'Gender',
66
- # validation: { required: { message: 'Required' } },
67
- # options: options
60
+ maxLength: 1000,
61
+ autoValidate: true
68
62
 
69
- languages = {
70
- 'brisbane' => 'Brisbane',
71
- 'canberra' => 'Canberra',
72
- 'melbourne' => 'Melbourne',
73
- 'sydney' => 'Sydney',
74
- }
63
+ options = ['programmer', 'devops', 'designer', 'ceo', 'office_boy'].map { |v| { text: v.humanize, value: v } }
75
64
  form.fields_select \
76
- name: 'user[city]',
65
+ prop: :position,
66
+ name: 'user[position]',
77
67
  width: 'matchParent',
78
- label: 'City',
79
- options: languages.map { |k, v| { value: k, text: v } },
80
- validation: { required: { message: 'Required' } }
68
+ label: 'Position',
69
+ options: options,
70
+ autoValidate: true
81
71
 
82
72
  form.spacer height: 10
83
73
  form.h4 text: 'Gender'
@@ -104,6 +94,9 @@ page.form \
104
94
  group.fields_check checkValue: 4, label: 'Mobile Development'
105
95
  end
106
96
 
97
+ form.spacer height: 10
98
+ form.fields_check prop: :accept, label: 'Accept terms & condition', name: 'user[accept]', autoValidate: true
99
+
107
100
  form.spacer height: 30
108
101
  form.fields_submit text: 'Submit'
109
102
  form.fields_submit text: 'Submit (disable if form invalid)', disableIfFormInvalid: true
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: 4.1.4
4
+ version: 4.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
@@ -134,6 +134,7 @@ files:
134
134
  - app/models/glib/active_storage/attachment.rb
135
135
  - app/models/glib/active_storage/blob.rb
136
136
  - app/models/glib/application_record.rb
137
+ - app/models/glib/dummy_job_application.rb
137
138
  - app/models/glib/dynamic_text_record.rb
138
139
  - app/models/glib/text.rb
139
140
  - app/policies/glib/application_policy.rb