pakyow-form 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 38724ce5ba048f63242d25fd9e7e0e58d08baf6e1230078067bfa243ade4aeac
4
+ data.tar.gz: 4f56809e4b503cfc879fedd67b0cb9263da7528e81dcee6f84ba142d0e24fdfb
5
+ SHA512:
6
+ metadata.gz: da7cbb0b434e38c6402758d84e3ad4becc4caeca18488531c68edf74f7c65b47ce222d05e32067e10faaad794d9ec8d6ae617cd7e6e17e1779160923f330be4f
7
+ data.tar.gz: c1f311416157209bbaa89ed22d288ecbb6442bb25b7f2b10501004c12a5351ad9e5108014ad8f8087e833da17d36366913860c058b82059c616efc34b7666e22
data/CHANGELOG.md ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # pakyow-form
2
+
3
+ Forms for Pakyow.
4
+
5
+ # Download
6
+
7
+ The latest version of Pakyow Form can be installed with RubyGems:
8
+
9
+ ```
10
+ gem install pakyow-form
11
+ ```
12
+
13
+ Source code can be downloaded as part of the Pakyow project on Github:
14
+
15
+ - https://github.com/pakyow/pakyow/tree/master/pakyow-form
16
+
17
+ # License
18
+
19
+ Pakyow Form is free and open-source under the [LGPLv3 license](https://choosealicense.com/licenses/lgpl-3.0/).
20
+
21
+ # Support
22
+
23
+ Found a bug? Tell us about it here:
24
+
25
+ - https://github.com/pakyow/pakyow/issues
26
+
27
+ We'd love to have you in the community:
28
+
29
+ - http://pakyow.org/get-involved
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/data"
4
+ require "pakyow/presenter"
5
+ require "pakyow/routing"
6
+
7
+ require "pakyow/form/framework"
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ require "pakyow/framework"
6
+
7
+ require "pakyow/support/extension"
8
+ require "pakyow/support/inflector"
9
+
10
+ module Pakyow
11
+ module Form
12
+ module ConnectionHelpers
13
+ def form
14
+ get(:__form)
15
+ end
16
+ end
17
+
18
+ class Framework < Pakyow::Framework(:form)
19
+ def boot
20
+ object.class_eval do
21
+ isolated :Controller do
22
+ # Clear form errors after a successful submission.
23
+ #
24
+ after "dispatch" do
25
+ if connection.form && connection.status < 400
26
+ data.ephemeral(:errors, form_id: connection.form[:id]).set([])
27
+ end
28
+ end
29
+ end
30
+
31
+ isolated :Connection do
32
+ include ConnectionHelpers
33
+ end
34
+
35
+ handle InvalidData, as: :bad_request do |error|
36
+ if connection.form && connection.form.include?(:origin)
37
+ errors = error.result.messages.flat_map { |type, messages|
38
+ case messages
39
+ when Array
40
+ messages.map { |type_message|
41
+ { field: type, message: "#{Support.inflector.humanize(type)} #{type_message}" }
42
+ }
43
+ when Hash
44
+ messages.flat_map { |field, field_messages|
45
+ field_messages.map { |field_message|
46
+ { field: field, message: "#{Support.inflector.humanize(field)} #{field_message}" }
47
+ }
48
+ }
49
+ end
50
+ }
51
+
52
+ if app.class.includes_framework?(:ui) && ui?
53
+ data.ephemeral(:errors, form_id: connection.form[:id]).set(errors)
54
+ else
55
+ connection.set(:__form_errors, errors)
56
+ connection.set(:__form_values, params.reject { |key| key == :form })
57
+ reroute connection.form[:origin], method: :get, as: :bad_request
58
+ end
59
+ else
60
+ reject
61
+ end
62
+ end
63
+
64
+ component :form do
65
+ def perform
66
+ errors = if connection.values.include?(:__form_errors)
67
+ connection.get(:__form_errors)
68
+ else
69
+ []
70
+ end
71
+
72
+ # We have to take over the management of form ids from presenter for error handling.
73
+ # If we're re-rendering a submitted form we reuse the id, otherwise create a new one.
74
+ #
75
+ form_id = if connection.form
76
+ connection.form[:id]
77
+ else
78
+ SecureRandom.hex(24)
79
+ end
80
+
81
+ expose :form_id, form_id
82
+
83
+ # Relate ephemeral errors to the form id.
84
+ #
85
+ expose :form_errors, data.ephemeral(:errors, form_id: form_id).set(errors)
86
+
87
+ # Expose submitted values to be presented in the form.
88
+ #
89
+ connection.get(:__form_values).to_h.each do |key, value|
90
+ expose connection.form[:binding], value
91
+ end
92
+ end
93
+
94
+ presenter do
95
+ render do
96
+ view.label(:form)[:id] = form_id
97
+ presented_form_binding = presentables.dig(:__form, :binding)
98
+ if presented_form_binding.nil? || presented_form_binding.to_sym == view.channeled_binding_name
99
+ classify_form; classify_fields
100
+ present_errors(form_errors)
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ def classify_form
107
+ if form_errors.any?
108
+ attrs[:class] << :"ui-errored"
109
+ else
110
+ attrs[:class].delete(:"ui-errored")
111
+ end
112
+ end
113
+
114
+ def classify_fields
115
+ view.each_binding_prop do |node|
116
+ binding_name = node.label(:binding)
117
+ error = form_errors.find { |e| e[:field] == binding_name }
118
+
119
+ find(binding_name).with do |field|
120
+ if error.nil?
121
+ field.attrs[:class].delete(:"ui-errored")
122
+ field.attrs[:title] = ""
123
+ else
124
+ field.attrs[:class] << :"ui-errored"
125
+ field.attrs[:title] = error[:message]
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ def present_errors(errors)
132
+ if form_errors_presenter = component(:"form-errors")
133
+ if error_presenter = form_errors_presenter.find(:error)
134
+ error_presenter.present(errors)
135
+ end
136
+
137
+ if errors.empty?
138
+ form_errors_presenter.attrs[:class] << :"ui-hidden"
139
+ else
140
+ form_errors_presenter.attrs[:class].delete(:"ui-hidden")
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pakyow-form
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Bryan Powell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pakyow-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0.rc1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0.rc1
27
+ - !ruby/object:Gem::Dependency
28
+ name: pakyow-data
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0.rc1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0.rc1
41
+ - !ruby/object:Gem::Dependency
42
+ name: pakyow-presenter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0.rc1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0.rc1
55
+ - !ruby/object:Gem::Dependency
56
+ name: pakyow-routing
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0.rc1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0.rc1
69
+ - !ruby/object:Gem::Dependency
70
+ name: pakyow-support
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.0.rc1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.0.rc1
83
+ description: Forms for Pakyow
84
+ email: bryan@metabahn.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - CHANGELOG.md
90
+ - README.md
91
+ - lib/pakyow/form.rb
92
+ - lib/pakyow/form/framework.rb
93
+ homepage: https://pakyow.org
94
+ licenses:
95
+ - LGPL-3.0
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 2.5.0
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.3.1
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.7.6
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Pakyow Forms
117
+ test_files: []