Permalink
Browse files

Test Uploader Module

  • Loading branch information...
1 parent dd85ed6 commit 8c33b47a45a37b8bfce45ea836388c33fddb3248 @galetahub galetahub committed May 8, 2012
View
@@ -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)
@@ -117,6 +120,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ carrierwave
database_cleaner
factory_girl_rails (~> 3.2.0)
fuubar
View
@@ -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
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_development_dependency "sqlite3"
+ s.add_development_dependency "carrierwave"
end
@@ -0,0 +1,5 @@
+class Article < ActiveRecord::Base
+ attr_accessible :content, :title
+
+ has_one :picture, :as => :assetable, :dependent => :destroy
+end
@@ -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
@@ -0,0 +1,3 @@
+class Picture < Asset
+ mount_uploader :data, PictureUploader
+end
@@ -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
@@ -4,6 +4,8 @@
Bundler.require
require "rails-uploader"
+require 'carrierwave'
+require 'carrierwave/orm/activerecord'
module Dummy
class Application < Rails::Application
@@ -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
@@ -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 not shown.
@@ -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
@@ -0,0 +1,6 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :asset do
+ end
+end
View
@@ -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.