Skip to content

Instantly share code, notes, and snippets.

@Kukunin
Created July 4, 2017 14:33
Show Gist options
  • Save Kukunin/618da0a0d912e9101914a51be03f63ea to your computer and use it in GitHub Desktop.
Save Kukunin/618da0a0d912e9101914a51be03f63ea to your computer and use it in GitHub Desktop.
Example of reading aggregates in rom-rb
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required.'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rom'
gem 'rom-sql'
gem 'rom-repository'
gem 'sqlite3'
gem 'rspec'
gem 'pry'
end
require 'rspec/autorun'
describe ROM::Repository do
let(:rom) { ROM.container(configuration) }
let(:configuration) do
ROM::Configuration.new(:sql, 'sqlite::memory').tap do |config|
config.default.create_table(:posts) do
primary_key :id
column :title, String, null: false
end
config.default.create_table(:tags) do
primary_key :id
foreign_key :post_id, :posts, null: false
column :name, String, null: false
end
config.relation(:posts) do
view :with_tags do
schema { self }
relation { self }
end
end
end
end
let(:repo) do
Class.new(ROM::Repository[:posts]) do
relations :tags
end.new(rom)
end
let(:post_id) { repo.root.insert(title: 'post 1') }
let(:posts) { repo.root.with_tags.to_a }
before { repo.tags.insert(post_id: post_id, name: 'tag 1') }
before { repo.tags.insert(post_id: post_id, name: 'tag 2') }
it { expect(posts.count).to eq 1 }
it { expect(posts.first.title).to eq 'post 1' }
it { expect(posts.first.tag_names).to eq ['tag 1', 'tag 2'] }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment