Sha256: 67bcf4de1832c6869e377f4416f93a63c2725c9c49ef5f06955efd3250d1cb66

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true
require 'active_support'

module ActiveEncode
  module Persistence
    extend ActiveSupport::Concern

    included do
      after_find do |encode|
        persist(persistence_model_attributes(encode))
      end

      around_create do |encode, block|
        create_options = encode.options
        encode = block.call
        persist(persistence_model_attributes(encode, create_options))
      end

      after_cancel do |encode|
        persist(persistence_model_attributes(encode))
      end

      after_reload do |encode|
        persist(persistence_model_attributes(encode))
      end
    end

    private

      def persist(encode_attributes)
        model = ActiveEncode::EncodeRecord.find_or_initialize_by(global_id: encode_attributes[:global_id])
        model.update(encode_attributes) # Don't fail if persisting doesn't succeed?
      end

      def persistence_model_attributes(encode, create_options = nil)
        attributes = {
          global_id: encode.to_global_id.to_s,
          state: encode.state,
          adapter: encode.class.engine_adapter.class.name,
          title: encode.input.url.to_s,
          # Need to ensure that these values come through or else validations will fail
          created_at: encode.created_at,
          updated_at: encode.updated_at,
          raw_object: encode.to_json,
          progress: encode.percent_complete
        }
        attributes[:create_options] = create_options.to_json if create_options.present?
        attributes
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_encode-0.8.2 lib/active_encode/persistence.rb
active_encode-0.8.1 lib/active_encode/persistence.rb
active_encode-0.8.0 lib/active_encode/persistence.rb
active_encode-0.7.0 lib/active_encode/persistence.rb