Class: Pakyow::Form::Framework

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/form/framework.rb

Instance Method Summary collapse

Instance Method Details

#bootObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pakyow/form/framework.rb', line 15

def boot
  object.class_eval do
    isolated :Controller do
      # Clear form errors after a successful submission.
      #
      after "dispatch" do
        if connection.form && connection.status < 400
          data.ephemeral(:errors, form_id: connection.form[:id]).set([])
        end
      end
    end

    isolated :Connection do
      include Pakyow::Application::Connection::Helpers::Form
    end

    handle InvalidData, as: :bad_request do |error|
      if connection.form && connection.form.include?(:origin)
        raw_messages = case error.result
        when Verifier::Result
          error.result.messages(type: :presentable)
        else
          error.result.messages
        end

        errors = case raw_messages
        when Array
          raw_messages.map { |message|
            { message: message }
          }
        when Hash
          raw_messages.flat_map { |type, messages|
            case messages
            when Array
              messages.map { |type_message|
                { field: type, message: type_message }
              }
            when Hash
              messages.flat_map { |field, field_messages|
                field_messages.map { |field_message|
                  { field: field, message: field_message }
                }
              }
            end
          }
        end

        if app.class.includes_framework?(:ui) && ui?
          data.ephemeral(:errors, form_id: connection.form[:id]).set(errors)
        else
          connection.set(:__form_errors, errors)
          connection.set(:__form_values, params.reject { |key| key == :form })
          reroute connection.form[:origin], method: :get, as: :bad_request
        end
      else
        reject
      end
    end

    component :form do
      def perform
        errors = if connection.values.include?(:__form_errors)
          connection.get(:__form_errors)
        else
          []
        end

        # We have to take over the management of form ids from presenter for error handling.
        # If we're re-rendering a submitted form we reuse the id, otherwise create a new one.
        #
        form_id = if connection.form
          connection.form[:id]
        else
          SecureRandom.hex(24)
        end

        expose :form_id, form_id

        # Relate ephemeral errors to the form id.
        #
        expose :form_errors, data.ephemeral(:errors, form_id: form_id).set(errors)

        # Expose submitted values to be presented in the form.
        #
        connection.get(:__form_values).to_h.each do |key, value|
          expose connection.form[:binding], value
        end
      end

      presenter do
        render do
          view.label(:form)[:id] = form_id
          presented_form_binding = presentables.dig(:__form, :binding)
          if presented_form_binding.nil? || presented_form_binding.to_sym == view.channeled_binding_name
            classify_form; classify_fields
            present_errors(form_errors)
          end
        end

        private

        def classify_form
          if form_errors.any?
            attrs[:class] << :"ui-errored"
          else
            attrs[:class].delete(:"ui-errored")
          end
        end

        def classify_fields
          view.each_binding_prop do |node|
            binding_name = node.label(:binding)
            error = form_errors.find { |e| e[:field] == binding_name }

            find(binding_name).with do |field|
              if error.nil?
                field.attrs[:class].delete(:"ui-errored")
                field.attrs[:title] = ""
              else
                field.attrs[:class] << :"ui-errored"
                field.attrs[:title] = error[:message]
              end
            end
          end
        end

        def present_errors(errors)
          if form_errors_presenter = component(:"form-errors")
            if error_presenter = form_errors_presenter.find(:error)
              error_presenter.present(errors)
            end

            if errors.empty?
              form_errors_presenter.attrs[:class] << :"ui-hidden"
            else
              form_errors_presenter.attrs[:class].delete(:"ui-hidden")
            end
          end
        end
      end
    end
  end
end