Sha256: 7eaa3e1f7c6ca496f54ed861db1d0aa6e854ce7acb4afd9aa64342de1f6d8730

Contents?: true

Size: 965 Bytes

Versions: 2

Compression:

Stored size: 965 Bytes

Contents

module Fish0
  module Concerns
    module Cacheable
      extend ActiveSupport::Concern

      included do
        def cache_key(*timestamp_names)
          if timestamp_names.any?
            cache_key_string(max_updated_column_timestamp(timestamp_names))
          elsif (timestamp = max_updated_column_timestamp)
            cache_key_string(timestamp)
          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

        def cache_key_string(timestamp)
          "#{self.class.to_s.tableize}/#{primary_key_value}-#{timestamp.utc.to_s(:nsec)}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
test_fish0-0.2.0 lib/fish0/concerns/cacheable.rb
fish0-0.2.0 lib/fish0/concerns/cacheable.rb