Sha256: 4c0d35cc2e2edba4d60e1e2908fb4076ce06c4f2665a4ecfdf8a202f0ae84283

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

module S3Relay
  class Upload < ActiveRecord::Base

    belongs_to :parent, polymorphic: true, optional: true

    validates :uuid,         presence: true, uniqueness: true
    validates :upload_type,  presence: true
    validates :filename,     presence: true
    validates :content_type, presence: true
    validates :pending_at,   presence: true

    after_initialize :finalize
    after_create :notify_parent

    def self.pending
      where(state: "pending")
    end

    def self.imported
      where(state: "imported")
    end

    def pending?
      state == "pending"
    end

    def imported?
      state == "imported"
    end

    def mark_imported!
      update_attributes(state: "imported", imported_at: Time.now)
    end

    def notify_parent
      return unless parent.present?

      if parent.respond_to?(:import_upload)
        parent.import_upload(id)
      end
    end

    def private_url
      S3Relay::PrivateUrl.new(uuid, filename).generate
    end

    private

    def finalize
      self.state      ||= "pending"
      self.pending_at ||= Time.now
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
s3_relay-0.7.0 app/models/s3_relay/upload.rb
s3_relay-0.6.2 app/models/s3_relay/upload.rb
s3_relay-0.6.1 app/models/s3_relay/upload.rb
s3_relay-0.6.0 app/models/s3_relay/upload.rb