Sha256: d97d857167c17eb6fc8e55a54adcf987c0304611774f4e8452e518d753dcddb3
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 Backend # @!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.5 | lib/mobility/backend/active_record/hash_valued.rb |
mobility-0.1.4 | lib/mobility/backend/active_record/hash_valued.rb |