diff --git a/Gemfile.lock b/Gemfile.lock index ea52d76..6b94f78 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/lib/uploader.rb b/lib/uploader.rb index 347e958..af6e4dc 100644 --- a/lib/uploader.rb +++ b/lib/uploader.rb @@ -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 diff --git a/rails-uploader.gemspec b/rails-uploader.gemspec index 6baee17..207fde8 100644 --- a/rails-uploader.gemspec +++ b/rails-uploader.gemspec @@ -19,4 +19,5 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.add_development_dependency "sqlite3" + s.add_development_dependency "carrierwave" end diff --git a/spec/dummy/app/models/article.rb b/spec/dummy/app/models/article.rb new file mode 100644 index 0000000..741350d --- /dev/null +++ b/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 diff --git a/spec/dummy/app/models/asset.rb b/spec/dummy/app/models/asset.rb new file mode 100644 index 0000000..c2a5ee3 --- /dev/null +++ b/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 diff --git a/spec/dummy/app/models/picture.rb b/spec/dummy/app/models/picture.rb new file mode 100644 index 0000000..254689a --- /dev/null +++ b/spec/dummy/app/models/picture.rb @@ -0,0 +1,3 @@ +class Picture < Asset + mount_uploader :data, PictureUploader +end diff --git a/spec/dummy/app/uploaders/picture_uploader.rb b/spec/dummy/app/uploaders/picture_uploader.rb new file mode 100644 index 0000000..d2899d8 --- /dev/null +++ b/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 diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index c608062..73b226e 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -4,6 +4,8 @@ Bundler.require require "rails-uploader" +require 'carrierwave' +require 'carrierwave/orm/activerecord' module Dummy class Application < Rails::Application diff --git a/spec/dummy/db/migrate/20120508093416_create_assets.rb b/spec/dummy/db/migrate/20120508093416_create_assets.rb new file mode 100644 index 0000000..08dae40 --- /dev/null +++ b/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 diff --git a/spec/dummy/db/migrate/20120508093830_create_articles.rb b/spec/dummy/db/migrate/20120508093830_create_articles.rb new file mode 100644 index 0000000..1418a21 --- /dev/null +++ b/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 diff --git a/spec/dummy/db/test.sqlite3 b/spec/dummy/db/test.sqlite3 index bf6b359..074e42a 100644 Binary files a/spec/dummy/db/test.sqlite3 and b/spec/dummy/db/test.sqlite3 differ diff --git a/spec/factories/articles.rb b/spec/factories/articles.rb new file mode 100644 index 0000000..8ba5160 --- /dev/null +++ b/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 diff --git a/spec/factories/assets.rb b/spec/factories/assets.rb new file mode 100644 index 0000000..ba72d78 --- /dev/null +++ b/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 diff --git a/spec/uploader_spec.rb b/spec/uploader_spec.rb new file mode 100644 index 0000000..3991803 --- /dev/null +++ b/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