Sha256: 7b81c38ae3ec5157026cd45bcacc26103e59874e53e90516a96188a8bbd27b7f

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

module Mobility
  module Backend
=begin

Internal class used by ActiveRecord backends that store values as a hash.

=end
    class ActiveRecord::HashValued
      include ActiveRecord

      # @!group Backend Accessors
      #
      # @!macro backend_reader
      def read(locale, **_)
        translations[locale]
      end

      # @!macro backend_writer
      def write(locale, value, **_)
        translations[locale] = value
      end
      # @!endgroup

      def translations
        model.read_attribute(attribute)
      end
      alias_method :new_cache, :translations

      def write_to_cache?
        true
      end

      setup do |attributes, options|
        attributes.each { |attribute| store attribute, coder: Coder }
        before_validation do
          attributes.each { |attribute| self.send(:"#{attribute}=", {}) if send(attribute).nil? }
        end
      end

      class Coder
        def self.dump(obj)
          if obj.is_a? Hash
            obj = 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

2 entries across 2 versions & 1 rubygems

Version Path
mobility-0.1.14 lib/mobility/backend/active_record/hash_valued.rb
mobility-0.1.13 lib/mobility/backend/active_record/hash_valued.rb