Skip to main content
Version: Next

Marten 0.5.0 release notes

Under development.

Requirements and compatibility

  • Crystal: 1.10, 1.11, and 1.12.
  • Databases:
    • MariaDB 10.4 and higher.
    • MySQL 8.0.11 and higher.
    • PostgreSQL 12 and higher.
    • SQLite 3.27.0 and higher.

New features

Relations pre-fetching

Marten now provides the ability to prefetch relations when using query sets through the use of the new #prefetch method. When using #prefetch, the records corresponding to the specified relationships will be prefetched in single batches and each record returned by the original query set will have the corresponding related objects already selected and populated.

For example:

posts_1 = Post.all.to_a
# hits the database to retrieve the related "tags" (many-to-many relation)
puts posts_1[0].tags.to_a

posts_2 = Post.all.prefetch(:tags).to_a
# doesn't hit the database since the related "tags" relation was already prefetched
puts posts_2[0].tags.to_a

Like the existing #join method, this allows to alleviate N+1 issues commonly encountered when accessing related objects. However, unlike #join (which can only be used with single-valued relationships), #prefetch can be used with both single-valued relationships and multi-valued relationships (such as many-to-many relationships, reverse many-to-many relationships, and reverse many-to-one relationships).

Please refer to Pre-fetching relations to learn more about this new capability.

Minor features

Models and databases

  • A new #pks method was introduced for query sets to make it easy to retrieve the primary key values of the model records targeted by a given current query set.
  • A #count method is now available on model classes and provides the same functionality as the #count query set method.
  • A new #bulk_create method was introduced to make it easy to insert multiple model instances into the database in a single query (which can be useful when dealing with large amounts of data that need to be inserted into the database).
  • A new #average method was introduced to allow computing the average values of a specific model field at the database level for the records targeted by a specific query set.
  • A new #sum method was introduced to allow computing the sum of the values of a specific model field at the database level for the records targeted by a specific query set.
  • It is now possible to compute the minimum and maximum values of a specific field at the database level for the records targeted by a query set through the use of the #minimum and #maximum methods.
  • The in query set predicate now supports filtering on arrays of model records directly.

Handlers and HTTP

  • The #render helper method and the generic handlers that involve template renderings now automatically insert the request object in the template context.
  • The Marten::Handlers::RecordDetail, Marten::Handlers::RecordUpdate, and Marten::Handlers::RecordDelete generic handlers now provide the ability to specify a custom query set instead of a model class. This can be achieved through the use of the #queryset macro.
  • A new middleware was introduced to make it easy to override the method of incoming requests based on the value of a specific request parameter or header: the method override middleware. This mechanism is useful for overriding HTTP methods in HTML forms that natively support GET and POST methods only. A dedicated set of settings is also available to easily customize the behavior of this middleware.

Templates

  • Enum values can now be used in templates. Please refer to Using enums in contexts to learn more about this capability.
  • Support for the nil, true, and false literals was added to the Marten templating language. Please refer to Literal values for more details on supported literals.
  • The assign template tag now supports an unless assigned suffix which allows to specify that the assignment must happen only if no variable with the same name is already present in the template context.
  • The include template tag now supports isolated and contextual suffixes that allow specifying whether the included templates should have access to the outer context variables. Unless specified, the default behavior is controlled by the new templates.isolated_inclusions setting.
  • A new capture template tag was introduced to make it possible to easily assign the output of a block of code to a new variable.

Development

  • The collectassets management command now provides the ability to fingerprint collected assets and generate a corresponding JSON manifest. Please refer to Asset manifests and fingerprinting to learn more about this capability.
  • The built-in authentication application (which can be generated either through the use of the new management command or the auth generator) now uses POST requests when signing out users.

Security

  • It is now possible to configure that the CSRF token must be persisted in the session store. This can be achieved by setting the use_session setting to true.

Backward incompatible changes

Handlers and HTTP

  • Handlers making use of the Marten::Handlers::Schema generic handler (or schemas inheriting from it, such as Marten::Handlers::RecordCreate or Marten::Handlers::RecordUpdate) will now return a 422 Unprocessable Content response instead of a 200 OK response when processing an invalid schema. While this won't have any incidence on end users, this change may make some specs fail if you were testing the response code of handlers that process invalid schema data. The way to fix this will be to replace 200 by 422 in the impacted specs.