Sha256: 9221c6597ac7653f788369007a48b5176a8b81f8dbdd2f501178a501bb6e0275

Contents?: true

Size: 974 Bytes

Versions: 10

Compression:

Stored size: 974 Bytes

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 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

10 entries across 10 versions & 1 rubygems

Version Path
fish0-0.0.14 lib/fish0/concerns/cacheable.rb
fish0-0.0.13 lib/fish0/concerns/cacheable.rb
fish0-0.0.12 lib/fish0/concerns/cacheable.rb
fish0-0.0.11 lib/fish0/concerns/cacheable.rb
fish0-0.0.10 lib/fish0/concerns/cacheable.rb
fish0-0.0.9 lib/fish0/concerns/cacheable.rb
fish0-0.0.8 lib/fish0/concerns/cacheable.rb
fish0-0.0.7 lib/fish0/concerns/cacheable.rb
fish0-0.0.6 lib/fish0/concerns/cacheable.rb
fish0-0.0.5 lib/fish0/concerns/cacheable.rb