Sha256: 0999080a32d533f6e32cef3d07b09c8cb8cf9328fa207d3225a348ad4afefbe3

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Fish0
  module Concerns
    module Cacheable
      extend ActiveSupport::Concern

      included do
        def cache_key(*timestamp_names)
          case
          when timestamp_names.any?
            timestamp = max_updated_column_timestamp(timestamp_names)
            "#{self.class.to_s.tableize}/#{primary_key_value}-#{timestamp.utc.to_s(:nsec)}"
          when timestamp = max_updated_column_timestamp
            "#{self.class.to_s.tableize}/#{primary_key_value}-#{timestamp.utc.to_s(:nsec)}"
          else
            "#{self.class.to_s.tableize}/#{primary_key_value}"
          end
        end

        private

        def primary_key_value
          send(primary_key)
        end

        def timestamp_attributes_for_update
          [:updated_at]
        end

        def max_updated_column_timestamp(timestamp_names = timestamp_attributes_for_update)
          timestamp_names
            .map { |attr| self[attr] }
            .compact
            .map(&:to_time)
            .max
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fish0-0.0.4 lib/fish0/concerns/cacheable.rb