carrierwave-datamapper 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-datamapper.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ = CarrierWave for Datamapper
2
+
3
+ This gem adds support for DataMapper to CarrierWave, see the CarrierWave
4
+ documentation for more detailed usage instructions.
5
+
6
+ Install it like this:
7
+
8
+ gem install carrierwave-datamapper
9
+
10
+ Use it like this:
11
+
12
+ require 'carrierwave/datamapper'
13
+
14
+ Make sure to disable auto_validation on the mounted column.
15
+
16
+ Using bundler:
17
+
18
+ gem 'carrierwave-datamapper', :require => 'carrierwave/datamapper'
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,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "carrierwave/datamapper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "carrierwave-datamapper"
7
+ s.version = Carrierwave::Datamapper::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-datamapper"
12
+ s.summary = %q{Datamapper support for CarrierWave}
13
+ s.description = %q{Datamapper support for CarrierWave}
14
+
15
+ s.rubyforge_project = "carrierwave-datamapper"
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 "dm-core", ["~> 1.0"]
23
+ s.add_dependency "carrierwave"
24
+ s.add_development_dependency "rspec", ["~> 2.0"]
25
+ s.add_development_dependency "dm-validations", ["~> 1.0"]
26
+ s.add_development_dependency "dm-migrations", ["~> 1.0"]
27
+ s.add_development_dependency "dm-sqlite-adapter", ["~> 1.0"]
28
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require 'dm-core'
4
+
5
+ module CarrierWave
6
+ module DataMapper
7
+
8
+ include CarrierWave::Mount
9
+
10
+ ##
11
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
12
+ #
13
+ def mount_uploader(column, uploader, options={}, &block)
14
+ super
15
+
16
+ alias_method :read_uploader, :attribute_get
17
+ alias_method :write_uploader, :attribute_set
18
+ after :save, "store_#{column}!".to_sym
19
+ pre_hook = ::DataMapper.const_defined?(:Validate) ? :valid? : :save
20
+ before pre_hook, "write_#{column}_identifier".to_sym
21
+ after :destroy, "remove_#{column}!".to_sym
22
+
23
+ # FIXME: Hack to work around Datamapper not triggering callbacks
24
+ # for objects that are not dirty. By explicitly calling
25
+ # attribute_set we are marking the record as dirty.
26
+ class_eval <<-RUBY
27
+ def remove_#{column}=(value)
28
+ _mounter(:#{column}).remove = value
29
+ attribute_set(:#{column}, '') if _mounter(:#{column}).remove?
30
+ end
31
+ RUBY
32
+ end
33
+
34
+ end # DataMapper
35
+ end # CarrierWave
36
+
37
+ DataMapper::Model.send(:include, CarrierWave::DataMapper)
@@ -0,0 +1,5 @@
1
+ module Carrierwave
2
+ module Datamapper
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,168 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ require 'dm-core'
6
+ require 'dm-validations'
7
+ require 'dm-migrations'
8
+
9
+ DataMapper.setup(:default, 'sqlite3::memory:')
10
+
11
+ describe CarrierWave::DataMapper do
12
+
13
+ before do
14
+ uploader = Class.new(CarrierWave::Uploader::Base)
15
+
16
+ @class = Class.new
17
+ @class.class_eval do
18
+ include DataMapper::Resource
19
+
20
+ storage_names[:default] = 'events'
21
+
22
+ property :id, DataMapper::Property::Serial
23
+
24
+ # auto validation off currently for inferred type
25
+ # validation. issue is that validations are done on
26
+ # the value returned by model#property_name which is overloaded
27
+ # by carrierwave. pull request in @dm-more to remedy this.
28
+ property :image, String, :auto_validation => false
29
+
30
+ mount_uploader :image, uploader
31
+ end
32
+
33
+ @class.auto_migrate!
34
+
35
+ @uploader = uploader
36
+
37
+ @event = @class.new
38
+ end
39
+
40
+ describe '#image' do
41
+
42
+ it "should return blank uploader when nothing has been assigned" do
43
+ @event.image.should be_blank
44
+ end
45
+
46
+ it "should return blank uploader when an empty string has been assigned" do
47
+ repository(:default).adapter.execute("INSERT INTO events (image) VALUES ('')")
48
+ @event = @class.first
49
+
50
+ @event.image.should be_blank
51
+ end
52
+
53
+ it "should retrieve a file from the storage if a value is stored in the database" do
54
+ repository(:default).adapter.execute("INSERT INTO events (image) VALUES ('test.jpg')")
55
+ @event = @class.first
56
+
57
+ @event.save
58
+ @event.reload
59
+ @event.image.should be_an_instance_of(@uploader)
60
+ end
61
+
62
+ it "should set the path to the store dir" do
63
+ @event.attribute_set(:image, 'test.jpeg')
64
+ @event.save
65
+ @event.reload
66
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
67
+ end
68
+
69
+ end
70
+
71
+ describe '#image=' do
72
+
73
+ it "should cache a file" do
74
+ @event.image = stub_file('test.jpeg')
75
+ @event.image.should be_an_instance_of(@uploader)
76
+ end
77
+
78
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
79
+ @event.attribute_get(:image).should be_nil
80
+ end
81
+
82
+ it "should copy a file into into the cache directory" do
83
+ @event.image = stub_file('test.jpeg')
84
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
85
+ end
86
+
87
+ it "should do nothing when nil is assigned" do
88
+ @event.image = nil
89
+ @event.image.should be_blank
90
+ end
91
+
92
+ it "should do nothing when an empty string is assigned" do
93
+ @event.image = ''
94
+ @event.image.should be_blank
95
+ end
96
+
97
+ end
98
+
99
+ describe '#save' do
100
+
101
+ it "should do nothing when no file has been assigned" do
102
+ @event.save
103
+ @event.image.should be_blank
104
+ end
105
+
106
+ it "should copy the file to the upload directory when a file has been assigned" do
107
+ @event.image = stub_file('test.jpeg')
108
+ @event.save
109
+ @event.image.should be_an_instance_of(@uploader)
110
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
111
+ end
112
+
113
+ it "should assign the filename to the database" do
114
+ @event.image = stub_file('test.jpeg')
115
+ @event.save
116
+ @event.reload
117
+ @event.attribute_get(:image).should == 'test.jpeg'
118
+ end
119
+
120
+ it "should remove the image if remove_image? returns true" do
121
+ @event.image = stub_file('test.jpeg')
122
+ @event.save
123
+ @event.remove_image = true
124
+ @event.save
125
+ @event.reload
126
+ @event.image.should be_blank
127
+ @event.attribute_get(:image).should == ''
128
+ end
129
+
130
+ describe "with validations" do
131
+ it "should do nothing when a validation fails" do
132
+ @class.validates_with_block(:textfile) { [false, "FAIL!"] }
133
+ @event.image = stub_file('test.jpeg')
134
+ @event.save
135
+ @event.image.should be_an_instance_of(@uploader)
136
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
137
+ end
138
+
139
+ it "should assign the filename before validation" do
140
+ @class.validates_with_block(:image) { [false, "FAIL!"] if self.image.nil? }
141
+ @event.image = stub_file('test.jpeg')
142
+ @event.save
143
+ @event.reload
144
+ @event.attribute_get(:image).should == 'test.jpeg'
145
+ end
146
+ end
147
+
148
+ end
149
+
150
+ describe '#destroy' do
151
+
152
+ it "should do nothing when no file has been assigned" do
153
+ @event.destroy
154
+ end
155
+
156
+ it "should remove the file from the filesystem" do
157
+ @event.image = stub_file('test.jpeg')
158
+ @event.save.should be_true
159
+ File.exist?(public_path('uploads/test.jpeg')).should be_true
160
+ @event.image.should be_an_instance_of(@uploader)
161
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
162
+ @event.destroy
163
+ File.exist?(public_path('uploads/test.jpeg')).should be_false
164
+ end
165
+
166
+ end
167
+
168
+ end
@@ -0,0 +1 @@
1
+ this is stuff
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+
5
+ require 'carrierwave'
6
+ require 'carrierwave/datamapper'
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,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-datamapper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jonas Nicklas
14
+ - Trevor Turk
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-05-18 00:00:00 -04:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: dm-core
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 15
31
+ segments:
32
+ - 1
33
+ - 0
34
+ version: "1.0"
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: carrierwave
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rspec
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 2
62
+ - 0
63
+ version: "2.0"
64
+ type: :development
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: dm-validations
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 15
75
+ segments:
76
+ - 1
77
+ - 0
78
+ version: "1.0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: dm-migrations
83
+ prerelease: false
84
+ requirement: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ hash: 15
90
+ segments:
91
+ - 1
92
+ - 0
93
+ version: "1.0"
94
+ type: :development
95
+ version_requirements: *id005
96
+ - !ruby/object:Gem::Dependency
97
+ name: dm-sqlite-adapter
98
+ prerelease: false
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ~>
103
+ - !ruby/object:Gem::Version
104
+ hash: 15
105
+ segments:
106
+ - 1
107
+ - 0
108
+ version: "1.0"
109
+ type: :development
110
+ version_requirements: *id006
111
+ description: Datamapper support for CarrierWave
112
+ email:
113
+ - jonas.nicklas@gmail.com
114
+ executables: []
115
+
116
+ extensions: []
117
+
118
+ extra_rdoc_files: []
119
+
120
+ files:
121
+ - .gitignore
122
+ - Gemfile
123
+ - README.rdoc
124
+ - Rakefile
125
+ - carrierwave-datamapper.gemspec
126
+ - lib/carrierwave/datamapper.rb
127
+ - lib/carrierwave/datamapper/version.rb
128
+ - spec/datamapper_spec.rb
129
+ - spec/fixtures/test.jpeg
130
+ - spec/spec_helper.rb
131
+ has_rdoc: true
132
+ homepage: https://github.com/jnicklas/carrierwave-datamapper
133
+ licenses: []
134
+
135
+ post_install_message:
136
+ rdoc_options: []
137
+
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ hash: 3
155
+ segments:
156
+ - 0
157
+ version: "0"
158
+ requirements: []
159
+
160
+ rubyforge_project: carrierwave-datamapper
161
+ rubygems_version: 1.3.7
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: Datamapper support for CarrierWave
165
+ test_files:
166
+ - spec/datamapper_spec.rb
167
+ - spec/fixtures/test.jpeg
168
+ - spec/spec_helper.rb