Sha256: 9f2db8a811c114170ada297c663130b8f51df012247c9b69bac32903d4836d9b

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Shoppe
  class Attachment < ActiveRecord::Base

    # Set the table name
    self.table_name = "shoppe_attachments"

    # Mount the Carrierwave uploader
    mount_uploader :file, AttachmentUploader

    # Relationships
    belongs_to :parent, polymorphic: true

    # Validations
    validates :file_name, presence: true
    validates :file_type, presence: true
    validates :file_size, presence: true
    validates :file, presence: true
    validates :token, presence: true, uniqueness: true

    # All attachments should have a token assigned to this
    before_validation { self.token = SecureRandom.uuid if self.token.blank? }

    # Set the appropriate values in the model
    before_validation do
      if self.file
        self.file_name = self.file.filename if self.file_name.blank?
        self.file_type = self.file.content_type if self.file_type.blank?
        self.file_size = self.file.size if self.file_size.blank?
      end
    end

    # Return the attachment for a given role
    def self.for(role)
      self.where(role: role).first
    end

    # Return the path to the attachment
    def path
      file.url
    end

    # Is the attachment an image?
    def image?
      file_type.match(/\Aimage\//).present?
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shoppe-1.1.2 app/models/shoppe/attachment.rb