Sha256: aaff679878e2591002cd5718a8f1706cd3e5bcf2c9148b6f9b9f5f9b092b7b31

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

module KktShoppe
  class Attachment < ActiveRecord::Base

    # Set the table name
    self.table_name = "kkt_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 & 1 rubygems

Version Path
kkt_shoppe-2.0.2 app/models/kkt_shoppe/attachment.rb
kkt_shoppe-2.0.1 app/models/kkt_shoppe/attachment.rb
kkt_shoppe-2.0.0 app/models/kkt_shoppe/attachment.rb
kkt_shoppe-1.3.0 app/models/kkt_shoppe/attachment.rb