carrierwave-sequel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ spec/public
5
+ .rvmrc
6
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in carrierwave-sequel.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ = CarrierWave for Sequel
2
+
3
+ This gem adds support for Sequel to CarrierWave, see the CarrierWave
4
+ documentation for more detailed usage instructions.
5
+
6
+ Install it like this:
7
+
8
+ gem install carrierwave-sequel
9
+
10
+ Use it like this:
11
+
12
+ require 'carrierwave/sequel'
13
+
14
+ Make sure to disable auto_validation on the mounted column.
15
+
16
+ Using bundler:
17
+
18
+ gem 'carrierwave-sequel', :require => 'carrierwave/sequel'
19
+
20
+ This used to be part of CarrierWave but has been extracted.
@@ -0,0 +1,12 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc "Run all examples"
7
+ RSpec::Core::RakeTask.new(:spec) do |t|
8
+ #t.rspec_path = 'bin/rspec'
9
+ t.rspec_opts = %w[--color]
10
+ end
11
+
12
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "carrierwave/sequel/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "carrierwave-sequel"
7
+ s.version = Carrierwave::Sequel::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jonas Nicklas", "Trevor Turk"]
10
+ s.email = ["jonas.nicklas@gmail.com"]
11
+ s.homepage = "https://github.com/jnicklas/carrierwave-sequel"
12
+ s.summary = %q{Sequel support for CarrierWave}
13
+ s.description = %q{Sequel support for CarrierWave}
14
+
15
+ s.rubyforge_project = "carrierwave-sequel"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "carrierwave"
23
+ s.add_dependency "sequel"
24
+ s.add_development_dependency "rspec", ["~> 2.0"]
25
+ s.add_development_dependency "sqlite3"
26
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'sequel'
4
+ require 'carrierwave'
5
+
6
+ module CarrierWave
7
+ module Sequel
8
+ include CarrierWave::Mount
9
+
10
+ def mount_uploader(column, uploader)
11
+ raise "You need to use Sequel 3.0 or higher. Please upgrade." unless ::Sequel::Model.respond_to?(:plugin)
12
+ super
13
+
14
+ alias_method :read_uploader, :[]
15
+ alias_method :write_uploader, :[]=
16
+
17
+ include CarrierWave::Sequel::Hooks
18
+ include CarrierWave::Sequel::Validations
19
+ end
20
+
21
+ end # Sequel
22
+ end # CarrierWave
23
+
24
+ # Instance hook methods for the Sequel 3.x
25
+ module CarrierWave::Sequel::Hooks
26
+ def after_save
27
+ return false if super == false
28
+ self.class.uploaders.each_key {|column| self.send("store_#{column}!") }
29
+ end
30
+
31
+ def before_save
32
+ return false if super == false
33
+ self.class.uploaders.each_key {|column| self.send("write_#{column}_identifier") }
34
+ end
35
+
36
+ def before_destroy
37
+ return false if super == false
38
+ self.class.uploaders.each_key {|column| self.send("remove_#{column}!") }
39
+ end
40
+ end
41
+
42
+ # Instance validation methods for the Sequel 3.x
43
+ module CarrierWave::Sequel::Validations
44
+ end
45
+
46
+ Sequel::Model.send(:extend, CarrierWave::Sequel)
@@ -0,0 +1,5 @@
1
+ module Carrierwave
2
+ module Sequel
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ this is stuff
@@ -0,0 +1,181 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ DB = Sequel.sqlite
6
+
7
+ describe CarrierWave::Sequel do
8
+
9
+ def setup_variables_for_class(klass)
10
+ uploader = Class.new(CarrierWave::Uploader::Base)
11
+ klass.mount_uploader(:image, uploader)
12
+ model = klass.new
13
+ [klass, uploader, model]
14
+ end
15
+
16
+ describe '.mount_uploader' do
17
+
18
+ before(:all) do
19
+ DB.create_table :events do
20
+ primary_key :id
21
+ column :image, :string
22
+ column :textfile, :string
23
+ end
24
+ end
25
+
26
+ after(:all) do
27
+ DB.drop_table :events
28
+ end
29
+
30
+ before(:each) do
31
+ @class = Class.new(Sequel::Model)
32
+ @class.set_dataset :events
33
+ @class, @uploader, @event = setup_variables_for_class(@class)
34
+ end
35
+
36
+ after(:each) { @class.delete }
37
+
38
+ describe '#image' do
39
+
40
+ it "should return blank uploader when nothing has been assigned" do
41
+ @event.image.should be_blank
42
+ end
43
+
44
+ it "should return blank uploader when an empty string has been assigned" do
45
+ @event[:image] = ''
46
+ @event.save
47
+ @event.reload
48
+ @event.image.should be_blank
49
+ end
50
+
51
+ it "should retrieve a file from the storage if a value is stored in the database" do
52
+ @event[:image] = 'test.jpeg'
53
+ @event.save
54
+ @event.reload
55
+ @event.image.should be_an_instance_of(@uploader)
56
+ end
57
+
58
+ it "should set the path to the store dir" do
59
+ @event[:image] = 'test.jpeg'
60
+ @event.save
61
+ @event.reload
62
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
63
+ end
64
+
65
+ end
66
+
67
+ describe '#image=' do
68
+
69
+ it "should cache a file" do
70
+ @event.image = stub_file('test.jpeg')
71
+ @event.image.should be_an_instance_of(@uploader)
72
+ end
73
+
74
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
75
+ @event[:image].should be_nil
76
+ end
77
+
78
+ it "should copy a file into into the cache directory" do
79
+ @event.image = stub_file('test.jpeg')
80
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
81
+ end
82
+
83
+ it "should do nothing when nil is assigned" do
84
+ @event.image = nil
85
+ @event.image.should be_blank
86
+ end
87
+
88
+ it "should do nothing when an empty string is assigned" do
89
+ @event.image = ''
90
+ @event.image.should be_blank
91
+ end
92
+
93
+ end
94
+
95
+ describe '#save' do
96
+
97
+ it "should do nothing when no file has been assigned" do
98
+ @event.save.should be_true
99
+ @event.image.should be_blank
100
+ end
101
+
102
+ it "should copy the file to the upload directory when a file has been assigned" do
103
+ @event.image = stub_file('test.jpeg')
104
+ @event.save.should be_true
105
+ @event.image.should be_an_instance_of(@uploader)
106
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
107
+ end
108
+
109
+ describe 'with validation' do
110
+
111
+ before do
112
+ @class.class_eval do
113
+ def validate
114
+ errors.add(:image, 'FAIL!')
115
+ end
116
+ end
117
+ # Turn off raising the exceptions on save
118
+ @event.raise_on_save_failure = false
119
+ end
120
+
121
+ it "should do nothing when a validation fails" do
122
+ @event.image = stub_file('test.jpeg')
123
+ @event.should_not be_valid
124
+ @event.save
125
+ @event.should be_new
126
+ @event.image.should be_an_instance_of(@uploader)
127
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
128
+ end
129
+ end
130
+
131
+ it "should assign the filename to the database" do
132
+ @event.image = stub_file('test.jpeg')
133
+ @event.save.should be_true
134
+ @event.reload
135
+ @event[:image].should == 'test.jpeg'
136
+ end
137
+
138
+ it "should remove the image if remove_image? returns true" do
139
+ @event.image = stub_file('test.jpeg')
140
+ @event.save
141
+ @event.remove_image = true
142
+ @event.save
143
+ @event.reload
144
+ @event.image.should be_blank
145
+ @event[:image].should == ''
146
+ end
147
+ end
148
+
149
+ describe 'with overriddent filename' do
150
+
151
+ describe '#save' do
152
+
153
+ before do
154
+ @uploader.class_eval do
155
+ def filename
156
+ model.name + File.extname(super)
157
+ end
158
+ end
159
+ @event.stub!(:name).and_return('jonas')
160
+ end
161
+
162
+ it "should copy the file to the upload directory when a file has been assigned" do
163
+ @event.image = stub_file('test.jpeg')
164
+ @event.save.should be_true
165
+ @event.image.should be_an_instance_of(@uploader)
166
+ @event.image.current_path.should == public_path('uploads/jonas.jpeg')
167
+ end
168
+
169
+ it "should assign an overridden filename to the database" do
170
+ @event.image = stub_file('test.jpeg')
171
+ @event.save.should be_true
172
+ @event.reload
173
+ @event[:image].should == 'jonas.jpeg'
174
+ end
175
+
176
+ end
177
+
178
+ end
179
+
180
+ end
181
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+
5
+ require 'carrierwave'
6
+ require 'carrierwave/sequel'
7
+
8
+ def file_path( *paths )
9
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
10
+ end
11
+
12
+ def public_path( *paths )
13
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
14
+ end
15
+
16
+ CarrierWave.root = public_path
17
+
18
+ module CarrierWave
19
+ module Test
20
+ module MockFiles
21
+ def stub_file(filename, mime_type=nil, fake_name=nil)
22
+ f = File.open(file_path(filename))
23
+ return f
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ RSpec.configure do |config|
30
+ config.include CarrierWave::Test::MockFiles
31
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-sequel
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Jonas Nicklas
13
+ - Trevor Turk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-18 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: carrierwave
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: sequel
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 2
57
+ - 0
58
+ version: "2.0"
59
+ type: :development
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: sqlite3
63
+ prerelease: false
64
+ requirement: &id004 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ type: :development
73
+ version_requirements: *id004
74
+ description: Sequel support for CarrierWave
75
+ email:
76
+ - jonas.nicklas@gmail.com
77
+ executables: []
78
+
79
+ extensions: []
80
+
81
+ extra_rdoc_files: []
82
+
83
+ files:
84
+ - .gitignore
85
+ - Gemfile
86
+ - README.rdoc
87
+ - Rakefile
88
+ - carrierwave-sequel.gemspec
89
+ - lib/carrierwave/sequel.rb
90
+ - lib/carrierwave/sequel/version.rb
91
+ - spec/fixtures/test.jpeg
92
+ - spec/sequel_spec.rb
93
+ - spec/spec_helper.rb
94
+ has_rdoc: true
95
+ homepage: https://github.com/jnicklas/carrierwave-sequel
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ requirements: []
120
+
121
+ rubyforge_project: carrierwave-sequel
122
+ rubygems_version: 1.3.7
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Sequel support for CarrierWave
126
+ test_files:
127
+ - spec/fixtures/test.jpeg
128
+ - spec/sequel_spec.rb
129
+ - spec/spec_helper.rb