ROM 0.6.1 Released
We’re pleased to announce that ROM 0.6.1 is now available, building on the improvements and API changes in 0.6.0.
This release includes updates of the following gems:
- rom 0.6.1 - changelog
- rom-sql 0.4.1 - changelog
- rom-rails 0.3.1 - changelog
- rom-lotus 0.0.1 - changelog
Auto-mapping of command results
The results of commands can now be mapped in the same way as relations, using as or map_with.
rom.command(:rel_name).as(:mapper_name)
New migrations interface
A database migration interface is now included in rom-sql. This is currently a thin wrapper around the excellent Sequel Migrations API.
You can use migrations with one or more repositories as follows:
ROM.setup(
  default: [:sql, 'sqlite::memory'],
  other: [:sql, 'postgres://localhost/test']
)
ROM.finalize
ROM::SQL.migration do
  change do
    create_table(:users) do
      primary_key :id
      String :name
    end
  end
end
# for a non-default repository
ROM::SQL.migration(:other) do
  # ...
end
The expected way to run migrations is to require the Rake task from rom/sql/rake_task, and provide a db:setup task that sets up the ROM connection.
require 'sqlite3'
require 'rom/sql/rake_task'
namespace :db do
  task :setup do
    ROM.setup(:sql, 'sqlite::memory')
    ROM.finalize
  end
end
Improvements to forms for Rails users
There are several new changes that rationalize and improve the usability of the Rails forms API:
- An optional mappingsdeclaration has been added, which applies named mappers to the results of form commands.
- Form generators can generate a shared base form class for new/update forms.
- inputand- validationsblocks are now available in all descendants of- ROM::Model::Form.
The following example shows what this looks like in action, with base attributes and validations shared by the child classes, which each declare their own command, mapper, and attribute inputs:
# app/forms/user_form.rb
class UserForm < ROM::Model::Form
  input do
    set_model_name 'User'
    attribute :name, String
    attribute :email, String
  end
  validations do
    relation :users
    validates :name, :email, presence: true
    validates :email, uniqueness: true
  end
end
# app/forms/new_user_form.rb
class NewUserForm < UserForm
  commands users: :create
  mappings users: :entity
  input do
    timestamps(:created_at)
  end
  def commit!
    users.try { users.create.call(attributes) }
  end
end
# app/forms/update_user_form.rb
class UpdateUserForm < UserForm
  commands users: :update
  mappings users: :entity
  input do
    timestamps(:updated_at)
  end
  def commit!
    users.try { users.update.by_id(id).set(attributes) }
  end
end
Lotus integration
And last but not least, rom-lotus is out shiny new integration with the Lotus Framework.
A very basic setup example:
# config/environment.rb
require 'lotus/setup'
require 'rom-lotus'
require_relative '../apps/web/application'
Lotus::Container.configure do
  mount Web::Application, at: '/'
  ROM::Lotus.setup(Web::Application) do |setup|
    setup.repositories[:default] = [:sql, "sqlite:///tmp/test.sqlite"]
  end
end
Expect to hear more about this as the integration evolves.
Upgrading to 0.6.1
Please let us know if you have any trouble with the upgrade. Contact us directly via Zulip or submit an issue on GitHub describing any problems you're having.
Onwards to 0.7
We’re planning to focus heavily on improvements to mappers and commands in 0.7, which should get us significantly closer to figuring out what the shape of these APIs will be in 1.0.
Thanks for your support!