pakyow-presenter 0.11.3 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +5 -5
  2. data/{pakyow-presenter/CHANGELOG.md → CHANGELOG.md} +0 -5
  3. data/LICENSE +4 -0
  4. data/{pakyow-presenter/README.md → README.md} +1 -2
  5. data/lib/pakyow/generators/presenter/presenter_generator.rb +28 -0
  6. data/lib/pakyow/generators/presenter/templates/presenter.rb.tt +2 -0
  7. data/lib/pakyow/plugin/helpers/rendering.rb +19 -0
  8. data/lib/pakyow/presenter/actions/auto_render.rb +15 -0
  9. data/lib/pakyow/presenter/attributes/attribute.rb +22 -0
  10. data/lib/pakyow/presenter/attributes/boolean.rb +24 -0
  11. data/lib/pakyow/presenter/attributes/hash.rb +89 -0
  12. data/lib/pakyow/presenter/attributes/set.rb +61 -0
  13. data/lib/pakyow/presenter/attributes/string.rb +38 -0
  14. data/lib/pakyow/presenter/attributes.rb +126 -0
  15. data/lib/pakyow/presenter/behavior/config.rb +29 -0
  16. data/lib/pakyow/presenter/behavior/error_rendering.rb +124 -0
  17. data/lib/pakyow/presenter/behavior/exposures.rb +29 -0
  18. data/lib/pakyow/presenter/behavior/implicit_rendering.rb +21 -0
  19. data/lib/pakyow/presenter/behavior/initializing.rb +37 -0
  20. data/lib/pakyow/presenter/behavior/modes.rb +117 -0
  21. data/lib/pakyow/presenter/behavior/versions.rb +71 -0
  22. data/lib/pakyow/presenter/behavior/watching.rb +21 -0
  23. data/lib/pakyow/presenter/binder.rb +115 -0
  24. data/lib/pakyow/presenter/binding_parts.rb +64 -0
  25. data/lib/pakyow/presenter/component.rb +74 -0
  26. data/lib/pakyow/presenter/composers/component.rb +78 -0
  27. data/lib/pakyow/presenter/composers/view.rb +73 -0
  28. data/lib/pakyow/presenter/errors.rb +63 -0
  29. data/lib/pakyow/presenter/framework.rb +203 -0
  30. data/lib/pakyow/presenter/front_matter_parser.rb +42 -0
  31. data/lib/pakyow/presenter/helpers/rendering.rb +18 -0
  32. data/lib/pakyow/presenter/presentable.rb +28 -0
  33. data/lib/pakyow/presenter/presentable_error.rb +19 -0
  34. data/lib/pakyow/presenter/presenter.rb +766 -0
  35. data/lib/pakyow/presenter/presenters/endpoint.rb +158 -0
  36. data/lib/pakyow/presenter/presenters/form.rb +522 -0
  37. data/lib/pakyow/presenter/processor.rb +61 -0
  38. data/lib/pakyow/presenter/renderable.rb +28 -0
  39. data/lib/pakyow/presenter/renderer.rb +269 -0
  40. data/lib/pakyow/presenter/rendering/actions/cleanup_prototype_nodes.rb +21 -0
  41. data/lib/pakyow/presenter/rendering/actions/cleanup_unbound_bindings.rb +35 -0
  42. data/lib/pakyow/presenter/rendering/actions/create_template_nodes.rb +27 -0
  43. data/lib/pakyow/presenter/rendering/actions/insert_prototype_bar.rb +101 -0
  44. data/lib/pakyow/presenter/rendering/actions/install_authenticity.rb +42 -0
  45. data/lib/pakyow/presenter/rendering/actions/place_in_mode.rb +56 -0
  46. data/lib/pakyow/presenter/rendering/actions/render_components.rb +279 -0
  47. data/lib/pakyow/presenter/rendering/actions/set_page_title.rb +35 -0
  48. data/lib/pakyow/presenter/rendering/actions/setup_endpoints.rb +62 -0
  49. data/lib/pakyow/presenter/rendering/actions/setup_forms.rb +174 -0
  50. data/lib/pakyow/presenter/significant_nodes.rb +309 -0
  51. data/lib/pakyow/presenter/templates.rb +229 -0
  52. data/lib/pakyow/presenter/versioned_view.rb +209 -0
  53. data/lib/pakyow/presenter/view.rb +586 -0
  54. data/lib/pakyow/presenter/views/form.rb +58 -0
  55. data/lib/pakyow/presenter/views/layout.rb +32 -0
  56. data/lib/pakyow/presenter/views/page.rb +72 -0
  57. data/lib/pakyow/presenter/views/partial.rb +28 -0
  58. data/lib/pakyow/presenter.rb +33 -0
  59. data/lib/pakyow/views/errors/layouts/development_error.html +102 -0
  60. data/lib/pakyow/views/errors/layouts/production_error.html +27 -0
  61. data/lib/pakyow/views/errors/pages/404.html +12 -0
  62. data/lib/pakyow/views/errors/pages/500.html +12 -0
  63. data/lib/pakyow/views/errors/pages/development/500.html +39 -0
  64. data/lib/string_doc/attributes.rb +109 -0
  65. data/lib/string_doc/meta_attributes.rb +48 -0
  66. data/lib/string_doc/meta_node.rb +328 -0
  67. data/lib/string_doc/node.rb +377 -0
  68. data/lib/string_doc.rb +642 -0
  69. metadata +95 -75
  70. data/pakyow-presenter/LICENSE +0 -20
  71. data/pakyow-presenter/lib/pakyow/presenter/attributes.rb +0 -228
  72. data/pakyow-presenter/lib/pakyow/presenter/base.rb +0 -38
  73. data/pakyow-presenter/lib/pakyow/presenter/binder.rb +0 -116
  74. data/pakyow-presenter/lib/pakyow/presenter/binder_set.rb +0 -94
  75. data/pakyow-presenter/lib/pakyow/presenter/binding_eval.rb +0 -37
  76. data/pakyow-presenter/lib/pakyow/presenter/config/presenter.rb +0 -42
  77. data/pakyow-presenter/lib/pakyow/presenter/container.rb +0 -6
  78. data/pakyow-presenter/lib/pakyow/presenter/doc_helpers.rb +0 -17
  79. data/pakyow-presenter/lib/pakyow/presenter/exceptions.rb +0 -11
  80. data/pakyow-presenter/lib/pakyow/presenter/ext/app.rb +0 -33
  81. data/pakyow-presenter/lib/pakyow/presenter/ext/call_context.rb +0 -28
  82. data/pakyow-presenter/lib/pakyow/presenter/helpers.rb +0 -46
  83. data/pakyow-presenter/lib/pakyow/presenter/page.rb +0 -110
  84. data/pakyow-presenter/lib/pakyow/presenter/partial.rb +0 -6
  85. data/pakyow-presenter/lib/pakyow/presenter/presenter.rb +0 -232
  86. data/pakyow-presenter/lib/pakyow/presenter/string_doc.rb +0 -380
  87. data/pakyow-presenter/lib/pakyow/presenter/string_doc_parser.rb +0 -144
  88. data/pakyow-presenter/lib/pakyow/presenter/string_doc_renderer.rb +0 -18
  89. data/pakyow-presenter/lib/pakyow/presenter/template.rb +0 -51
  90. data/pakyow-presenter/lib/pakyow/presenter/view.rb +0 -541
  91. data/pakyow-presenter/lib/pakyow/presenter/view_collection.rb +0 -330
  92. data/pakyow-presenter/lib/pakyow/presenter/view_composer.rb +0 -237
  93. data/pakyow-presenter/lib/pakyow/presenter/view_context.rb +0 -111
  94. data/pakyow-presenter/lib/pakyow/presenter/view_store.rb +0 -262
  95. data/pakyow-presenter/lib/pakyow/presenter/view_store_loader.rb +0 -43
  96. data/pakyow-presenter/lib/pakyow/presenter/view_version.rb +0 -113
  97. data/pakyow-presenter/lib/pakyow/presenter.rb +0 -8
  98. data/pakyow-presenter/lib/pakyow/views/errors/404.erb +0 -26
  99. data/pakyow-presenter/lib/pakyow/views/errors/500.erb +0 -23
  100. data/pakyow-presenter/lib/pakyow-presenter.rb +0 -1
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/presenter/presenter"
4
+ require "pakyow/presenter/presenters/form"
5
+
6
+ module Pakyow
7
+ module Presenter
8
+ module Presenters
9
+ class Endpoint < DelegateClass(Presenter)
10
+ def setup
11
+ if endpoint_method == :delete
12
+ setup_endpoint_for_removal(
13
+ path: endpoint_path
14
+ )
15
+ else
16
+ setup_endpoint(
17
+ path: endpoint_path,
18
+ method: endpoint_method
19
+ )
20
+ end
21
+ end
22
+
23
+ # Fixes an issue using pp inside a delegator.
24
+ #
25
+ def pp(*args)
26
+ Kernel.pp(*args)
27
+ end
28
+
29
+ # Delegate private methods.
30
+ #
31
+ def method_missing(method_name, *args, &block)
32
+ __getobj__.send(method_name, *args, &block)
33
+ end
34
+
35
+ def respond_to_missing?(method_name, include_private = false)
36
+ super || __getobj__.respond_to?(method_name, true)
37
+ end
38
+
39
+ private
40
+
41
+ def endpoint_method
42
+ if view.object.labeled?(:endpoint_object)
43
+ view.object.label(:endpoint_object).method
44
+ else
45
+ :get
46
+ end
47
+ end
48
+
49
+ def endpoint_path
50
+ if Pakyow.env?(:prototype)
51
+ case view.object.tagname
52
+ when "a"
53
+ view.attrs[:href].to_s
54
+ when "form"
55
+ view.attrs[:action].to_s
56
+ else
57
+ nil
58
+ end
59
+ elsif view.object.labeled?(:endpoint_object)
60
+ view.object.label(:endpoint_object).path(
61
+ __endpoint.params.merge(view.object.label(:endpoint_params).to_h)
62
+ )
63
+ else
64
+ nil
65
+ end
66
+ end
67
+
68
+ def setup_endpoint(path:, method:)
69
+ endpoint_action_presenter = endpoint_action
70
+ case endpoint_action_presenter.object.tagname
71
+ when "a"
72
+ if path && !Pakyow.env?(:prototype)
73
+ endpoint_action_presenter.attributes[:href] = path
74
+ end
75
+
76
+ if endpoint_action_presenter.attributes.has?(:href)
77
+ endpoint_path = __endpoint[:path].to_s
78
+ if endpoint_path == endpoint_action_presenter.attributes[:href]
79
+ attributes[:class].add(:current)
80
+ elsif endpoint_path.start_with?(endpoint_action_presenter.attributes[:href])
81
+ attributes[:class].add(:active)
82
+ end
83
+ end
84
+ when "form"
85
+ form_presenter = Form.new(__getobj__)
86
+ form_presenter.action = path
87
+ form_presenter.method = method
88
+ end
89
+ end
90
+
91
+ def setup_endpoint_for_removal(path:)
92
+ if object.tagname == "form"
93
+ form_presenter = presenter_for(__getobj__, type: Form)
94
+ form_presenter.action = path
95
+ form_presenter.method = :delete
96
+ attributes[:"data-ui"] = "confirmable"
97
+ else
98
+ replace(
99
+ View.new(
100
+ <<~HTML
101
+ <form action="#{path}" method="post" data-ui="confirmable">
102
+ <input type="hidden" name="_method" value="delete">
103
+ #{view.object.render}
104
+ </form>
105
+ HTML
106
+ )
107
+ )
108
+ end
109
+ end
110
+
111
+ class << self
112
+ # Recursively attach to binding endpoints.
113
+ #
114
+ # @api private
115
+ def attach_to_node(node, renders, binding_path: [])
116
+ node.each_significant_node(:binding) do |binding_node|
117
+ next_binding_path = binding_path.dup
118
+ if binding_node.significant?(:binding_within)
119
+ next_binding_path << binding_node.label(:channeled_binding)
120
+ end
121
+
122
+ current_binding_path = binding_path.dup
123
+ current_binding_path << binding_node.label(:channeled_binding)
124
+
125
+ if binding_node.significant?(:endpoint)
126
+ renders << {
127
+ binding_path: current_binding_path,
128
+ priority: :low,
129
+ block: Proc.new {
130
+ setup
131
+ }
132
+ }
133
+ else
134
+ binding_node.find_significant_nodes(:endpoint).each do |endpoint_node|
135
+ renders << {
136
+ binding_path: current_binding_path,
137
+ priority: :low,
138
+ block: Proc.new {
139
+ endpoint_view = endpoint(endpoint_node.label(:endpoint))
140
+ case endpoint_view
141
+ when Presenters::Form
142
+ Presenters::Endpoint.new(endpoint_view.__getobj__).setup
143
+ when Presenters::Endpoint
144
+ endpoint_view.setup
145
+ end
146
+ }
147
+ }
148
+ end
149
+ end
150
+
151
+ attach_to_node(binding_node, renders, binding_path: next_binding_path)
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,522 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ require "pakyow/support/core_refinements/array/ensurable"
6
+ require "pakyow/support/inflector"
7
+ require "pakyow/support/safe_string"
8
+
9
+ require "pakyow/presenter/presenter"
10
+ require "pakyow/presenter/presenters/endpoint"
11
+
12
+ module Pakyow
13
+ module Presenter
14
+ module Presenters
15
+ class Form < DelegateClass(Presenter)
16
+ class << self
17
+ # @api private
18
+ def pluralize_field_name(field)
19
+ unless field.attributes[:name].to_s.end_with?("[]") || field.attributes[:name].to_s.empty?
20
+ field.attributes[:name] = "#{field.attributes[:name]}[]"
21
+ end
22
+ end
23
+
24
+ # @api private
25
+ def connect_input_to_label(input, label)
26
+ if false || input.attributes[:id].to_s.empty?
27
+ id = SecureRandom.hex(4)
28
+ input.attributes[:id] = id
29
+ else
30
+ id = input.attributes[:id]
31
+ end
32
+
33
+ label.attributes[:for] = id
34
+ end
35
+ end
36
+
37
+ using Support::Refinements::Array::Ensurable
38
+
39
+ include Support::SafeStringHelpers
40
+
41
+ SUPPORTED_ACTIONS = %i(create update replace delete).freeze
42
+ ACTION_METHODS = { create: "post", update: "patch", replace: "put", delete: "delete" }.freeze
43
+
44
+ # @api private
45
+ ID_LABEL = :__form_id
46
+
47
+ def object_for_form
48
+ if labeled?(:binding)
49
+ presentables[channeled_binding_name]
50
+ end
51
+ end
52
+
53
+ def id
54
+ label(ID_LABEL)
55
+ end
56
+
57
+ # Sets the form action (where it submits to).
58
+ #
59
+ def action=(action)
60
+ if action.is_a?(Symbol)
61
+ if endpoint = app.endpoints.find(name: action)
62
+ view.object.set_label(:endpoint, action)
63
+ view.object.set_label(:endpoint_object, endpoint)
64
+ view.object.set_label(:endpoint_params, {})
65
+ Endpoint.new(__getobj__).setup
66
+ end
67
+ else
68
+ attrs[:action] = action
69
+ end
70
+ end
71
+
72
+ # Sets the form method. Automatically handles method overrides by prepending a hidden field
73
+ # named `_method` when +method+ is not get or post, setting the form method to +post+.
74
+ #
75
+ def method=(method)
76
+ method = method.to_s.downcase
77
+ if method_override_required?(method)
78
+ attrs[:method] = "post"
79
+
80
+ find_or_create_method_override_input.attributes[:value] = method
81
+ else
82
+ attrs[:method] = method
83
+ end
84
+ end
85
+
86
+ # Populates a select field with options.
87
+ #
88
+ def options_for(field, options = nil)
89
+ unless field_presenter = find(field) || find(Support.inflector.singularize(field)) || find(Support.inflector.pluralize(field))
90
+ raise ArgumentError.new("could not find field named `#{field}'")
91
+ end
92
+
93
+ unless options_for_allowed?(field_presenter)
94
+ raise ArgumentError.new("expected `#{field}' to be a select field, checkbox, radio button, or binding")
95
+ end
96
+
97
+ options = if block_given?
98
+ yield(field_presenter)
99
+ else
100
+ options
101
+ end
102
+
103
+ case field_presenter.object.tagname
104
+ when "select"
105
+ create_select_options(options, field_presenter)
106
+ when "input"
107
+ create_input_options(options, field_presenter)
108
+ else
109
+ create_options(options, field_presenter)
110
+ end
111
+ end
112
+
113
+ # Populates a select field with grouped options.
114
+ #
115
+ def grouped_options_for(field, options = nil)
116
+ unless field_presenter = find(field)
117
+ raise ArgumentError.new("could not find field named `#{field}'")
118
+ end
119
+
120
+ unless grouped_options_for_allowed?(field_presenter)
121
+ raise ArgumentError.new("expected `#{field}' to be a select field")
122
+ end
123
+
124
+ options = options || yield
125
+ case field_presenter.object.tagname
126
+ when "select"
127
+ create_grouped_select_options(options, field_presenter)
128
+ end
129
+ end
130
+
131
+ def setup(object = {})
132
+ use_binding_nodes
133
+ use_global_options
134
+
135
+ if block_given?
136
+ yield self
137
+ end
138
+
139
+ bind(object)
140
+ setup!
141
+ self
142
+ end
143
+
144
+ def setup?
145
+ view.object.labeled?(:__form_setup)
146
+ end
147
+
148
+ private def setup!
149
+ view.object.set_label(:__form_setup, true)
150
+ end
151
+
152
+ # Setup the form for creating an object.
153
+ #
154
+ def create(object = {})
155
+ yield self if block_given?
156
+ setup_form_for_binding(:create, object)
157
+ end
158
+
159
+ # Setup the form for updating an object.
160
+ #
161
+ def update(object)
162
+ yield self if block_given?
163
+ setup_form_for_binding(:update, object)
164
+ end
165
+
166
+ # Setup the form for replacing an object.
167
+ #
168
+ def replace(object)
169
+ yield self if block_given?
170
+ setup_form_for_binding(:replace, object)
171
+ end
172
+
173
+ # Setup the form for removing an object.
174
+ #
175
+ def delete(object)
176
+ yield self if block_given?
177
+ setup_form_for_binding(:delete, object)
178
+ end
179
+
180
+ # Fixes an issue using pp inside a delegator.
181
+ #
182
+ def pp(*args)
183
+ Kernel.pp(*args)
184
+ end
185
+
186
+ # Delegate private methods.
187
+ #
188
+ def method_missing(method_name, *args, &block)
189
+ __getobj__.send(method_name, *args, &block)
190
+ end
191
+
192
+ def respond_to_missing?(method_name, include_private = false)
193
+ super || __getobj__.respond_to?(method_name, true)
194
+ end
195
+
196
+ private
197
+
198
+ def setup_form_for_binding(action, object)
199
+ setup(object) do
200
+ if SUPPORTED_ACTIONS.include?(action)
201
+ unless labeled?(:endpoint)
202
+ if self.action = form_action_for_binding(action, object)
203
+ self.method = method_for_action(action)
204
+ end
205
+ end
206
+ else
207
+ raise ArgumentError.new("expected action to be one of: #{SUPPORTED_ACTIONS.join(", ")}")
208
+ end
209
+ end
210
+ end
211
+
212
+ def use_binding_nodes
213
+ view.object.set_label(:bound, true)
214
+ view.object.children.each_significant_node(:binding, descend: true) do |object|
215
+ if Pakyow::Presenter::Form::FIELD_TAGS.include?(object.tagname)
216
+ object.set_label(:bound, true)
217
+ end
218
+ end
219
+ end
220
+
221
+ def use_global_options
222
+ __getobj__.class.__global_options.each do |form_binding, options|
223
+ form = if view.object.tagname == "form" && view.binding_name == form_binding
224
+ self
225
+ else
226
+ form(form_binding)
227
+ end
228
+
229
+ if form
230
+ options.each do |field_binding, metadata|
231
+ if metadata[:block]
232
+ form.options_for(field_binding) do |context|
233
+ instance_exec(context, &metadata[:block])
234
+ end
235
+ else
236
+ form.options_for(field_binding, metadata[:options])
237
+ end
238
+ end
239
+ end
240
+ end
241
+ end
242
+
243
+ def form_action_for_binding(action, object)
244
+ [
245
+ Support.inflector.singularize(label(:binding)).to_sym,
246
+ Support.inflector.pluralize(label(:binding)).to_sym
247
+ ].map { |possible_endpoint_name|
248
+ params = Hash[label(:binding_info).to_h.map { |key, value|
249
+ [:"#{key}_id", value]
250
+ }].merge(object.to_h)
251
+
252
+ endpoint_path = label(:binding_info).to_h.keys.map { |key|
253
+ Support.inflector.pluralize(key).to_sym
254
+ }
255
+
256
+ endpoint_path << possible_endpoint_name
257
+ endpoint_path.uniq!
258
+
259
+ app.endpoints.path_to(*endpoint_path, action, params)
260
+ }.compact.first
261
+ end
262
+
263
+ def method_for_action(action)
264
+ ACTION_METHODS[action]
265
+ end
266
+
267
+ def method_override_required?(method)
268
+ method != "get" && method != "post"
269
+ end
270
+
271
+ def method_override_input
272
+ html_safe("<input type=\"hidden\" name=\"_method\">")
273
+ end
274
+
275
+ def find_or_create_method_override_input
276
+ unless input = view.object.find_first_significant_node(:method_override)
277
+ prepend(method_override_input)
278
+ input = view.object.find_first_significant_node(:method_override)
279
+ end
280
+
281
+ input
282
+ end
283
+
284
+ def create_select_options(values, field_presenter)
285
+ options = Oga::XML::Document.new
286
+
287
+ Array.ensure(values).compact.each do |value|
288
+ options.children << create_select_option(value, field_presenter)
289
+ end
290
+
291
+ add_options_to_select(options, field_presenter)
292
+ end
293
+
294
+ def create_grouped_select_options(values, field_presenter)
295
+ options = Oga::XML::Document.new
296
+
297
+ values.each do |group_name, grouped_values|
298
+ group = Oga::XML::Element.new(name: "optgroup")
299
+ group.set("label", ensure_html_safety(group_name.to_s))
300
+ options.children << group
301
+
302
+ grouped_values.each do |value|
303
+ group.children << create_select_option(value, field_presenter)
304
+ end
305
+ end
306
+
307
+ add_options_to_select(options, field_presenter)
308
+ end
309
+
310
+ def create_select_option(value, view)
311
+ option_binding = if option = view.object.find_first_significant_node(:option)
312
+ option.label(:binding)
313
+ else
314
+ nil
315
+ end
316
+
317
+ Oga::XML::Element.new(name: "option").tap do |option_node|
318
+ option_node.set("value", ensure_html_safety(option_value(value, view).to_s))
319
+
320
+ display_value = if value.is_a?(Array)
321
+ value[1]
322
+ elsif option_binding && value.respond_to?(:[])
323
+ value[option_binding.to_sym]
324
+ else
325
+ nil
326
+ end
327
+
328
+ option_node.inner_text = ensure_html_safety(display_value.to_s)
329
+ end
330
+ end
331
+
332
+ def create_input_options(values, field_presenter)
333
+ if values.is_a?(Array) && field_presenter.attributes[:type] != "radio"
334
+ self.class.pluralize_field_name(field_presenter.object)
335
+ end
336
+
337
+ values = Array.ensure(values).compact
338
+
339
+ if values.any?
340
+ field_view = Pakyow::Presenter::Form.from_object(field_presenter.view.object)
341
+ field_template = field_view.dup
342
+ insertable_field = field_view
343
+ current_field = field_view
344
+
345
+ values.each do |value|
346
+ current_field.attributes[:value] = option_value(value, field_presenter.view).to_s
347
+
348
+ unless current_field.equal?(field_view)
349
+ insertable_field.after(current_field)
350
+ insertable_field = current_field
351
+ end
352
+
353
+ current_field = field_template.dup
354
+ end
355
+ else
356
+ field_presenter.remove
357
+ end
358
+ end
359
+
360
+ def create_options(original_values, field_presenter)
361
+ values = Array.ensure(original_values).compact
362
+
363
+ if values.any?
364
+ field_view = Pakyow::Presenter::Form.from_object(field_presenter.view.object)
365
+ template = field_view.dup
366
+ insertable = field_view
367
+ current = field_view
368
+
369
+ values.each do |value|
370
+ if treat_as_nested?(current, value)
371
+ # We bind to a view here to avoid checking the value when setting up the option.
372
+ #
373
+ View.from_object(current.object).bind(value)
374
+
375
+ # Set the field names appropriately.
376
+ #
377
+ current.object.each_significant_node(:field) do |field|
378
+ name = "#{view.object.label(:binding)}[#{current.label(:binding)}]"
379
+ name = if original_values.is_a?(Array)
380
+ "#{name}[][#{field.label(:binding)}]"
381
+ else
382
+ "#{name}[#{field.label(:binding)}]"
383
+ end
384
+
385
+ field.attributes[:name] = name
386
+ end
387
+
388
+ # Insert a hidden field to identify the data on submission.
389
+ #
390
+ if key = option_value_keys(current, value).find { |k| value.include?(k) }
391
+ id_input = Oga::XML::Element.new(name: "input")
392
+ id_input.set(:type, "hidden")
393
+
394
+ name = "#{view.object.label(:binding)}[#{current.label(:binding)}]"
395
+ name = if original_values.is_a?(Array)
396
+ "#{name}[][#{key}]"
397
+ else
398
+ "#{name}[#{key}]"
399
+ end
400
+
401
+ id_input.set(:name, name)
402
+ id_input.set(:value, presentables[:__verifier].sign(
403
+ ensure_html_safety(value[key].to_s)
404
+ ))
405
+
406
+ current.prepend(html_safe(id_input.to_xml))
407
+ end
408
+ else
409
+ if input = current.object.find_first_significant_node(:field)
410
+ input.attributes[:name] = "#{view.object.label(:binding)}[#{current.label(:binding)}]"
411
+
412
+ if original_values.is_a?(Array) && input.attributes[:type] != "radio"
413
+ self.class.pluralize_field_name(input)
414
+ end
415
+
416
+ input.attributes[:value] = ensure_html_safety(option_value(value, current).to_s)
417
+ end
418
+
419
+ if label = current.object.find_first_significant_node(:label)
420
+ label.html = ensure_html_safety(label_value(value, label).to_s)
421
+ label.set_label(:bound, true)
422
+ end
423
+
424
+ if input && label
425
+ self.class.connect_input_to_label(input, label)
426
+ end
427
+ end
428
+
429
+ unless current.equal?(field_view)
430
+ insertable.after(current)
431
+ insertable = current
432
+ end
433
+
434
+ current.object.set_label(:bound, true)
435
+ current = template.dup
436
+ end
437
+ else
438
+ field_presenter.remove
439
+ end
440
+ end
441
+
442
+ def treat_as_nested?(view, value)
443
+ if value.is_a?(Array)
444
+ false
445
+ else
446
+ keys = option_value_keys(view, value, false)
447
+ view.object.each_significant_node(:field) do |field|
448
+ return true if field.labeled?(:binding) && !keys.include?(field.label(:binding))
449
+ end
450
+
451
+ false
452
+ end
453
+ end
454
+
455
+ def option_value(value, view)
456
+ if value.is_a?(Array)
457
+ value[0]
458
+ elsif value.is_a?(String)
459
+ value
460
+ elsif value.respond_to?(:[])
461
+ option_value_keys(view, value).each do |key|
462
+ if value.include?(key)
463
+ return value[key]
464
+ end
465
+ end
466
+
467
+ nil
468
+ else
469
+ value.to_s
470
+ end
471
+ end
472
+
473
+ def label_value(value, view)
474
+ if value.is_a?(Array)
475
+ value[1]
476
+ elsif value.is_a?(String)
477
+ value
478
+ elsif view.labeled?(:binding) && value.respond_to?(:[])
479
+ value[view.label(:binding)]
480
+ else
481
+ nil
482
+ end
483
+ end
484
+
485
+ def option_value_keys(view, value, include_binding_prop = true)
486
+ [].tap do |keys|
487
+ if include_binding_prop
488
+ keys << view.object.label(:binding_prop)
489
+ end
490
+
491
+ if value.class.respond_to?(:primary_key_field)
492
+ keys << value.class.primary_key_field
493
+ end
494
+
495
+ keys << :id
496
+ end.compact
497
+ end
498
+
499
+ def options_for_allowed?(field_presenter)
500
+ field_presenter.object.tagname == "select" || (
501
+ field_presenter.object.tagname == "input" && (
502
+ field_presenter.object.attributes[:type] == "checkbox" || field_presenter.object.attributes[:type] == "radio"
503
+ ) ||
504
+ field_presenter.object.significant?(:binding)
505
+ )
506
+ end
507
+
508
+ def grouped_options_for_allowed?(field_presenter)
509
+ field_presenter.object.tagname == "select"
510
+ end
511
+
512
+ def add_options_to_select(options, field_presenter)
513
+ # remove existing options
514
+ field_presenter.clear
515
+
516
+ # add generated options
517
+ field_presenter.append(html_safe(options.to_xml))
518
+ end
519
+ end
520
+ end
521
+ end
522
+ end