Permalink
Browse files

Extracted from CarrierWave

  • Loading branch information...
0 parents commit 45cdcbb6cdabb942de4ed2d6ea19b1ae18a489e8 @jnicklas jnicklas committed May 18, 2011
Showing with 332 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +4 −0 Gemfile
  3. +20 −0 README.rdoc
  4. +12 −0 Rakefile
  5. +26 −0 carrierwave-sequel.gemspec
  6. +46 −0 lib/carrierwave/sequel.rb
  7. +5 −0 lib/carrierwave/sequel/version.rb
  8. +1 −0 spec/fixtures/test.jpeg
  9. +181 −0 spec/sequel_spec.rb
  10. +31 −0 spec/spec_helper.rb
@@ -0,0 +1,6 @@
+pkg/*
+*.gem
+.bundle
+spec/public
+.rvmrc
+Gemfile.lock
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in carrierwave-sequel.gemspec
+gemspec
@@ -0,0 +1,20 @@
+= CarrierWave for Sequel
+
+This gem adds support for Sequel to CarrierWave, see the CarrierWave
+documentation for more detailed usage instructions.
+
+Install it like this:
+
+ gem install carrierwave-sequel
+
+Use it like this:
+
+ require 'carrierwave/sequel'
+
+Make sure to disable auto_validation on the mounted column.
+
+Using bundler:
+
+ gem 'carrierwave-sequel', :require => 'carrierwave/sequel'
+
+This used to be part of CarrierWave but has been extracted.
@@ -0,0 +1,12 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+
+desc "Run all examples"
+RSpec::Core::RakeTask.new(:spec) do |t|
+ #t.rspec_path = 'bin/rspec'
+ t.rspec_opts = %w[--color]
+end
+
+task :default => :spec
@@ -0,0 +1,26 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "carrierwave/sequel/version"
+
+Gem::Specification.new do |s|
+ s.name = "carrierwave-sequel"
+ s.version = Carrierwave::Sequel::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Jonas Nicklas", "Trevor Turk"]
+ s.email = ["jonas.nicklas@gmail.com"]
+ s.homepage = "https://github.com/jnicklas/carrierwave-sequel"
+ s.summary = %q{Sequel support for CarrierWave}
+ s.description = %q{Sequel support for CarrierWave}
+
+ s.rubyforge_project = "carrierwave-sequel"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency "carrierwave"
+ s.add_dependency "sequel"
+ s.add_development_dependency "rspec", ["~> 2.0"]
+ s.add_development_dependency "sqlite3"
+end
@@ -0,0 +1,46 @@
+# encoding: utf-8
+
+require 'sequel'
+require 'carrierwave'
+
+module CarrierWave
+ module Sequel
+ include CarrierWave::Mount
+
+ def mount_uploader(column, uploader)
+ raise "You need to use Sequel 3.0 or higher. Please upgrade." unless ::Sequel::Model.respond_to?(:plugin)
+ super
+
+ alias_method :read_uploader, :[]
+ alias_method :write_uploader, :[]=
+
+ include CarrierWave::Sequel::Hooks
+ include CarrierWave::Sequel::Validations
+ end
+
+ end # Sequel
+end # CarrierWave
+
+# Instance hook methods for the Sequel 3.x
+module CarrierWave::Sequel::Hooks
+ def after_save
+ return false if super == false
+ self.class.uploaders.each_key {|column| self.send("store_#{column}!") }
+ end
+
+ def before_save
+ return false if super == false
+ self.class.uploaders.each_key {|column| self.send("write_#{column}_identifier") }
+ end
+
+ def before_destroy
+ return false if super == false
+ self.class.uploaders.each_key {|column| self.send("remove_#{column}!") }
+ end
+end
+
+# Instance validation methods for the Sequel 3.x
+module CarrierWave::Sequel::Validations
+end
+
+Sequel::Model.send(:extend, CarrierWave::Sequel)
@@ -0,0 +1,5 @@
+module Carrierwave
+ module Sequel
+ VERSION = "0.1.0"
+ end
+end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,181 @@
+# encoding: utf-8
+
+require 'spec_helper'
+
+DB = Sequel.sqlite
+
+describe CarrierWave::Sequel do
+
+ def setup_variables_for_class(klass)
+ uploader = Class.new(CarrierWave::Uploader::Base)
+ klass.mount_uploader(:image, uploader)
+ model = klass.new
+ [klass, uploader, model]
+ end
+
+ describe '.mount_uploader' do
+
+ before(:all) do
+ DB.create_table :events do
+ primary_key :id
+ column :image, :string
+ column :textfile, :string
+ end
+ end
+
+ after(:all) do
+ DB.drop_table :events
+ end
+
+ before(:each) do
+ @class = Class.new(Sequel::Model)
+ @class.set_dataset :events
+ @class, @uploader, @event = setup_variables_for_class(@class)
+ end
+
+ after(:each) { @class.delete }
+
+ describe '#image' do
+
+ it "should return blank uploader when nothing has been assigned" do
+ @event.image.should be_blank
+ end
+
+ it "should return blank uploader when an empty string has been assigned" do
+ @event[:image] = ''
+ @event.save
+ @event.reload
+ @event.image.should be_blank
+ end
+
+ it "should retrieve a file from the storage if a value is stored in the database" do
+ @event[:image] = 'test.jpeg'
+ @event.save
+ @event.reload
+ @event.image.should be_an_instance_of(@uploader)
+ end
+
+ it "should set the path to the store dir" do
+ @event[:image] = 'test.jpeg'
+ @event.save
+ @event.reload
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
+ end
+
+ end
+
+ describe '#image=' do
+
+ it "should cache a file" do
+ @event.image = stub_file('test.jpeg')
+ @event.image.should be_an_instance_of(@uploader)
+ end
+
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
+ @event[:image].should be_nil
+ end
+
+ it "should copy a file into into the cache directory" do
+ @event.image = stub_file('test.jpeg')
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
+ end
+
+ it "should do nothing when nil is assigned" do
+ @event.image = nil
+ @event.image.should be_blank
+ end
+
+ it "should do nothing when an empty string is assigned" do
+ @event.image = ''
+ @event.image.should be_blank
+ end
+
+ end
+
+ describe '#save' do
+
+ it "should do nothing when no file has been assigned" do
+ @event.save.should be_true
+ @event.image.should be_blank
+ end
+
+ it "should copy the file to the upload directory when a file has been assigned" do
+ @event.image = stub_file('test.jpeg')
+ @event.save.should be_true
+ @event.image.should be_an_instance_of(@uploader)
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
+ end
+
+ describe 'with validation' do
+
+ before do
+ @class.class_eval do
+ def validate
+ errors.add(:image, 'FAIL!')
+ end
+ end
+ # Turn off raising the exceptions on save
+ @event.raise_on_save_failure = false
+ end
+
+ it "should do nothing when a validation fails" do
+ @event.image = stub_file('test.jpeg')
+ @event.should_not be_valid
+ @event.save
+ @event.should be_new
+ @event.image.should be_an_instance_of(@uploader)
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
+ end
+ end
+
+ it "should assign the filename to the database" do
+ @event.image = stub_file('test.jpeg')
+ @event.save.should be_true
+ @event.reload
+ @event[:image].should == 'test.jpeg'
+ end
+
+ it "should remove the image if remove_image? returns true" do
+ @event.image = stub_file('test.jpeg')
+ @event.save
+ @event.remove_image = true
+ @event.save
+ @event.reload
+ @event.image.should be_blank
+ @event[:image].should == ''
+ end
+ end
+
+ describe 'with overriddent filename' do
+
+ describe '#save' do
+
+ before do
+ @uploader.class_eval do
+ def filename
+ model.name + File.extname(super)
+ end
+ end
+ @event.stub!(:name).and_return('jonas')
+ end
+
+ it "should copy the file to the upload directory when a file has been assigned" do
+ @event.image = stub_file('test.jpeg')
+ @event.save.should be_true
+ @event.image.should be_an_instance_of(@uploader)
+ @event.image.current_path.should == public_path('uploads/jonas.jpeg')
+ end
+
+ it "should assign an overridden filename to the database" do
+ @event.image = stub_file('test.jpeg')
+ @event.save.should be_true
+ @event.reload
+ @event[:image].should == 'jonas.jpeg'
+ end
+
+ end
+
+ end
+
+ end
+end
@@ -0,0 +1,31 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'rspec'
+
+require 'carrierwave'
+require 'carrierwave/sequel'
+
+def file_path( *paths )
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
+end
+
+def public_path( *paths )
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
+end
+
+CarrierWave.root = public_path
+
+module CarrierWave
+ module Test
+ module MockFiles
+ def stub_file(filename, mime_type=nil, fake_name=nil)
+ f = File.open(file_path(filename))
+ return f
+ end
+ end
+ end
+end
+
+RSpec.configure do |config|
+ config.include CarrierWave::Test::MockFiles
+end

0 comments on commit 45cdcbb

Please sign in to comment.