Sha256: ab52620c25db7de6cc4efa475a3d827227f2de54e6c633329d5334786559fe2d

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true
require "mobility/util"
require "mobility/backends/sequel"
require "mobility/backends/hash_valued"
require "mobility/sequel/column_changes"

module Mobility
  module Backends
=begin

Internal class used by Sequel backends backed by a Postgres data type (hstore,
jsonb).

=end
    module Sequel
      class PgHash
        include Sequel
        include HashValued

        def read(locale, options = {})
          super(locale.to_s, options)
        end

        def write(locale, value, options = {})
          super(locale.to_s, value, options)
        end

        # @!macro backend_iterator
        def each_locale
          super { |l| yield l.to_sym }
        end

        def translations
          model[column_name.to_sym]
        end

        setup do |attributes, options|
          column_affix = "#{options[:column_prefix]}%s#{options[:column_suffix]}"
          columns = attributes.map { |attribute| (column_affix % attribute).to_sym }

          before_validation = Module.new do
            define_method :before_validation do
              columns.each do |column|
                self[column].delete_if { |_, v| Util.blank?(v) }
              end
              super()
            end
          end
          include before_validation
          include Mobility::Sequel::HashInitializer.new(*columns)
          include Mobility::Sequel::ColumnChanges.new(attributes, column_affix: column_affix)

          plugin :defaults_setter
          columns.each { |column| default_values[column] = {} }
        end
      end
      private_constant :PgHash
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mobility-0.6.0 lib/mobility/backends/sequel/pg_hash.rb