Sha256: a00b55b8ee3483c6a8f061dfd07ea7c2e105904d8f9dacc407706b36188af6d0

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

class AttributeMapping
  module LocalCache
    class MappingCache < Struct.new(:namespace)
      include Traveller::LocalCache

      def fetch_value
        sql = %{
          SELECT attribute_name, value, label
          FROM attribute_mappings
          WHERE namespace = #{AttributeMapping.connection.quote(namespace)}
          ORDER BY value;
        }.squish!

        AttributeMapping.connection.select_rows(sql).each_with_object({}) do |a, result|
          name, value, label = a
          result[name] ||= {}
          result[name][value] = label
        end
      end

      def cache_name
        "attribute_mapping_#{namespace}"
      end
    end

    extend ActiveSupport::Concern

    included do
      after_save { self.class.cached_mapping_for(namespace).expire! }
      after_destroy { self.class.cached_mapping_for(namespace).expire! }
    end

    module ClassMethods
      def cached_mapping_for(namespace)
        @cached_mappings ||= {}
        @cached_mappings[namespace] ||= MappingCache.new(namespace)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
axle_attributes-1.13.2 app/models/attribute_mapping/local_cache.rb