Skip to content

Commit

Permalink
Test Uploader Module
Browse files Browse the repository at this point in the history
  • Loading branch information
galetahub committed May 8, 2012
1 parent dd85ed6 commit 8c33b47
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -35,6 +35,9 @@ GEM
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.0)
carrierwave (0.6.2)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
database_cleaner (0.7.2)
diff-lcs (1.1.3)
erubis (2.7.0)
Expand Down Expand Up @@ -117,6 +120,7 @@ PLATFORMS
ruby

DEPENDENCIES
carrierwave
database_cleaner
factory_girl_rails (~> 3.2.0)
fuubar
Expand Down
2 changes: 1 addition & 1 deletion lib/uploader.rb
Expand Up @@ -21,7 +21,7 @@ def self.root_path

def self.assets
Dir[root_path.join('vendor/assets/javascripts/uploader/**', '*.{js,css}')].inject([]) do |list, path|
list << Pathname.new(path).relative_path_from(root_path.join('vendor/assets/javascripts'))
list << Pathname.new(path).relative_path_from(root_path.join('vendor/assets/javascripts')).to_s
list
end
end
Expand Down
1 change: 1 addition & 0 deletions rails-uploader.gemspec
Expand Up @@ -19,4 +19,5 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_development_dependency "sqlite3"
s.add_development_dependency "carrierwave"
end
5 changes: 5 additions & 0 deletions spec/dummy/app/models/article.rb
@@ -0,0 +1,5 @@
class Article < ActiveRecord::Base
attr_accessible :content, :title

has_one :picture, :as => :assetable, :dependent => :destroy
end
27 changes: 27 additions & 0 deletions spec/dummy/app/models/asset.rb
@@ -0,0 +1,27 @@
# == Schema Information
#
# Table name: assets
#
# id :integer(4) not null, primary key
# data_file_name :string(255) not null
# data_content_type :string(255)
# data_file_size :integer(4)
# assetable_id :integer(4) not null
# assetable_type :string(25) not null
# type :string(25)
# guid :string(10)
# user_id :integer(4)
# sort_order :integer(4) default(0)
# created_at :datetime
# updated_at :datetime
#
# Indexes
#
# index_assets_on_assetable_type_and_assetable_id (assetable_type,assetable_id)
# index_assets_on_user_id (user_id)
#
class Asset < ActiveRecord::Base
attr_accessible :data

belongs_to :assetable, :polymorphic => true
end
3 changes: 3 additions & 0 deletions spec/dummy/app/models/picture.rb
@@ -0,0 +1,3 @@
class Picture < Asset
mount_uploader :data, PictureUploader
end
55 changes: 55 additions & 0 deletions spec/dummy/app/uploaders/picture_uploader.rb
@@ -0,0 +1,55 @@
# encoding: utf-8

class PictureUploader < CarrierWave::Uploader::Base

# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick

# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper

# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end

# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end

# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end

# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end

end
2 changes: 2 additions & 0 deletions spec/dummy/config/application.rb
Expand Up @@ -4,6 +4,8 @@

Bundler.require
require "rails-uploader"
require 'carrierwave'
require 'carrierwave/orm/activerecord'

module Dummy
class Application < Rails::Application
Expand Down
19 changes: 19 additions & 0 deletions spec/dummy/db/migrate/20120508093416_create_assets.rb
@@ -0,0 +1,19 @@
class CreateAssets < ActiveRecord::Migration
def change
create_table :assets do |t|
t.string "data_file_name", :null => false
t.string "data_content_type"
t.integer "data_file_size"
t.integer "assetable_id", :null => false
t.string "assetable_type", :limit => 25, :null => false
t.string "type", :limit => 25
t.string "guid", :limit => 10
t.integer "user_id"

t.timestamps
end

add_index "assets", ["assetable_type", "assetable_id"]
add_index "assets", ["user_id"]
end
end
10 changes: 10 additions & 0 deletions spec/dummy/db/migrate/20120508093830_create_articles.rb
@@ -0,0 +1,10 @@
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :title
t.text :content

t.timestamps
end
end
end
Binary file modified spec/dummy/db/test.sqlite3
Binary file not shown.
8 changes: 8 additions & 0 deletions spec/factories/articles.rb
@@ -0,0 +1,8 @@
# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryGirl.define do
factory :article do
title "MyString"
content "MyText"
end
end
6 changes: 6 additions & 0 deletions spec/factories/assets.rb
@@ -0,0 +1,6 @@
# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryGirl.define do
factory :asset do
end
end
18 changes: 18 additions & 0 deletions spec/uploader_spec.rb
@@ -0,0 +1,18 @@
require 'spec_helper'

describe Uploader do
it "should be a Module" do
Uploader.should be_a(Module)
end

it "should generate random string" do
value = Uploader.guid
value.should_not be_blank
value.size.should == 10
end

it "should find all precompile assets" do
Uploader.assets.should_not be_nil
Uploader.assets.should include('uploader/jquery.fileupload.js')
end
end

0 comments on commit 8c33b47

Please sign in to comment.