phlexi-form 0.3.0.rc1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -8
  3. data/gemfiles/default.gemfile.lock +34 -32
  4. data/gemfiles/rails_7.gemfile.lock +16 -18
  5. data/lib/phlexi/form/base.rb +42 -41
  6. data/lib/phlexi/form/builder.rb +297 -0
  7. data/lib/phlexi/form/components/base.rb +3 -3
  8. data/lib/phlexi/form/components/collection_checkboxes.rb +1 -1
  9. data/lib/phlexi/form/components/collection_radio_buttons.rb +1 -1
  10. data/lib/phlexi/form/components/concerns/extracts_input.rb +53 -0
  11. data/lib/phlexi/form/components/concerns/handles_input.rb +4 -34
  12. data/lib/phlexi/form/components/concerns/submits_form.rb +8 -0
  13. data/lib/phlexi/form/components/error.rb +1 -1
  14. data/lib/phlexi/form/components/file_input.rb +1 -0
  15. data/lib/phlexi/form/components/hint.rb +1 -1
  16. data/lib/phlexi/form/components/input.rb +16 -6
  17. data/lib/phlexi/form/components/input_array.rb +1 -1
  18. data/lib/phlexi/form/components/select.rb +4 -3
  19. data/lib/phlexi/form/components/textarea.rb +2 -3
  20. data/lib/phlexi/form/html.rb +18 -0
  21. data/lib/phlexi/form/{field_options → options}/autofocus.rb +1 -1
  22. data/lib/phlexi/form/{field_options → options}/collection.rb +14 -10
  23. data/lib/phlexi/form/{field_options → options}/disabled.rb +1 -1
  24. data/lib/phlexi/form/{field_options → options}/errors.rb +18 -14
  25. data/lib/phlexi/form/options/hints.rb +13 -0
  26. data/lib/phlexi/form/options/inferred_types.rb +32 -0
  27. data/lib/phlexi/form/{field_options → options}/length.rb +3 -3
  28. data/lib/phlexi/form/{field_options → options}/limit.rb +2 -2
  29. data/lib/phlexi/form/options/max.rb +55 -0
  30. data/lib/phlexi/form/options/min.rb +55 -0
  31. data/lib/phlexi/form/{field_options → options}/pattern.rb +2 -2
  32. data/lib/phlexi/form/{field_options → options}/readonly.rb +1 -1
  33. data/lib/phlexi/form/{field_options → options}/required.rb +3 -3
  34. data/lib/phlexi/form/options/step.rb +39 -0
  35. data/lib/phlexi/form/options/validators.rb +24 -0
  36. data/lib/phlexi/form/structure/field_collection.rb +12 -27
  37. data/lib/phlexi/form/structure/namespace.rb +4 -111
  38. data/lib/phlexi/form/structure/namespace_collection.rb +1 -32
  39. data/lib/phlexi/form/theme.rb +160 -0
  40. data/lib/phlexi/form/version.rb +1 -1
  41. data/lib/phlexi/form.rb +3 -6
  42. metadata +36 -24
  43. data/lib/phlexi/form/field_options/associations.rb +0 -21
  44. data/lib/phlexi/form/field_options/hints.rb +0 -22
  45. data/lib/phlexi/form/field_options/inferred_types.rb +0 -155
  46. data/lib/phlexi/form/field_options/labels.rb +0 -28
  47. data/lib/phlexi/form/field_options/min_max.rb +0 -92
  48. data/lib/phlexi/form/field_options/multiple.rb +0 -65
  49. data/lib/phlexi/form/field_options/placeholder.rb +0 -18
  50. data/lib/phlexi/form/field_options/themes.rb +0 -207
  51. data/lib/phlexi/form/field_options/validators.rb +0 -48
  52. data/lib/phlexi/form/structure/dom.rb +0 -62
  53. data/lib/phlexi/form/structure/field_builder.rb +0 -236
  54. data/lib/phlexi/form/structure/node.rb +0 -18
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f0a5677bd380aa749f9de4f25243f6c7367839c66a3bfe3f59dfae0f1b32ab6
4
- data.tar.gz: 9107d443cb28c47c1c0a113af182af3b2b6c0a386451e913447a3b834676bbbe
3
+ metadata.gz: 275c0339b1d6b87503e8acf312fe8fa7f4dddeb2cc8a54840b3adfa66d3b61a6
4
+ data.tar.gz: b7f6fe52cfb50974dfec86e3c45ffb22a4ea2a0c1a65cb28d947ed325b0f6965
5
5
  SHA512:
6
- metadata.gz: 0bedfa08fae8602d5cfc632d4907fb12a41c3ac7a64deaad4cc977c82fb3451f976c37e404e449388f6f88127921fe235cec2f42ffedeadc118865c341f924d8
7
- data.tar.gz: db75b28ad2166e117054c2f5eccd5e73e0efcfc7de330d17b02e46468cf46d0aa80934ab31fbd273910c9c80a66b1fdff4184ccf88dd3cbb814b758f36023c38
6
+ metadata.gz: d55f3d6c19be8cab8fe158085730b81bd199c0c4ec4912209b8ec3273b4d5e312412036f534330da6f357adcdff0bd327686736fcba3a078d60270e904c8da26
7
+ data.tar.gz: 547e96dc75e481b0a645ff2156a92f6ff9c3629af5658b5612649df713f81c024bc44b91f53e053db35728d0420572fc831bc6786a0b90bb6e3244768d6c343f
data/README.md CHANGED
@@ -159,14 +159,16 @@ Phlexi::Form supports theming through a flexible theming system:
159
159
 
160
160
  ```ruby
161
161
  class ThemedForm < Phlexi::Form::Base
162
- private
163
-
164
- def default_theme
165
- {
166
- input: "border rounded px-2 py-1",
167
- label: "font-bold text-gray-700",
168
- # Add more theme options here
169
- }
162
+ class Builder < Builder
163
+ private
164
+
165
+ def default_theme
166
+ {
167
+ input: "border rounded px-2 py-1",
168
+ label: "font-bold text-gray-700",
169
+ # Add more theme options here
170
+ }
171
+ end
170
172
  end
171
173
  end
172
174
  ```
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- phlexi-form (0.2.0)
4
+ phlexi-form (0.3.1)
5
5
  activesupport
6
6
  phlex (~> 1.11)
7
7
  zeitwerk
@@ -9,32 +9,34 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actionpack (7.1.3.4)
13
- actionview (= 7.1.3.4)
14
- activesupport (= 7.1.3.4)
12
+ actionpack (7.2.1)
13
+ actionview (= 7.2.1)
14
+ activesupport (= 7.2.1)
15
15
  nokogiri (>= 1.8.5)
16
16
  racc
17
- rack (>= 2.2.4)
17
+ rack (>= 2.2.4, < 3.2)
18
18
  rack-session (>= 1.0.1)
19
19
  rack-test (>= 0.6.3)
20
20
  rails-dom-testing (~> 2.2)
21
21
  rails-html-sanitizer (~> 1.6)
22
- actionview (7.1.3.4)
23
- activesupport (= 7.1.3.4)
22
+ useragent (~> 0.16)
23
+ actionview (7.2.1)
24
+ activesupport (= 7.2.1)
24
25
  builder (~> 3.1)
25
26
  erubi (~> 1.11)
26
27
  rails-dom-testing (~> 2.2)
27
28
  rails-html-sanitizer (~> 1.6)
28
- activesupport (7.1.3.4)
29
+ activesupport (7.2.1)
29
30
  base64
30
31
  bigdecimal
31
- concurrent-ruby (~> 1.0, >= 1.0.2)
32
+ concurrent-ruby (~> 1.0, >= 1.3.1)
32
33
  connection_pool (>= 2.2.5)
33
34
  drb
34
35
  i18n (>= 1.6, < 2)
36
+ logger (>= 1.4.2)
35
37
  minitest (>= 5.1)
36
- mutex_m
37
- tzinfo (~> 2.0)
38
+ securerandom (>= 0.3)
39
+ tzinfo (~> 2.0, >= 2.0.5)
38
40
  addressable (2.8.7)
39
41
  public_suffix (>= 2.0.2, < 7.0)
40
42
  ansi (1.5.0)
@@ -48,7 +50,7 @@ GEM
48
50
  builder (3.3.0)
49
51
  bundle-audit (0.1.0)
50
52
  bundler-audit
51
- bundler-audit (0.9.1)
53
+ bundler-audit (0.9.2)
52
54
  bundler (>= 1.2.0, < 3)
53
55
  thor (~> 1.0)
54
56
  capybara (3.40.0)
@@ -64,7 +66,7 @@ GEM
64
66
  activesupport (>= 3.0.0)
65
67
  railties (>= 3.0.0)
66
68
  thor (>= 0.14.6)
67
- concurrent-ruby (1.3.3)
69
+ concurrent-ruby (1.3.4)
68
70
  connection_pool (2.4.1)
69
71
  crass (1.0.6)
70
72
  drb (2.2.1)
@@ -78,22 +80,22 @@ GEM
78
80
  json (2.7.2)
79
81
  language_server-protocol (3.17.0.3)
80
82
  lint_roller (1.1.0)
83
+ logger (1.6.1)
81
84
  loofah (2.22.0)
82
85
  crass (~> 1.0.2)
83
86
  nokogiri (>= 1.12.0)
84
87
  matrix (0.4.2)
85
88
  mini_mime (1.1.5)
86
- minitest (5.24.1)
89
+ minitest (5.25.1)
87
90
  minitest-reporters (1.7.1)
88
91
  ansi
89
92
  builder
90
93
  minitest (>= 5.0)
91
94
  ruby-progressbar
92
- mutex_m (0.2.0)
93
95
  nokogiri (1.16.7-x86_64-darwin)
94
96
  racc (~> 1.4)
95
- parallel (1.25.1)
96
- parser (3.3.4.0)
97
+ parallel (1.26.3)
98
+ parser (3.3.5.0)
97
99
  ast (~> 2.4.1)
98
100
  racc
99
101
  phlex (1.11.0)
@@ -119,10 +121,10 @@ GEM
119
121
  rails-html-sanitizer (1.6.0)
120
122
  loofah (~> 2.21)
121
123
  nokogiri (~> 1.14)
122
- railties (7.1.3.4)
123
- actionpack (= 7.1.3.4)
124
- activesupport (= 7.1.3.4)
125
- irb
124
+ railties (7.2.1)
125
+ actionpack (= 7.2.1)
126
+ activesupport (= 7.2.1)
127
+ irb (~> 1.13)
126
128
  rackup (>= 1.0.0)
127
129
  rake (>= 12.2)
128
130
  thor (~> 1.0, >= 1.2.2)
@@ -132,31 +134,31 @@ GEM
132
134
  rdoc (6.7.0)
133
135
  psych (>= 4.0.0)
134
136
  regexp_parser (2.9.2)
135
- reline (0.5.9)
137
+ reline (0.5.10)
136
138
  io-console (~> 0.5)
137
- rexml (3.3.2)
138
- strscan
139
- rubocop (1.64.1)
139
+ rexml (3.3.7)
140
+ rubocop (1.65.1)
140
141
  json (~> 2.3)
141
142
  language_server-protocol (>= 3.17.0)
142
143
  parallel (~> 1.10)
143
144
  parser (>= 3.3.0.2)
144
145
  rainbow (>= 2.2.2, < 4.0)
145
- regexp_parser (>= 1.8, < 3.0)
146
+ regexp_parser (>= 2.4, < 3.0)
146
147
  rexml (>= 3.2.5, < 4.0)
147
148
  rubocop-ast (>= 1.31.1, < 2.0)
148
149
  ruby-progressbar (~> 1.7)
149
150
  unicode-display_width (>= 2.4.0, < 3.0)
150
- rubocop-ast (1.31.3)
151
+ rubocop-ast (1.32.3)
151
152
  parser (>= 3.3.1.0)
152
153
  rubocop-performance (1.21.1)
153
154
  rubocop (>= 1.48.1, < 2.0)
154
155
  rubocop-ast (>= 1.31.1, < 2.0)
155
156
  ruby-progressbar (1.13.0)
156
- standard (1.39.2)
157
+ securerandom (0.3.1)
158
+ standard (1.40.0)
157
159
  language_server-protocol (~> 3.17.0.2)
158
160
  lint_roller (~> 1.0)
159
- rubocop (~> 1.64.0)
161
+ rubocop (~> 1.65.0)
160
162
  standard-custom (~> 1.0.0)
161
163
  standard-performance (~> 1.4)
162
164
  standard-custom (1.0.2)
@@ -166,15 +168,15 @@ GEM
166
168
  lint_roller (~> 1.1)
167
169
  rubocop-performance (~> 1.21.0)
168
170
  stringio (3.1.1)
169
- strscan (3.1.0)
170
- thor (1.3.1)
171
+ thor (1.3.2)
171
172
  tzinfo (2.0.6)
172
173
  concurrent-ruby (~> 1.0)
173
174
  unicode-display_width (2.5.0)
175
+ useragent (0.16.10)
174
176
  webrick (1.8.1)
175
177
  xpath (3.2.0)
176
178
  nokogiri (~> 1.8)
177
- zeitwerk (2.6.17)
179
+ zeitwerk (2.6.18)
178
180
 
179
181
  PLATFORMS
180
182
  x86_64-darwin
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- phlexi-form (0.2.0)
4
+ phlexi-form (0.3.1)
5
5
  activesupport
6
6
  phlex (~> 1.11)
7
7
  zeitwerk
@@ -96,7 +96,7 @@ GEM
96
96
  builder (3.3.0)
97
97
  bundle-audit (0.1.0)
98
98
  bundler-audit
99
- bundler-audit (0.9.1)
99
+ bundler-audit (0.9.2)
100
100
  bundler (>= 1.2.0, < 3)
101
101
  thor (~> 1.0)
102
102
  capybara (3.40.0)
@@ -112,7 +112,7 @@ GEM
112
112
  activesupport (>= 3.0.0)
113
113
  railties (>= 3.0.0)
114
114
  thor (>= 0.14.6)
115
- concurrent-ruby (1.3.3)
115
+ concurrent-ruby (1.3.4)
116
116
  connection_pool (2.4.1)
117
117
  crass (1.0.6)
118
118
  date (3.3.4)
@@ -140,14 +140,14 @@ GEM
140
140
  marcel (1.0.4)
141
141
  matrix (0.4.2)
142
142
  mini_mime (1.1.5)
143
- minitest (5.24.1)
143
+ minitest (5.25.1)
144
144
  minitest-reporters (1.7.1)
145
145
  ansi
146
146
  builder
147
147
  minitest (>= 5.0)
148
148
  ruby-progressbar
149
149
  mutex_m (0.2.0)
150
- net-imap (0.4.14)
150
+ net-imap (0.4.16)
151
151
  date
152
152
  net-protocol
153
153
  net-pop (0.1.2)
@@ -159,8 +159,8 @@ GEM
159
159
  nio4r (2.7.3)
160
160
  nokogiri (1.16.7-x86_64-darwin)
161
161
  racc (~> 1.4)
162
- parallel (1.25.1)
163
- parser (3.3.4.0)
162
+ parallel (1.26.3)
163
+ parser (3.3.5.0)
164
164
  ast (~> 2.4.1)
165
165
  racc
166
166
  phlex (1.11.0)
@@ -213,32 +213,31 @@ GEM
213
213
  rdoc (6.7.0)
214
214
  psych (>= 4.0.0)
215
215
  regexp_parser (2.9.2)
216
- reline (0.5.9)
216
+ reline (0.5.10)
217
217
  io-console (~> 0.5)
218
- rexml (3.3.2)
219
- strscan
220
- rubocop (1.64.1)
218
+ rexml (3.3.7)
219
+ rubocop (1.65.1)
221
220
  json (~> 2.3)
222
221
  language_server-protocol (>= 3.17.0)
223
222
  parallel (~> 1.10)
224
223
  parser (>= 3.3.0.2)
225
224
  rainbow (>= 2.2.2, < 4.0)
