Sha256: 93735cd21f5e66335b1733a96e19fec28948d1caac3d334386435e06de011bd6

Contents?: true

Size: 1.47 KB

Versions: 9

Compression:

Stored size: 1.47 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

9 entries across 9 versions & 1 rubygems

Version Path
active_encode-1.2.3 lib/active_encode/persistence.rb
active_encode-1.2.2 lib/active_encode/persistence.rb
active_encode-1.2.1 lib/active_encode/persistence.rb
active_encode-1.2.0 lib/active_encode/persistence.rb
active_encode-1.1.3 lib/active_encode/persistence.rb
active_encode-1.1.2 lib/active_encode/persistence.rb
active_encode-1.1.1 lib/active_encode/persistence.rb
active_encode-1.1.0 lib/active_encode/persistence.rb
active_encode-1.0.0 lib/active_encode/persistence.rb