-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathpg_hash.rb
More file actions
48 lines (40 loc) · 1.07 KB
/
Copy pathpg_hash.rb
File metadata and controls
48 lines (40 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
require "mobility/backends/active_record"
require "mobility/backends/hash_valued"
module Mobility
module Backends
=begin
Internal class used by ActiveRecord backends backed by a Postgres data type
(hstore, jsonb).
=end
class ActiveRecord::PgHash
include ActiveRecord
include HashValued
# @!macro backend_iterator
def each_locale
super { |l| yield l.to_sym }
end
def translations
model.read_attribute(attribute)
end
setup do |attributes|
attributes.each { |attribute| store attribute, coder: Coder }
end
class Coder
def self.dump(obj)
if obj.is_a? Hash
obj.inject({}) do |translations, (locale, value)|
translations[locale] = value if value.present?
translations
end
else
raise ArgumentError, "Attribute is supposed to be a Hash, but was a #{obj.class}. -- #{obj.inspect}"
end
end
def self.load(obj)
obj
end
end
end
end
end