226
- regexp_parser (>= 1.8, < 3.0)
225
+ regexp_parser (>= 2.4, < 3.0)
227
226
  rexml (>= 3.2.5, < 4.0)
228
227
  rubocop-ast (>= 1.31.1, < 2.0)
229
228
  ruby-progressbar (~> 1.7)
230
229
  unicode-display_width (>= 2.4.0, < 3.0)
231
- rubocop-ast (1.31.3)
230
+ rubocop-ast (1.32.3)
232
231
  parser (>= 3.3.1.0)
233
232
  rubocop-performance (1.21.1)
234
233
  rubocop (>= 1.48.1, < 2.0)
235
234
  rubocop-ast (>= 1.31.1, < 2.0)
236
235
  ruby-progressbar (1.13.0)
237
236
  sqlite3 (1.7.3-x86_64-darwin)
238
- standard (1.39.2)
237
+ standard (1.40.0)
239
238
  language_server-protocol (~> 3.17.0.2)
240
239
  lint_roller (~> 1.0)
241
- rubocop (~> 1.64.0)
240
+ rubocop (~> 1.65.0)
242
241
  standard-custom (~> 1.0.0)
243
242
  standard-performance (~> 1.4)
244
243
  standard-custom (1.0.2)
@@ -248,8 +247,7 @@ GEM
248
247
  lint_roller (~> 1.1)
249
248
  rubocop-performance (~> 1.21.0)
250
249
  stringio (3.1.1)
251
- strscan (3.1.0)
252
- thor (1.3.1)
250
+ thor (1.3.2)
253
251
  timeout (0.4.1)
254
252
  tzinfo (2.0.6)
255
253
  concurrent-ruby (~> 1.0)
@@ -260,7 +258,7 @@ GEM
260
258
  websocket-extensions (0.1.5)
261
259
  xpath (3.2.0)
262
260
  nokogiri (~> 1.8)
263
- zeitwerk (2.6.17)
261
+ zeitwerk (2.6.18)
264
262
 
265
263
  PLATFORMS
266
264
  x86_64-darwin
@@ -10,23 +10,23 @@ module Phlexi
10
10
  # A form component for building flexible and customizable forms.
11
11
  #
12
12
  # @example Basic usage
13
- # Phlexi::Form.new(user, action: '/users', method: 'post') do |f|
13
+ # Phlexi::Form(user, action: '/users', method: 'post') do
14
14
  # render field(:name).placeholder("Name").input_tag
15
15
  # render field(:email).placeholder("Email").input_tag
16
16
  # end
17
17
  #
18
18
  # @attr_reader [Symbol] key The form's key, derived from the record or explicitly set
19
19
  # @attr_reader [ActiveModel::Model, nil] object The form's associated object
20
- class Base < COMPONENT_BASE
21
- class Namespace < Structure::Namespace; end
20
+ class Base < Phlexi::Form::HTML
21
+ class Namespace < Phlexi::Form::Structure::Namespace; end
22
22
 
23
- class FieldBuilder < Structure::FieldBuilder; end
23
+ class Builder < Phlexi::Form::Builder; end
24
24
 
25
- class << self
26
- def inherited(subclass)
27
- subclass.const_set(:Namespace, Class.new(self::Namespace)) unless subclass.const_defined?(:Namespace)
28
- subclass.const_set(:FieldBuilder, Class.new(self::FieldBuilder)) unless subclass.const_defined?(:FieldBuilder)
29
- super
25
+ def self.inline(*, **, &block)
26
+ raise ArgumentError, "block is required" unless block
27
+
28
+ new(*, **) do |f|
29
+ f.instance_exec(&block)
30
30
  end
31
31
  end
32
32
 
@@ -47,7 +47,6 @@ module Phlexi
47
47
  def initialize(record, action: nil, method: nil, attributes: {}, **options)
48
48
  @form_action = action
49
49
  @form_method = method
50
- @form_class = options.delete(:class)
51
50
  @attributes = attributes
52
51
  @namespace_klass = options.delete(:namespace_klass) || default_namespace_klass
53
52
  @builder_klass = options.delete(:builder_klass) || default_builder_klass
