carrierwave-aws-record-model 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ spec/support/aws_init.rb
19
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-aws-record-model.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jeremy Green
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Carrierwave::Aws::Record::Model
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'carrierwave-aws-record-model'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install carrierwave-aws-record-model
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave/aws/record/model/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "carrierwave-aws-record-model"
8
+ gem.version = CarrierWave::AWS::Record::Model::VERSION
9
+ gem.authors = ["Jeremy Green"]
10
+ gem.email = ["jeremy@octolabs.com"]
11
+ gem.description = %q{CarrierWave for AWS::Record::Model}
12
+ gem.summary = %q{CarrierWave for AWS::Record::Model}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+
21
+ gem.add_dependency "carrierwave", "~> 0.8.0"
22
+ gem.add_dependency "aws-sdk", "~> 1.8.1.3"
23
+ gem.add_dependency "simple_callbacks", "~> 0.0.2"
24
+ gem.add_development_dependency "rspec", "~> 2.12.0"
25
+ gem.add_development_dependency "debugger"
26
+
27
+ end
@@ -0,0 +1,81 @@
1
+ require "carrierwave/aws/record/model/version"
2
+ require 'carrierwave/validations/active_model'
3
+ require "aws-sdk"
4
+ require 'simple_callbacks'
5
+
6
+
7
+ module CarrierWave
8
+ module AWS
9
+ module Record
10
+ module Model
11
+
12
+ include CarrierWave::Mount
13
+
14
+ ##
15
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
16
+ #
17
+ def mount_uploader(column, uploader=nil, options={}, &block)
18
+ string_attr options[:mount_on] || "#{column}_store".to_sym
19
+ super
20
+
21
+ include CarrierWave::Validations::ActiveModel
22
+ include CarrierWave::AWS::Record::Model::Uploaders
23
+
24
+ #validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
25
+ #validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
26
+ #validates_download_of column if uploader_option(column.to_sym, :validate_download)
27
+
28
+ after_save :"store_#{column}!"
29
+ before_save :"write_#{column}_identifier"
30
+ after_destroy :"remove_#{column}!"
31
+ before_update :"store_previous_model_for_#{column}"
32
+ after_save :"remove_previously_stored_#{column}"
33
+
34
+ class_eval <<-RUBY, __FILE__, __LINE__+1
35
+ def #{column}_changed?
36
+ #{column}_store_changed?
37
+ end
38
+ RUBY
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+
47
+ module CarrierWave::AWS::Record::Model::Uploaders
48
+ def read_uploader(name)
49
+ store_name = "#{name}_store".to_sym
50
+ self[store_name]
51
+ end
52
+
53
+ def write_uploader(name,uploader)
54
+ store_name = "#{name}_store".to_sym
55
+ self[store_name]= uploader
56
+ end
57
+ end
58
+
59
+ # Instance hook methods for the Sequel 3.x
60
+ module CarrierWave::AWS::Record::Model::Hooks
61
+ #def after_save
62
+ #return false if super == false
63
+ #self.class.uploaders.each_key {|column| self.send("store_#{column}!") }
64
+ #end
65
+
66
+ #def before_save
67
+ #return false if super == false
68
+ #self.class.uploaders.each_key {|column| self.send("write_#{column}_identifier") }
69
+ #end
70
+
71
+ #def before_destroy
72
+ #return false if super == false
73
+ #self.class.uploaders.each_key {|column| self.send("remove_#{column}!") }
74
+ #end
75
+ end
76
+
77
+ # Instance validation methods for the Sequel 3.x
78
+ module CarrierWave::AWS::Record::Model::Validations
79
+ end
80
+
81
+ AWS::Record::Model.send(:extend, CarrierWave::AWS::Record::Model)
@@ -0,0 +1,9 @@
1
+ module CarrierWave
2
+ module AWS
3
+ module Record
4
+ module Model
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,200 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe CarrierWave::AWS::Record::Model do
6
+
7
+ let(:klass) { Class.new(AWS::Record::Base) }
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
+ @klass = klass
20
+ @klass.create_domain
21
+ # Avoid the "can't persist empty record" problem
22
+ @klass.string_attr :some_data, :default_value => 'fooled you!'
23
+ end
24
+
25
+ after(:all) do
26
+
27
+ end
28
+
29
+ before(:each) do
30
+ @class = Class.new(AWS::Record::Base)
31
+ @class.create_domain
32
+ # Avoid the "can't persist empty record" problem
33
+ @class.string_attr :some_data, :default_value => 'fooled you!'
34
+ @class.validates_presence_of :some_data
35
+ @class.validates_length_of :some_data, :minimum => 2
36
+ #@class.set_dataset :events
37
+ @class, @uploader, @event = setup_variables_for_class(@class)
38
+ #@class = Class.new(Sequel::Model)
39
+ #@class.set_dataset :events
40
+ #@class, @uploader, @event = setup_variables_for_class(@klass)
41
+ end
42
+
43
+ after(:each) do
44
+ @klass.each{|u| u.destroy }
45
+ #sleep(2)
46
+ end
47
+
48
+ describe '#image' do
49
+
50
+ it "should return blank uploader when nothing has been assigned" do
51
+ @event.image.should be_blank
52
+ end
53
+
54
+ it "should return blank uploader when an empty string has been assigned" do
55
+ @event.image = ''
56
+ @event.save
57
+ #@event.reload
58
+ AWS::SimpleDB.consistent_reads{ @event = @class.find @event.id }
59
+ @event.image.should be_blank
60
+ end
61
+
62
+ it "should retrieve a file from the storage if a value is stored in the database" do
63
+ @event.image = 'test.jpeg'
64
+ @event.save
65
+ #@event.reload
66
+ AWS::SimpleDB.consistent_reads{ @event = @class.find @event.id }
67
+ @event.image.should be_an_instance_of(@uploader)
68
+ end
69
+
70
+ it "should set the path to the store dir" do
71
+ @event.image_store = 'test.jpeg'
72
+ @event.save
73
+ #@event.reload
74
+ AWS::SimpleDB.consistent_reads{ @event = @class.find @event.id }
75
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
76
+ end
77
+
78
+ end
79
+
80
+ describe '#image=' do
81
+
82
+ it "should cache a file" do
83
+ @event.image = stub_file('test.jpeg')
84
+ @event.image.should be_an_instance_of(@uploader)
85
+ end
86
+
87
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
88
+ @event.image.should be_blank
89
+ end
90
+
91
+ it "should copy a file into into the cache directory" do
92
+ @event.image = stub_file('test.jpeg')
93
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
94
+ end
95
+
96
+ it "should do nothing when nil is assigned" do
97
+ @event.image = nil
98
+ @event.image.should be_blank
99
+ end
100
+
101
+ it "should do nothing when an empty string is assigned" do
102
+ @event.image = ''
103
+ @event.image.should be_blank
104
+ end
105
+
106
+ end
107
+
108
+ describe '#save' do
109
+
110
+ it "should do nothing when no file has been assigned" do
111
+ @event.save.should be_true
112
+ @event.image.should be_blank
113
+ end
114
+
115
+ it "should copy the file to the upload directory when a file has been assigned" do
116
+ @event.image = stub_file('test.jpeg')
117
+ @event.save.should be_true
118
+ @event.image.should be_an_instance_of(@uploader)
119
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
120
+ end
121
+
122
+ describe 'with validation' do
123
+
124
+ before do
125
+ @class.class_eval do
126
+ def validate
127
+ puts "calling custom validate"
128
+ errors.add(:image, 'FAIL!')
129
+ end
130
+ end
131
+ # Turn off raising the exceptions on save
132
+ #@event.raise_on_save_failure = false
133
+ end
134
+
135
+ it "should do nothing when a validation fails" do
136
+ @event.image = stub_file('test.jpeg')
137
+ @event.some_data = nil
138
+ @event.should_not be_valid
139
+ @event.save
140
+ @event.id.should be_blank
141
+ @event.image.should be_an_instance_of(@uploader)
142
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
143
+ end
144
+ end
145
+
146
+ it "should assign the filename to the database" do
147
+ @event.image = stub_file('test.jpeg')
148
+ @event.save.should be_true
149
+ #@event.reload
150
+ AWS::SimpleDB.consistent_reads{ @event = @class.find @event.id }
151
+ @event.image_store.should == 'test.jpeg'
152
+ end
153
+
154
+ it "should remove the image if remove_image? returns true" do
155
+ @event.image = stub_file('test.jpeg')
156
+ @event.save
157
+ sleep(1)
158
+ @event.remove_image = true
159
+ @event.save
160
+ #@event.reload
161
+ AWS::SimpleDB.consistent_reads{ @event = @class.find @event.id }
162
+ @event.image.should be_blank
163
+ #@event.image.should == ''
164
+ end
165
+ end
166
+
167
+ describe 'with overriddent filename' do
168
+
169
+ describe '#save' do
170
+
171
+ before do
172
+ @uploader.class_eval do
173
+ def filename
174
+ model.name + File.extname(super)
175
+ end
176
+ end
177
+ @event.stub!(:name).and_return('jonas')
178
+ end
179
+
180
+ it "should copy the file to the upload directory when a file has been assigned" do
181
+ @event.image = stub_file('test.jpeg')
182
+ @event.save.should be_true
183
+ @event.image.should be_an_instance_of(@uploader)
184
+ @event.image.current_path.should == public_path('uploads/jonas.jpeg')
185
+ end
186
+
187
+ it "should assign an overridden filename to the database" do
188
+ @event.image = stub_file('test.jpeg')
189
+ @event.save.should be_true
190
+ #@event.reload
191
+ AWS::SimpleDB.consistent_reads{ @event = @class.find @event.id }
192
+ @event.image_store.should == 'jonas.jpeg'
193
+ end
194
+
195
+ end
196
+
197
+ end
198
+
199
+ end
200
+ end
@@ -0,0 +1 @@
1
+ I'm just a file.
@@ -0,0 +1 @@
1
+ I'm just a file.
@@ -0,0 +1 @@
1
+ I'm just a file.
@@ -0,0 +1,48 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'aws-sdk'
9
+ require 'carrierwave'
10
+ require 'carrierwave/aws/record/model'
11
+ require 'support/aws_init'
12
+
13
+
14
+ def file_path( *paths )
15
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
16
+ end
17
+
18
+ def public_path( *paths )
19
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
20
+ end
21
+
22
+ CarrierWave.root = public_path
23
+
24
+ module CarrierWave
25
+ module Test
26
+ module MockFiles
27
+ def stub_file(filename, mime_type=nil, fake_name=nil)
28
+ f = File.open(file_path(filename))
29
+ return f
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+
36
+ RSpec.configure do |config|
37
+ config.treat_symbols_as_metadata_keys_with_true_values = true
38
+ config.run_all_when_everything_filtered = true
39
+ config.filter_run :focus
40
+
41
+ # Run specs in random order to surface order dependencies. If you find an
42
+ # order dependency and want to debug it, you can fix the order by providing
43
+ # the seed, which is printed after each run.
44
+ # --seed 1234
45
+ config.order = 'random'
46
+
47
+ config.include CarrierWave::Test::MockFiles
48
+ end
@@ -0,0 +1,6 @@
1
+ AWS_SECRET_KEY_ID='abc'
2
+ AWS_SECRET_KEY ='123'
3
+ AWS.config({:access_key_id => AWS_SECRET_KEY_ID, :secret_access_key => AWS_SECRET_KEY})
4
+ AWS::Record.domain_prefix = "carrierwave-aws-model-record-test-"
5
+
6
+
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-aws-record-model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy Green
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: carrierwave
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: aws-sdk
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.1.3
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.1.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: simple_callbacks
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.0.2
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.2
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.12.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.12.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: debugger
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: CarrierWave for AWS::Record::Model
95
+ email:
96
+ - jeremy@octolabs.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - carrierwave-aws-record-model.gemspec
108
+ - lib/carrierwave/aws/record/model.rb
109
+ - lib/carrierwave/aws/record/model/version.rb
110
+ - spec/aws_model_record_spec.rb
111
+ - spec/fixtures/test.jpeg
112
+ - spec/public/uploads/jonas.jpeg
113
+ - spec/public/uploads/test.jpeg
114
+ - spec/spec_helper.rb
115
+ - spec/support/aws_init.rb.example
116
+ homepage: ''
117
+ licenses: []
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.24
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: CarrierWave for AWS::Record::Model
140
+ test_files:
141
+ - spec/aws_model_record_spec.rb
142
+ - spec/fixtures/test.jpeg
143
+ - spec/public/uploads/jonas.jpeg
144
+ - spec/public/uploads/test.jpeg
145
+ - spec/spec_helper.rb
146
+ - spec/support/aws_init.rb.example