Sha256: 764de813f2610dd4530d0cb264a9fbda376924836779d6a50e9b2951b86c3a61

Contents?: true

Size: 1.04 KB

Versions: 15

Compression:

Stored size: 1.04 KB

Contents

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
mobility-0.4.3 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.4.2 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.4.1 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.4.0 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.3.6 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.3.5 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.3.4 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.3.3 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.3.2 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.3.1 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.3.0 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.2.3 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.2.2 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.2.1 lib/mobility/backends/active_record/pg_hash.rb
mobility-0.2.0 lib/mobility/backends/active_record/pg_hash.rb