Sha256: 0c4a4a7e00f7d663c719b39e38b4778f85a9dcf85020e21bbf58960013922729

Contents?: true

Size: 1.44 KB

Versions: 5

Compression:

Stored size: 1.44 KB

Contents

module Lolita
  module Upload
    class File < ActiveRecord::Base
      self.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

5 entries across 5 versions & 1 rubygems

Version Path
lolita-file-upload-0.7.1 app/models/lolita/upload/file.rb
lolita-file-upload-0.6.2 app/models/lolita/upload/file.rb
lolita-file-upload-0.6.1 app/models/lolita/upload/file.rb
lolita-file-upload-0.6.0 app/models/lolita/upload/file.rb
lolita-file-upload-0.5.0 app/models/lolita/upload/file.rb