Sha256: 3ae209158820158a1f961e539f80e440dbafcee48db78b61f3790f2399d09876

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

# encoding: utf-8
# thanks for https://github.com/nowa/carrierwave-upyun/blob/master/spec/upload_spec.rb

require File.dirname(__FILE__) + '/spec_helper'
require "open-uri"

ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')

describe "Upload" do
  def setup_db
    ActiveRecord::Schema.define(:version => 1) do
      create_table :photos do |t|
        t.column :image, :string
      end
    end
  end
  
  def drop_db
    ActiveRecord::Base.connection.tables.each do |table|
      ActiveRecord::Base.connection.drop_table(table)
    end
  end
  
  class PhotoUploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick

    version :small do
      process :resize_to_fill => [100, 100]
    end
    
    def store_dir
      "photos"
    end

    def filename
      "images/#{secure_token(10)}.#{file.extension}" if original_filename.present?
    end

    protected
    def secure_token(length=16)
      var = :"@#{mounted_as}_secure_token"
      model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2))
    end
  end

  class Photo < ActiveRecord::Base
    mount_uploader :image, PhotoUploader
  end
  
  
  before :all do
    setup_db
  end
  
  after :all do
    drop_db
  end
  
  context "Upload Image" do
    it "does upload image" do
      f = load_file("ruby-china.png")
      photo = Photo.create(:image => f)
      photo.errors.count.should == 0
      open(photo.image.url).should_not == nil
      open(photo.image.url).size.should == f.size
      open(photo.image.small.url).should_not == nil
      puts ""
      puts 'The image was uploaded to:'
      puts ""
      puts photo.image.url
      puts photo.image.small.url
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carrierwave-qiniu-0.0.1 spec/upload_spec.rb