Sha256: 563f6c6297811053afc208142df9cdc26b3fde9f5ce5f6968b29978314551182

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

# encoding: UTF-8
module ActAsAttachedFile
  extend ActiveSupport::Concern
  include AttachmentProcessing
  include StorageImageProcessing

  included do
    IMAGE_EXTS = %w[jpg jpeg pjpeg png x-png gif bmp]
    IMAGE_CONTENT_TYPES = IMAGE_EXTS.map{ |e| "image/#{e}" }

    belongs_to :user
    belongs_to :storage, polymorphic: true
    acts_as_nested_set scope: [:user_id, :storage_id, :storage_type]
    include TheSortableTree::Scopes
    
    before_validation :generate_file_name, on: :create
    after_create   :recalculate_storage_counters!
    after_destroy  :recalculate_storage_counters!
    scope :images, ->{ where(attachment_content_type: IMAGE_CONTENT_TYPES)  }
  end

  def file_css_class
    'f_' + TheStorages.file_ext(attachment_file_name)
  end

  # HELPERS
  def title
    attachment_file_name
  end

  def file_name
    TheStorages.file_name(attachment_file_name)
  end

  def content_type
    attachment_content_type
  end

  def mb_size
    FileSizeHelper.mb_size(attachment_file_size)
  end

  def path style = nil
    attachment.path(style)
  end

  def url style = nil, opts = {}
    url = attachment.url(style, opts)
    return url unless opts[:nocache]
    rnd = (rand*1000000).to_i.to_s
    url =~ /\?/ ? (url + rnd) : (url + '?' + rnd)
  end

  # BASE HELPERS
  def is_image?
    IMAGE_EXTS.include? TheStorages.file_ext(attachment_file_name)
  end

  def generate_file_name
    file_name  = attachment.instance_read(:file_name)
    file_name  = TheStorages.slugged_file_name(file_name)
    attachment.instance_write :file_name, file_name
  end

  # CALLBACKS
  def recalculate_storage_counters!
    storage.recalculate_storage_counters!
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
the_storages-0.0.2 app/models/concerns/act_as_attached_file.rb