super 0.19.0 → 0.20.0

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.
@@ -2,53 +2,34 @@
2
2
 
3
3
  module Super
4
4
  class LinkBuilder
5
- def initialize(text, href, **options)
6
- @text = text
7
- @href = href
8
- @options = options
9
- @requirements = []
10
- @requirements += gather_requirements(text)
11
- @requirements += gather_requirements(href)
12
- @requirements += gather_requirements(options)
13
-
14
- unknown_arguments = @requirements - known_requirements
15
- if unknown_arguments.any?
16
- raise Error::ArgumentError, "Unknown arguments: #{unknown_arguments.join(", ")}"
17
- end
5
+ %i[text href options].each do |method_name|
6
+ class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
7
+ def #{method_name}(&block)
8
+ @#{method_name} = block
9
+ self
10
+ end
11
+
12
+ def process_#{method_name}(&block)
13
+ @process_#{method_name} = block
14
+ self
15
+ end
16
+ RUBY
18
17
  end
19
18
 
20
- attr_reader :requirements
21
-
22
19
  def resolve(**kwargs)
23
- Link.new(
24
- into_value(@text, kwargs),
25
- into_value(@href, kwargs),
26
- **into_value(@options, kwargs),
20
+ raise Super::Error::IncompleteBuilder, "LinkBuilder requires that #text is set" if @text.nil?
21
+ raise Super::Error::IncompleteBuilder, "LinkBuilder requires that #href is set" if @href.nil?
22
+
23
+ @options ||= -> (**) { {} }
24
+ @process_text ||= -> (t) { t }
25
+ @process_href ||= -> (h) { h }
26
+ @process_options ||= -> (o) { o }
27
+
28
+ Super::Link.new(
29
+ @process_text.call(@text.call(**kwargs)),
30
+ @process_href.call(@href.call(**kwargs)),
31
+ **@process_options.call(@options.call(**kwargs))
27
32
  )
28
33
  end
29
-
30
- private
31
-
32
- def known_requirements
33
- %i[params record].freeze
34
- end
35
-
36
- def gather_requirements(value_or_proc)
37
- return [] if !value_or_proc.is_a?(Proc)
38
-
39
- requirements =
40
- value_or_proc
41
- .parameters
42
- .select { |(kind, name)| kind = :keyreq }
43
- .map(&:last)
44
- end
45
-
46
- def into_value(value_or_proc, kwargs)
47
- if value_or_proc.kind_of?(Proc)
48
- value_or_proc.call(**kwargs)
49
- else
50
- value_or_proc
51
- end
52
- end
53
34
  end
54
35
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Super
4
+ module Useful
5
+ module I19
6
+ module_function
7
+
8
+ def build_chain(prefix, optional, suffix)
9
+ prefix = Array(prefix)
10
+ optional = Array(optional)
11
+ suffix = Array(suffix)
12
+
13
+ result = []
14
+
15
+ (optional.size + 1).times do
16
+ lookup = [prefix, optional, suffix].flatten.join(".").to_sym
17
+ result.push(lookup)
18
+ optional.pop
19
+ end
20
+
21
+ result
22
+ end
23
+
24
+ def chain_to_i18n(chain)
25
+ head, *tail = chain
26
+
27
+ [head, { default: tail }]
28
+ end
29
+
30
+ def i18n_with_fallback(prefix, optional, suffix)
31
+ chain_to_i18n(build_chain(prefix, optional, suffix))
32
+ end
33
+ end
34
+ end
35
+ end
data/lib/super/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Super
4
- VERSION = "0.19.0"
4
+ VERSION = "0.20.0"
5
5
  end
data/lib/super.rb CHANGED
@@ -7,6 +7,7 @@ require "rails/engine"
7
7
  require "super/schema/common"
8
8
  require "super/useful/builder"
9
9
  require "super/useful/enum"
10
+ require "super/useful/i19"
10
11
 
11
12
  require "super/action_inquirer"
12
13
  require "super/assets"
@@ -24,12 +25,16 @@ require "super/filter/guesser"
24
25
  require "super/filter/operator"
25
26
  require "super/filter/schema_types"
26
27
  require "super/form"
27
- require "super/form/builder"
28
28
  require "super/form/field_transcript"
29
29
  require "super/form/guesser"
30
30
  require "super/form/inline_errors"
31
31
  require "super/form/schema_types"
32
32
  require "super/form/strong_params"
33
+ require "super/form_builder"
34
+ require "super/form_builder/action_text_methods"
35
+ require "super/form_builder/base_methods"
36
+ require "super/form_builder/flatpickr_methods"
37
+ require "super/form_builder/option_methods"
33
38
  require "super/layout"
34
39
  require "super/link"
35
40
  require "super/link_builder"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Ahn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-14 00:00:00.000000000 Z
11
+ date: 2022-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -332,12 +332,16 @@ files:
332
332
  - lib/super/filter/operator.rb
333
333
  - lib/super/filter/schema_types.rb
334
334
  - lib/super/form.rb
335
- - lib/super/form/builder.rb
336
335
  - lib/super/form/field_transcript.rb
337
336
  - lib/super/form/guesser.rb
338
337
  - lib/super/form/inline_errors.rb
339
338
  - lib/super/form/schema_types.rb
340
339
  - lib/super/form/strong_params.rb
340
+ - lib/super/form_builder.rb
341
+ - lib/super/form_builder/action_text_methods.rb
342
+ - lib/super/form_builder/base_methods.rb
343
+ - lib/super/form_builder/flatpickr_methods.rb
344
+ - lib/super/form_builder/option_methods.rb
341
345
  - lib/super/layout.rb
342
346
  - lib/super/link.rb
343
347
  - lib/super/link_builder.rb
@@ -357,6 +361,7 @@ files:
357
361
  - lib/super/sort.rb
358
362
  - lib/super/useful/builder.rb
359
363
  - lib/super/useful/enum.rb
364
+ - lib/super/useful/i19.rb
360
365
  - lib/super/version.rb
361
366
  - lib/super/view_chain.rb
362
367
  - lib/super/view_helper.rb
@@ -365,11 +370,11 @@ homepage:
365
370
  licenses:
366
371
  - LGPL-3.0-only
367
372
  metadata:
368
- bug_tracker_uri: https://github.com/zachahn/super/issues
369
- changelog_uri: https://github.com/zachahn/super/blob/main/CHANGELOG.md
373
+ bug_tracker_uri: https://github.com/superadministration/super/issues
374
+ changelog_uri: https://github.com/superadministration/super/blob/main/CHANGELOG.md
370
375
  documentation_uri: https://superadministration.github.io/
371
- homepage_uri: https://github.com/zachahn/super
372
- source_code_uri: https://github.com/zachahn/super
376
+ homepage_uri: https://github.com/superadministration/super
377
+ source_code_uri: https://github.com/superadministration/super
373
378
  post_install_message:
374
379
  rdoc_options: []
375
380
  require_paths:
@@ -385,7 +390,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
385
390
  - !ruby/object:Gem::Version
386
391
  version: '0'
387
392
  requirements: []
388
- rubygems_version: 3.0.3.1
393
+ rubygems_version: 3.3.4
389
394
  signing_key:
390
395
  specification_version: 4
391
396
  summary: A simple, powerful, zero dependency Rails admin framework
@@ -1,289 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Super
4
- class Form
5
- # Example
6
- #
7
- # ```ruby
8
- # super_form_for([:admin, @member]) do |f|
9
- # # the long way
10
- # f.super.label :name
11
- # f.super.text_field :name
12
- # f.super.inline_errors :name
13
- #
14
- # # the short way (slightly different from the long way, for alignment)
15
- # f.super.text_field! :position
16
- # end
17
- # ```
18
- #
19
- # Refer to the Rails docs:
20
- # https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html
21
- class Builder < ActionView::Helpers::FormBuilder
22
- FIELD_ERROR_PROC = proc { |html_tag, instance| html_tag }
23
- FORM_BUILDER_DEFAULTS = { builder: self }.freeze
24
-
25
- def super(**options)
26
- @super_wrappers ||= Wrappers.new(self, @template)
27
- end
28
-
29
- class Wrappers
30
- def initialize(builder, template)
31
- @builder = builder
32
- @template = template
33
- end
34
-
35
- def inline_errors(attribute)
36
- if @builder.object
37
- messages = InlineErrors.error_messages(@builder.object, attribute).map do |msg|
38
- error_content_tag(msg)
39
- end
40
-
41
- @template.safe_join(messages)
42
- else
43
- error_content_tag(<<~MSG.html_safe)
44
- This form doesn't have an object, so something is probably wrong.
45
- Maybe <code>accepts_nested_attributes_for</code> isn't set up?
46
- MSG
47
- end
48
- end
49
-
50
- def label(attribute, text = nil, options = {}, &block)
51
- options, defaults = split_defaults(options, class: "block")
52
- options[:class] = join_classes(defaults[:class], options[:class])
53
-
54
- @builder.label(attribute, text, options, &block)
55
- end
56
-
57
- def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0")
58
- @builder.check_box(attribute, options, checked_value, unchecked_value)
59
- end
60
-
61
- def date_flatpickr(attribute, options = {})
62
- options, defaults = split_defaults(
63
- options,
64
- class: "super-input w-full",
65
- data: {
66
- controller: "flatpickr",
67
- flatpickr_options_value: {
68
- dateFormat: "Y-m-d",
69
- }
70
- }
71
- )
72
- options[:class] = join_classes(defaults[:class], options[:class])
73
- options[:data] = defaults[:data].deep_merge(options[:data] || {})
74
- options[:value] = @builder.object.public_send(attribute).presence
75
- options[:value] = options[:value].iso8601 if options[:value].respond_to?(:iso8601)
76
-
77
- @builder.text_field(attribute, options)
78
- end
79
-
80
- def datetime_flatpickr(attribute, options = {})
81
- options, defaults = split_defaults(
82
- options,
83
- class: "super-input w-full",
84
- data: {
85
- controller: "flatpickr",
86
- flatpickr_options_value: {
87
- enableSeconds: true,
88
- enableTime: true,
89
- dateFormat: "Z",
90
- }
91
- }
92
- )
93
- options[:class] = join_classes(defaults[:class], options[:class])
94
- options[:data] = defaults[:data].deep_merge(options[:data] || {})
95
- options[:value] = @builder.object.public_send(attribute).presence
96
- options[:value] = options[:value].iso8601 if options[:value].respond_to?(:iso8601)
97
-
98
- @builder.text_field(attribute, options)
99
- end
100
-
101
- def time_flatpickr(attribute, options = {})
102
- options, defaults = split_defaults(
103
- options,
104
- class: "super-input w-full",
105
- data: {
106
- controller: "flatpickr",
107
- flatpickr_options_value: {
108
- enableSeconds: true,
109
- enableTime: true,
110
- noCalendar: true,
111
- dateFormat: "H:i:S",
112
- }
113
- }
114
- )
115
- options[:class] = join_classes(defaults[:class], options[:class])
116
- options[:data] = defaults[:data].deep_merge(options[:data] || {})
117
- options[:value] = @builder.object.public_send(attribute).presence
118
- options[:value] = options[:value].strftime("%H:%M:%S") if options[:value].respond_to?(:strftime)
119
-
120
- @builder.text_field(attribute, options)
121
- end
122
-
123
- def password_field(attribute, options = {})
124
- options, defaults = split_defaults(options, class: "super-input w-full")
125
- options[:class] = join_classes(defaults[:class], options[:class])
126
-
127
- @builder.password_field(attribute, options)
128
- end
129
-
130
- def rich_text_area(attribute, options = {})
131
- options, defaults = split_defaults(options, class: "trix-content super-input w-full")
132
- options[:class] = join_classes(defaults[:class], options[:class])
133
-
134
- @builder.rich_text_area(attribute, options)
135
- end
136
-
137
- def select(attribute, choices, options = {}, html_options = {}, &block)
138
- options, defaults = split_defaults(options, include_blank: true)
139
- options = defaults.merge(options)
140
- html_options, html_defaults = split_defaults(html_options, class: "super-input super-input-select")
141
- html_options[:class] = join_classes(html_defaults[:class], html_options[:class])
142
-
143
- @builder.select(attribute, choices, options, html_options, &block)
144
- end
145
-
146
- def submit(value = nil, options = {})
147
- value, options = nil, value if value.is_a?(Hash)
148
- options, defaults = split_defaults(options, class: "super-button")
149
- options[:class] = join_classes(defaults[:class], options[:class])
150
-
151
- @builder.submit(value, options)
152
- end
153
-
154
- def text_field(attribute, options = {})
155
- options, defaults = split_defaults(options, class: "super-input w-full")
156
- options[:class] = join_classes(defaults[:class], options[:class])
157
-
158
- @builder.text_field(attribute, options)
159
- end
160
-
161
- def container(&block)
162
- @template.content_tag(:div, class: "super-field-group", &block)
163
- end
164
-
165
- def check_box!(attribute, checked_value: "1", unchecked_value: "0", label_text: nil, label: {}, field: {}, show_errors: true)
166
- label[:super] ||= {}
167
- label[:super] = { class: "select-none ml-1" }.merge(label[:super])
168
- container do
169
- compact_join([
170
- "<div>".html_safe,
171
- public_send(:check_box, attribute, field, checked_value, unchecked_value),
172
- public_send(:label, attribute, label_text, label),
173
- "</div>".html_safe,
174
- show_errors && inline_errors(attribute),
175
- ])
176
- end
177
- end
178
-
179
- def date_flatpickr!(attribute, label_text: nil, label: {}, field: {}, show_errors: true)
180
- container do
181
- compact_join([
182
- public_send(:label, attribute, label_text, label),
183
- %(<div class="mt-1">).html_safe,
184
- public_send(:date_flatpickr, attribute, field),
185
- show_errors && inline_errors(attribute),
186
- %(</div>).html_safe,
187
- ])
188
- end
189
- end
190
-
191
- def datetime_flatpickr!(attribute, label_text: nil, label: {}, field: {}, show_errors: true)
192
- container do
193
- compact_join([
194
- public_send(:label, attribute, label_text, label),
195
- %(<div class="mt-1">).html_safe,
196
- public_send(:datetime_flatpickr, attribute, field),
197
- show_errors && inline_errors(attribute),
198
- %(</div>).html_safe,
199
- ])
200
- end
201
- end
202
-
203
- def time_flatpickr!(attribute, label_text: nil, label: {}, field: {}, show_errors: true)
204
- container do
205
- compact_join([
206
- public_send(:label, attribute, label_text, label),
207
- %(<div class="mt-1">).html_safe,
208
- public_send(:time_flatpickr, attribute, field),
209
- show_errors && inline_errors(attribute),
210
- %(</div>).html_safe,
211
- ])
212
- end
213
- end
214
-
215
- def password_field!(attribute, label_text: nil, label: {}, field: {}, show_errors: true)
216
- container do
217
- compact_join([
218
- public_send(:label, attribute, label_text, label),
219
- %(<div class="mt-1">).html_safe,
220
- public_send(:password_field, attribute, field),
221
- show_errors && inline_errors(attribute),
222
- %(</div>).html_safe,
223
- ])
224
- end
225
- end
226
-
227
- def rich_text_area!(attribute, label_text: nil, label: {}, field: {}, show_errors: true)
228
- container do
229
- compact_join([
230
- public_send(:label, attribute, label_text, label),
231
- %(<div class="mt-1">).html_safe,
232
- public_send(:rich_text_area, attribute, field),
233
- show_errors && inline_errors(attribute),
234
- %(</div>).html_safe,
235
- ])
236
- end
237
- end
238
-
239
- def select!(attribute, collection, label_text: nil, label: {}, field: {}, show_errors: true)
240
- container do
241
- compact_join([
242
- public_send(:label, attribute, label_text, label),
243
- %(<div class="mt-1">).html_safe,
244
- public_send(:select, attribute, collection, field),
245
- show_errors && inline_errors(attribute),
246
- %(</div>).html_safe,
247
- ])
248
- end
249
- end
250
-
251
- def text_field!(attribute, label_text: nil, label: {}, field: {}, show_errors: true)
252
- container do
253
- compact_join([
254
- public_send(:label, attribute, label_text, label),
255
- %(<div class="mt-1">).html_safe,
256
- public_send(:text_field, attribute, field),
257
- show_errors && inline_errors(attribute),
258
- %(</div>).html_safe,
259
- ])
260
- end
261
- end
262
-
263
- private
264
-
265
- def split_defaults(options, **internal_defaults)
266
- defaults = options.delete(:super) || {}
267
- # prefer options set in `defaults`, since they are user overrides
268
- defaults = internal_defaults.merge(defaults)
269
-
270
- [options, defaults]
271
- end
272
-
273
- def join_classes(*class_lists)
274
- class_lists.flatten.map(&:presence).compact
275
- end
276
-
277
- def error_content_tag(content)
278
- @template.content_tag(:p, content, class: "text-red-400 text-xs italic pt-1")
279
- end
280
-
281
- def compact_join(*parts)
282
- @template.safe_join(
283
- parts.flatten.map(&:presence).compact
284
- )
285
- end
286
- end
287
- end
288
- end
289
- end