Forked from solnic/rom-sql-ar-like-polymorphic-associations.rb
Created
March 8, 2017 04:04
-
-
Save dsisnero/8957ebb372470fa82c5e3ea022419668 to your computer and use it in GitHub Desktop.
[rom-sql] AR-like polymorphic associations (PLEASE DON'T USE IT UNLESS YOU HAVE A LEGACY SCHEMA)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rom-repository' | |
require 'rom-sql' | |
require 'logger' | |
config = ROM::Configuration.new(:sql, 'sqlite::memory') | |
config.gateways[:default].connection.create_table(:songs) do | |
primary_key :id | |
column :title, String | |
end | |
config.gateways[:default].connection.create_table(:books) do | |
primary_key :id | |
column :title, String | |
end | |
config.gateways[:default].connection.create_table(:tags) do | |
primary_key :id | |
column :taggable_id, Integer | |
column :taggable_type, String | |
column :name, String | |
end | |
config.relation(:songs) do | |
schema(infer: true) do | |
associations do | |
has_many :tags, foreign_key: :taggable_id, view: :for_songs | |
end | |
end | |
def for_tags(tags) | |
by_pk(tags.select { |tag| tag[:taggable_type] == 'Song' }.map { |tag| tag[:taggable_id] }) | |
end | |
end | |
config.relation(:books) do | |
schema(infer: true) do | |
associations do | |
has_many :tags, foreign_key: :taggable_id, view: :for_books | |
end | |
end | |
def for_tags(tags) | |
by_pk(tags.select { |tag| tag[:taggable_type] == 'Book' }.map { |tag| tag[:taggable_id] }) | |
end | |
end | |
config.relation(:tags) do | |
schema(infer: true) do | |
associations do | |
belongs_to :books, as: :book, foreign_key: :taggable_id, view: :for_tags | |
belongs_to :songs, as: :song, foreign_key: :taggable_id, view: :for_tags | |
end | |
end | |
def for_songs | |
where(taggable_type: 'Song') | |
end | |
def for_books | |
where(taggable_type: 'Book') | |
end | |
end | |
class SongRepo < ROM::Repository[:songs] | |
relations :tags | |
commands :create | |
end | |
class BookRepo < ROM::Repository[:books] | |
relations :tags | |
commands :create | |
end | |
class TagRepo < ROM::Repository[:tags] | |
relations :songs, :books | |
commands :create | |
end | |
rom = ROM.container(config) | |
song_repo = SongRepo.new(rom) | |
book_repo = BookRepo.new(rom) | |
tag_repo = TagRepo.new(rom) | |
song = song_repo.create(title: 'A song') | |
book = book_repo.create(title: 'A book') | |
song_tag = tag_repo.create(taggable_id: song.id, taggable_type: 'Song', name: 'red') | |
book_tag = tag_repo.create(taggable_id: book.id, taggable_type: 'Book', name: 'blue') | |
puts song_repo.aggregate(:tags).to_a.first.inspect | |
#<ROM::Struct[Song] id=1 title="A song" tags=[#<ROM::Struct[Tag] id=1 taggable_id=1 taggable_type="Song" name="red">]> | |
puts book_repo.aggregate(:tags).to_a.first.inspect | |
#<ROM::Struct[Book] id=1 title="A book" tags=[#<ROM::Struct[Tag] id=2 taggable_id=1 taggable_type="Book" name="blue">]> | |
puts tag_repo.tags.wrap(:book).for_books.to_a.first.inspect | |
# #<ROM::Struct[Tag] id=2 taggable_id=1 taggable_type="Book" name="blue" book=#<ROM::Struct[Book] id=1 title="A book">> | |
puts tag_repo.tags.wrap(:song).for_songs.to_a.first.inspect | |
# #<ROM::Struct[Tag] id=1 taggable_id=1 taggable_type="Song" name="red" song=#<ROM::Struct[Song] id=1 title="A song">> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment