Sha256: 7824c26bdc92cd408966a2653cf9ed8dbce3b846abcdb4096cf7ea74710e99aa

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

# encoding: UTF-8

require 'tempfile'

module Spontaneous::FieldTypes
  class FileField < Field
    include Spontaneous::Plugins::Field::EditorClass

    def outputs
      [:html, :filesize, :filename]
    end

    def preprocess(image_path)
      filename = mimetype = nil
      case image_path
      when Hash
        mimetype = image_path[:type]
        filename = image_path[:filename]
        image_path = image_path[:tempfile].path
      when String
        filename = ::File.basename(image_path)
      end
      return image_path unless File.exist?(image_path)

      media_file = Spontaneous::Media::File.new(owner, filename, mimetype)
      media_file.copy(image_path)
      set_unprocessed_value(filename)
      media_file
    end

    def generate_filesize(input)
      if input.respond_to?(:filesize)
        input.filesize
      else
        if ::File.exist?(input)
          ::File.size(input)
        else
          0
        end
      end
    end

    def generate_filename(input)
      if input.respond_to?(:filename)
        input.filename
      else
        ::File.basename(input.to_s)
      end
    end

    def generate_html(input)
      return input if input.is_a?(String)
      input.url
    end

    def export(user = nil)
      super(user).merge({
        :processed_value => processed_values
      })
    end

    def path
      value(:html)
    end
  end

  FileField.register
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spontaneous-0.2.0.alpha7 lib/spontaneous/field_types/file_field.rb
spontaneous-0.2.0.alpha6 lib/spontaneous/field_types/file_field.rb
spontaneous-0.2.0.alpha5 lib/spontaneous/field_types/file_field.rb
spontaneous-0.2.0.alpha4 lib/spontaneous/field_types/file_field.rb
spontaneous-0.2.0.alpha3 lib/spontaneous/field_types/file_field.rb