Releases: martenframework/marten
0.4.5
Marten 0.4.5 fixes a couple of bugs.
Bug fixes
- Fix a possible compilation error with custom settings namespace generation.
- Ensure that the
serve
management command recompiles projects upon translation file changes. - Fix the order of asset finders to ensure that those associated with configured asset directories (via the
assets.dirs
setting) have priority over those associated with application directories.
0.4.4
0.4.3
Marten 0.4.3 fixes a couple of bugs.
Bug fixes
- Ensure that request query parameters can be accessed from templates.
- Fix hidden folders being ignored by the
collectassets
management command. - Fix some ameba warnings in generated projects and applications.
- Fix inconsistency of
#using
method definitions between model classes and query sets. - Fix missing DB-specific requirements in generated projects.
0.4.2
Marten 0.4.2 fixes a couple of bugs.
Bug fixes
- Fix a possible syntax error in the SQL queries generated for SQLite databases when running migrations.
0.4.1
Marten 0.4.1 fixes a couple of bugs.
Bug fixes
- Fix the position of the
config/routes.cr
file requirement in thesrc/project.cr
file of generated projects.
0.4.0
Marten 0.4.0 brings substantial enhancements to the framework, including the addition of generators, multi-table inheritance, schema handler callbacks, and more!
Requirements and compatibility
Crystal 1.9, 1.10, and 1.11.
New features
Generators
Marten now provides a generator mechanism that makes it easy to create various abstractions, files, and structures within an existing project. This feature is available through the use of the gen
management command and facilitates the generation of key components such as models, schemas, emails, or applications. The authentication application can now also be added easily to existing projects through the use of generators. By leveraging generators, developers can improve their workflow and speed up the development of their Marten projects while following best practices.
Below are highlighted some examples illustrating the use of the gen
management command:
# Generate a model in the admin app:
marten gen model User name:string email:string --app admin
# Generate a new TestEmail email in the blog application:
marten gen email TestEmail --app blog
# Add a new 'blogging' application to the current project:
marten gen app blogging
# Add the authentication application to the current project:
margen gen auth
You can read more about the generator mechanism in the dedicated documentation. All the available generators are also listed in the generators reference.
Multi table inheritance
It is now possible to define models that inherit from other concrete models (ie. non-abstract models). In this situation, each model can be used/queried individually and has its own associated database table. The framework automatically defines a set of "links" between each model that uses multi table inheritance and its parent models in order to ensure that the relational structure and inheritance hierarchy are maintained.
For example:
class Person < Marten::Model
field :id, :big_int, primary_key: true, auto: true
field :first_name, :string, max_size: 100
field :last_name, :string, max_size: 100
end
class Employee < Person
field :company_name, :string, max_size: 100
end
employee = Employee.filter(first_name: "John").first!
employee.first_name # => "John"
All the fields defined in the Person
model can be accessed when interacting with records of the Employee
model (despite the fact that the data itself is stored in distinct tables).
You can read more about this new kind of model inheritance in Multi table inheritance.
Schema handler callbacks
Handlers that inherit from the base schema handler - Marten::Handlers::Schema
- or one of its subclasses (such as Marten::Handlers::RecordCreate
or Marten::Handlers::RecordUpdate
) can now define new kinds of callbacks that allow to easily manipulate the considered schema instance and to define logic to execute before the schema is validated or after (eg. when the schema validation is successful or failed):
before_schema_validation
after_schema_validation
after_successful_schema_validation
after_failed_schema_validation
For example, the after_successful_schema_validation
callback can be used to create a flash message after a schema has been successfully validated:
class ArticleCreateHandler < Marten::Handlers::Schema
success_route_name "home"
template_name "articles/create.html"
schema ArticleSchema
after_successful_schema_validation :generate_success_flash_message
private def generate_success_flash_message : Nil
flash[:notice] = "Article successfully created!"
end
end
Please head over to Schema handler callbacks to learn more about these new types of callbacks.
URL field for models and schemas
It is now possible to define url
fields in models and schemas. These allow you to easily persist valid URLs in your models but also to expect valid URL values in data validated through the use of schemas.
For example:
class User < Marten::Model
field :id, :big_int, primary_key: true, auto: true
field :website_url, :url, blank: true, null: true
end
Slug field for models and schemas
It is now possible to define slug
fields in models and schemas. These allow you to easily persist valid slug values (ie. strings that can only include characters, numbers, dashes, and underscores) in your models but also to expect such values in data validated through the use of schemas.
For example:
class User < Marten::Model
field :id, :big_int, primary_key: true, auto: true
field :username, :slug
end
Minor features
Models and databases
- Support for removing records from many-to-many fields was added and many-to-many field query sets now provide a
#remove
helper method allowing to easily remove specific records from a specific relation. You can learn more about this capability in Many-to-many relationships. - Support for clearing all the references to records targeted by many-to-many fields was added. Indeed, many-to-many field query sets now provide a
#clear
method allowing to easily clear a specific relation. You can learn more about this capability in Many-to-many relationships. - It is now possible to specify arrays of records to add or remove from a many-to-many relationship query set, through the use of the
#add
and#remove
methods. See the related documentation to learn more about interacting with records targeted by many-to-many relationships. - Records targeted by reverse relations that are contributed to models by
one_to_one
(ie. when using therelated
option) are now memoized when the corresponding methods are called on related model instances. - Relation fields that contribute methods that return query sets to models (such as
many_to_one
ormany_to_many
fields) now make sure that those query set objects are memoized at the record level. The corresponding instance variables are also reset when the considered records are reloaded. This allows to limit the number of queries involved when iterating multiple times over the records targeted by amany_to_many
field for example. - The
#order
query set method can now be called directly on model classes to allow retrieving all the records of the considered model in a specific order. - A
#pk?
model method can now be leveraged to determine if a primary key value is set on a given model record. - The
#join
query set method now makes it possible to pre-select one-to-one reverse relations. This essentially ...
0.3.4
0.3.3
Marten 0.3.3 fixes a couple of bugs.
Bug fixes
- Fix unexpected
Marten::Template::Errors::InvalidSyntax
exceptions raised when adding spaces between a template filter name and its argument. - Make sure that the
#add
method of many-to-many query sets honors the targeted DB alias. - Make sure that the
#delete
and#update
query set methods reset cached records if applicable. - Make sure that the
#add
method of many-to-many query sets resets cached records if applicable.
0.3.2
Marten 0.3.2 fixes a couple of bugs.
Bug fixes
- Fix possible inconsistencies in results returned by query sets based on the order of calls to
#filter
and#exclude
. - Fix invalid through model generation for recursive many-to-many relationships.
- Ensure that
#<field>?
model methods return false for empty field values. - Add missing
#<field>?
method forfile
model fields.
0.3.1
Marten 0.3.1 fixes a couple of bugs.
Bug fixes
- Ensure that context objects provided by generic handlers are initialized using
Marten::Template::Context
. - Fix a possible compilation error happening around template variables initialization.
- Ensure that
#<field>?
schema methods returnfalse
for empty field values.