Sha256: b5049eec60e5ae67eb18971c91ab51882b3e7ed78f541f5b9a87dcda112537f0

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module Lolita
  module Upload
    class File < ActiveRecord::Base
      set_table_name :lolita_files
      attr_accessible :asset, :name, :fileable_type, :asset_extension
      mount_uploader :asset, FileUploader
      belongs_to :fileable, :polymorphic=>true

      #validate :size_limit FIXME to work with more than one tab
      before_save :set_metadata
      #before_create :singularize_files
      

      private

      def singularize_files
        
        if self.fileable && lolita = self.fileable.class.lolita
          if lolita.tabs.by_type(:files).association_type == :one
            self.class.destroy_all(["fileable_type = :type AND fileable_id = :id AND fileable_id > 0",:type => fileable_type,:id => fileable_id])
          end
        end
      end

      def size_limit
        if self.fileable && lolita=self.fileable.class.lolita
          maxsize=lolita.tabs.by_type(:files).maxfilesize
          if maxsize.to_i>0 && self.asset.file.size>maxsize
            bytes=Lolita::Support::Bytes.new(maxsize)
            self.errors.add(:asset,I18n.t("lolita.upload.errors.file too big",:value=>bytes.value,:unit=>bytes.unit))
          end
        end
      end

      def set_metadata
        unless self.asset.blank?
          self.name||=::File.basename(self.asset.store_path)
          self.asset_size=self.asset.file.size
          self.asset_extension=::File.extname(self.asset.store_path).gsub(".","")
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lolita-file-upload-0.4.13 app/models/lolita/upload/file.rb