martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
405 stars 23 forks source link

Add callbacks to schema handlers #111

Closed ellmetha closed 1 year ago

ellmetha commented 1 year ago

Description

Presently, the only way to extend the logic involved when processing valid/invalid schemas as part of handlers (subclasses of Marten::Handlers::Schema) is to override the #process_valid_schema and #process_invalid_schema.

For example:

class MyFormHandler < Marten::Handlers::Schema
  schema MySchema
  template_name "app/my_form.html"
  success_route_name "home"

  def process_valid_schema
    # This method is called when the schema is valid.
    # You can decide to do something with the validated data...
    super
  end

  def process_invalid_schema
    # This method is called when the schema is invalid.
    super
  end
end

This works fine, but overriding methods from the Marten::Handlers::Schema base class is not ideal. Being able to register methods to execute upon processing valid/invalid schemas would be preferable.

In this light, let's introduce the ability to define new kinds of callbacks as part of schema handlers (subclasses of Marten::Handlers::Schema):

If one of these callbacks returns an HTTP response object (instance of Marten::HTTP::Response), then the obtained response should bypass the rest of the schema handler implementation and be returned directly (like this is the case for standard handler callbacks).