Skip to content

Instantly share code, notes, and snippets.

@dsisnero
Forked from solnic/custom_changeset.rb
Created February 4, 2017 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsisnero/62bcf38ed6963b1e2a5b8473ab208957 to your computer and use it in GitHub Desktop.
Save dsisnero/62bcf38ed6963b1e2a5b8473ab208957 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "rom-sql"
require "rom-repository"
require "dry-types"
module Relations
class Users < ROM::Relation[:sql]
schema(:users, infer: true)
end
class Participants < ROM::Relation[:sql]
schema(:participants, infer: true)
end
end
module Repositories
class Users < ROM::Repository[:users]
end
class Participants < ROM::Repository[:participants]
end
end
config = ROM::Configuration.new(:sql, "sqlite::memory")
config.gateways[:default].tap do |gateway|
migration = gateway.migration do
change do
create_table :users do
primary_key :id
column :name, String
column :activated_at, Time
end
create_table :participants do
primary_key :id
string :name, null: true
column :activated_at, Time
end
end
end
migration.apply gateway.connection, :up
end
class CreateChangeset < ROM::Changeset::Create
map do |tuple|
tuple.merge(activated_at: Time.now)
end
end
config.register_relation Relations::Users
config.register_relation Relations::Participants
container = ROM.container(config)
user_repo = Repositories::Users.new(container)
participant_repo = Repositories::Participants.new(container)
puts user_repo.changeset(CreateChangeset).data(name: 'Jane').commit.inspect
# #<ROM::Struct[User] id=1 name="Jane" activated_at=2017-02-02 11:39:45 +0100>
puts participant_repo.changeset(CreateChangeset).data(name: 'Jane').commit.inspect
# #<ROM::Struct[Participant] id=1 name="Jane" activated_at=2017-02-02 11:39:45 +0100>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment