Last active
March 10, 2017 22:47
-
-
Save flash-gordon/054da24d1e4984c6ca22e59a2b54d475 to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
require 'rom-sql' | |
DB = Sequel.connect('postgres://localhost/rom_sql') | |
DB.execute('create extension if not exists hstore ') | |
DB.drop_table?(:strings) | |
DB.create_table(:strings) do | |
primary_key(:id) | |
hstore :value | |
end | |
rom = ROM.container(:sql, DB) do |config| | |
config.relation(:strings) do | |
schema(infer: true) | |
def translated(language) | |
project { [id, `coalesce(value->'#{ language }', value->'en')`.as(:value)] } | |
end | |
end | |
end | |
DB[:strings] << { id: 1, value: Sequel.hstore('en' => 'Hi', 'fr' => 'salut') } | |
puts rom.relation(:strings).translated('en') # {:id=>1, :value=>"Hi"} | |
puts rom.relation(:strings).translated('fr') # {:id=>1, :value=>"salut"} | |
puts rom.relation(:strings).translated('ru') # {:id=>1, :value=>"Hi"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment