Sha256: e53b49b7d34e341948d8f63a8b1349db17c4a3b309ac1769431a5cd67a6db4d8

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

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

4 entries across 4 versions & 2 rubygems

Version Path
shoppe-1.1.1 app/models/shoppe/attachment.rb
shoppe-1.1.0 app/models/shoppe/attachment.rb
kkt_shoppe-1.1.2 app/models/shoppe/attachment.rb
shoppe-1.0.9 app/models/shoppe/attachment.rb