/
record_create.cr
70 lines (62 loc) · 3.11 KB
/
record_create.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
require "./schema"
module Marten
module Handlers
# Handler allowing to create a new model record by processing a schema.
#
# This handler can be used to process a form, validate its data through the use of a schema, and create a record by
# using the validated data. It is expected that the handler will be accessed through a GET request first: when this
# happens the configured template is rendered and displayed, and the configured schema which is initialized can be
# accessed from the template context in order to render a form for example. When the form is submitted via a POST
# request, the configured schema is validated using the form data. If the data is valid, the corresponding model
# record is created and the handler returns an HTTP redirect to a configured success URL.
#
# ```
# class MyFormHandler < Marten::Handlers::RecordCreate
# model MyModel
# schema MyFormSchema
# template_name "my_form.html"
# success_route_name "my_form_success"
# end
# ```
#
# It should be noted that the redirect response issued will be a 302 (found).
#
# The model class used to create the new record can be configured through the use of the `#model` class method. The
# schema used to perform the validation can be defined through the use of the `#schema` class method. Alternatively,
# the `#schema_class` method can also be overridden to dynamically define the schema class as part of the request
# handling.
#
# The `#template_name` class method allows to define the name of the template to use to render the schema while the
# `#success_route_name` method can be used to specify the name of a route to redirect to once the schema has been
# validated. Alternatively, the `#sucess_url` class method can be used to provide a raw URL to redirect to. The same
# method can also be overridden at the instance level in order to rely on a custom logic to generate the sucess URL
# to redirect to.
class RecordCreate < Handlers::Schema
@record : DB::Model? = nil
# Returns the configured model class.
class_getter model : DB::Model.class | Nil
# Returns the created record upon a valid schema processing, returns `nil` otherwise.
getter record
setter record
# Allows to configure the model class that should be used to create the new record.
def self.model(model : DB::Model.class | Nil)
@@model = model
end
# Produces the response when the processed schema is valid.
#
# By default, this will create the new record and return a 302 redirect targetting the configured success URL.
def process_valid_schema
self.record = model.new(schema.validated_data)
self.record.try(&.save!)
super
end
# Returns the model used to create the new record.
def model : Model.class
self.class.model || raise Errors::ImproperlyConfigured.new(
"'#{self.class.name}' must define a model class via the '::model' class method method or by overriding the " \
"'#model' method"
)
end
end
end
end