Sha256: e8d92911e6e539aa2368e310c848b7fafa0a2b81048181b8f639d172f19ced5d

Contents?: true

Size: 1.37 KB

Versions: 13

Compression:

Stored size: 1.37 KB

Contents

module Lolita
  module Upload
    class File < ActiveRecord::Base
      set_table_name :lolita_files

      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

13 entries across 13 versions & 1 rubygems

Version Path
lolita-file-upload-0.4.12 app/models/lolita/upload/file.rb
lolita-file-upload-0.4.11 app/models/lolita/upload/file.rb
lolita-file-upload-0.3.0.2 app/models/lolita/upload/file.rb
lolita-file-upload-0.3.0.1 app/models/lolita/upload/file.rb
lolita-file-upload-0.4.10 app/models/lolita/upload/file.rb
lolita-file-upload-0.4.9 app/models/lolita/upload/file.rb
lolita-file-upload-0.4.8 app/models/lolita/upload/file.rb
lolita-file-upload-0.4.7 app/models/lolita/upload/file.rb
lolita-file-upload-0.4.2 app/models/lolita/upload/file.rb
lolita-file-upload-0.4.1 app/models/lolita/upload/file.rb
lolita-file-upload-0.4.0 app/models/lolita/upload/file.rb
lolita-file-upload-0.3.0 app/models/lolita/upload/file.rb
lolita-file-upload-0.2.9 app/models/lolita/upload/file.rb