@@ -61,8 +60,8 @@ module Phlexi
61
60
  # Renders the form template.
62
61
  #
63
62
  # @return [void]
64
- def view_template
65
- form_tag { form_template }
63
+ def view_template(&)
64
+ form_tag { form_template(&) }
66
65
  end
67
66
 
68
67
  # Executes the form's content block.
@@ -70,19 +69,7 @@ module Phlexi
70
69
  #
71
70
  # @return [void]
72
71
  def form_template
73
- instance_exec(&@_content_block) if @_content_block
74
- end
75
-
76
- # Renders the form tag with its contents.
77
- #
78
- # @yield The form's content
79
- # @return [void]
80
- def form_tag(&)
81
- form(**form_attributes) do
82
- render_hidden_method_field
83
- render_authenticity_token if authenticity_token?
84
- yield
85
- end
72
+ yield if block_given?
86
73
  end
87
74
 
88
75
  def extract_input(params)
@@ -110,10 +97,11 @@ module Phlexi
110
97
  else
111
98
  @object = record
112
99
  if @key.nil?
113
- unless object.respond_to?(:model_name) && object.model_name.respond_to?(:param_key) && object.model_name.param_key.present?
114
- raise ArgumentError, "record must respond to #model_name.param_key with a non nil value or set `key` option e.g. Phlexi::Form(record, key: :record)"
100
+ @key = if object.respond_to?(:model_name) && object.model_name.respond_to?(:param_key) && object.model_name.param_key.present?
101
+ object.model_name.param_key
102
+ else
103
+ object.class.name.demodulize.underscore
115
104
  end
116
- @key = object.model_name.param_key
117
105
  end
118
106
  end
119
107
  @key = @key.to_sym
@@ -133,6 +121,25 @@ module Phlexi
133
121
  attributes.fetch(:accept_charset) { attributes[:accept_charset] = "UTF-8" }
134
122
  end
135
123
 
124
+ # Retrieves the form's CSS classes.
125
+ #
126
+ # @return [String] The form's CSS classes
127
+ def form_class
128
+ themed(:base, nil)
129
+ end
130
+
131
+ # Renders the form tag with its contents.
132
+ #
133
+ # @yield The form's content
134
+ # @return [void]
135
+ def form_tag(&)
136
+ form(**form_attributes) do
137
+ render_hidden_method_field
138
+ render_authenticity_token if has_authenticity_token?
139
+ yield
140
+ end
141
+ end
142
+
136
143
  # Determines the form's action URL.
137
144
  #
138
145
  # @return [String, nil] The form's action URL
@@ -156,16 +163,11 @@ module Phlexi
156
163
  ActiveSupport::StringInquirer.new(@form_method)
157
164
  end
158
165
 
159
- # Retrieves the form's CSS classes.
160
- #
161
- # @return [String] The form's CSS classes
162
- attr_reader :form_class
163
-
164
166
  # Checks if the authenticity token should be included.
165
167
  #
166
168
  # @return [Boolean] True if the authenticity token should be included, false otherwise
167
- def authenticity_token?
168
- defined?(helpers) && options.fetch(:authenticity_token) { !form_method.get? }
169
+ def has_authenticity_token?
170
+ !form_method.get? && ((defined?(helpers) && helpers) || options[:authenticity_token])
169
171
  end
170
172
 
171
173
  # Retrieves the authenticity token.
@@ -221,13 +223,12 @@ module Phlexi
221
223
  #
222
224
  # @return [Hash] The form attributes
223
225
  def form_attributes
224
- {
226
+ mix({
225
227
  id: @namespace.dom_id,
226
- action: form_action,
227
- method: standardized_form_method,
228
228
  class: form_class,
229
- **attributes
230
- }
229
+ action: form_action,
230
+ method: standardized_form_method
231
+ }, attributes)
231
232
  end
232
233
 
233
234
  # Renders the authenticity token if required.
@@ -244,7 +245,7 @@ module Phlexi
244
245
  end
245
246
 
246
247
  def default_builder_klass
247
- self.class::FieldBuilder
248
+ self.class::Builder
248
249
  end
249
250
  end
250
251
  end