carrierwave-activerecord-store-in-model 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +4 -0
- data/Gemfile +22 -0
- data/Gemfile.lock +140 -0
- data/LICENSE.txt +20 -0
- data/README.md +0 -0
- data/Rakefile +2 -0
- data/carrierwave-activerecord-store-in-model.gemspec +33 -0
- data/db/.gitkeep +0 -0
- data/lib/carrierwave-activerecord-store-in-model.rb +26 -0
- data/lib/carrierwave-activerecord-store-in-model/storage/active_record_file.rb +23 -0
- data/lib/carrierwave-activerecord-store-in-model/storage/file.rb +74 -0
- data/lib/carrierwave-activerecord-store-in-model/storage/storage_provider.rb +70 -0
- data/lib/carrierwave-activerecord-store-in-model/storage/version.rb +6 -0
- data/lib/generators/carrierwave_activerecord_generator.rb +19 -0
- data/lib/generators/templates/create_carrierwave_files.rb +12 -0
- data/lib/locale/nl.yml +2 -0
- data/spec/carrierwave-activerecord-store-in-model_spec.rb +50 -0
- data/spec/factories.rb +7 -0
- data/spec/fixtures/images/Curacao-Airport-2010.jpg +0 -0
- data/spec/fixtures/images/Curacao-Artis-2009.jpg +0 -0
- data/spec/fixtures/images/Curacao-Punda-1956.jpg +0 -0
- data/spec/fixtures/images/license +5 -0
- data/spec/spec_helper.rb +75 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NTBiNDk0NTQ1OTViNDFlYjFmM2Y1MzQ3YTgxMmM2MjVmNWE1MTM4Yw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NGY1YjA1ZWE3ZjNiNDM4NWQ0ZmMyMDQwZmYwZTYwYjljMzE0ZWJhMw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YmQ5YjUyN2FjNTc1NmNjOThkYjUxMzBkZjBkYzdkMTMwM2MxMzAwN2VhNDQz
|
10
|
+
ZGNkOWMzOWU5YzU5OWQ4OWQ4ZGEwMzBiNmI5NGYxMTA5NGRlMGQ4YzRjNWUz
|
11
|
+
ZmViNDJjMDc0MmI0YjAzZjMzNzk2MzZkZjZjOWQ1MjE2NTdiNjc=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZjZiZjI4NzE3Y2NiZTQyNTkzYjRhMDczZDhjMTU2ZjUwYmIyYWY5ZGMzZmFj
|
14
|
+
MDM1OWEwMjkwZWEwZGZhMzkyOTg5MWM3NGUyOWY5YmIzNGZlOGVmZTE5NGEx
|
15
|
+
YWY0MGU3N2NkNmI0NGQ5ZGM4ZjQ2NzZhM2U1N2IwOWEyMjBhNjI=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#source "http://rubygems.org"
|
2
|
+
#gem "factory_girl_rails", "~> 4.0"
|
3
|
+
#gem 'colored'
|
4
|
+
#gem 'protected_attributes'
|
5
|
+
#gemspec
|
6
|
+
|
7
|
+
source :rubygems
|
8
|
+
|
9
|
+
gemspec # See: carrierwave-activerecord.gemspec
|
10
|
+
|
11
|
+
# Helpful development gems which are not development "dependencies".
|
12
|
+
group :development do
|
13
|
+
gem 'guard-rspec'
|
14
|
+
gem 'rb-fsevent', :require => false
|
15
|
+
gem 'rb-inotify', :require => false
|
16
|
+
gem 'rb-fchange', :require => false
|
17
|
+
gem 'simplecov'
|
18
|
+
end
|
19
|
+
group :test do
|
20
|
+
gem 'colored'
|
21
|
+
gem "factory_girl_rails", "~> 4.0"
|
22
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
carrierwave-activerecord-store-in-model (0.8.0)
|
5
|
+
activerecord (~> 3.2.0)
|
6
|
+
carrierwave (~> 0.8.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionpack (3.2.17)
|
12
|
+
activemodel (= 3.2.17)
|
13
|
+
activesupport (= 3.2.17)
|
14
|
+
builder (~> 3.0.0)
|
15
|
+
erubis (~> 2.7.0)
|
16
|
+
journey (~> 1.0.4)
|
17
|
+
rack (~> 1.4.5)
|
18
|
+
rack-cache (~> 1.2)
|
19
|
+
rack-test (~> 0.6.1)
|
20
|
+
sprockets (~> 2.2.1)
|
21
|
+
activemodel (3.2.17)
|
22
|
+
activesupport (= 3.2.17)
|
23
|
+
builder (~> 3.0.0)
|
24
|
+
activerecord (3.2.17)
|
25
|
+
activemodel (= 3.2.17)
|
26
|
+
activesupport (= 3.2.17)
|
27
|
+
arel (~> 3.0.2)
|
28
|
+
tzinfo (~> 0.3.29)
|
29
|
+
activesupport (3.2.17)
|
30
|
+
i18n (~> 0.6, >= 0.6.4)
|
31
|
+
multi_json (~> 1.0)
|
32
|
+
arel (3.0.3)
|
33
|
+
builder (3.0.4)
|
34
|
+
carrierwave (0.8.0)
|
35
|
+
activemodel (>= 3.2.0)
|
36
|
+
activesupport (>= 3.2.0)
|
37
|
+
celluloid (0.15.2)
|
38
|
+
timers (~> 1.1.0)
|
39
|
+
celluloid-io (0.15.0)
|
40
|
+
celluloid (>= 0.15.0)
|
41
|
+
nio4r (>= 0.5.0)
|
42
|
+
coderay (1.1.0)
|
43
|
+
colored (1.2)
|
44
|
+
diff-lcs (1.2.5)
|
45
|
+
docile (1.1.3)
|
46
|
+
erubis (2.7.0)
|
47
|
+
factory_girl (4.4.0)
|
48
|
+
activesupport (>= 3.0.0)
|
49
|
+
factory_girl_rails (4.4.1)
|
50
|
+
factory_girl (~> 4.4.0)
|
51
|
+
railties (>= 3.0.0)
|
52
|
+
ffi (1.9.3)
|
53
|
+
formatador (0.2.4)
|
54
|
+
guard (2.6.0)
|
55
|
+
formatador (>= 0.2.4)
|
56
|
+
listen (~> 2.7)
|
57
|
+
lumberjack (~> 1.0)
|
58
|
+
pry (>= 0.9.12)
|
59
|
+
thor (>= 0.18.1)
|
60
|
+
guard-rspec (4.2.8)
|
61
|
+
guard (~> 2.1)
|
62
|
+
rspec (>= 2.14, < 4.0)
|
63
|
+
hike (1.2.3)
|
64
|
+
i18n (0.6.9)
|
65
|
+
journey (1.0.4)
|
66
|
+
json (1.8.1)
|
67
|
+
listen (2.7.1)
|
68
|
+
celluloid (>= 0.15.2)
|
69
|
+
celluloid-io (>= 0.15.0)
|
70
|
+
rb-fsevent (>= 0.9.3)
|
71
|
+
rb-inotify (>= 0.9)
|
72
|
+
lumberjack (1.0.5)
|
73
|
+
method_source (0.8.2)
|
74
|
+
multi_json (1.9.2)
|
75
|
+
nio4r (1.0.0)
|
76
|
+
pry (0.9.12.6)
|
77
|
+
coderay (~> 1.0)
|
78
|
+
method_source (~> 0.8)
|
79
|
+
slop (~> 3.4)
|
80
|
+
rack (1.4.5)
|
81
|
+
rack-cache (1.2)
|
82
|
+
rack (>= 0.4)
|
83
|
+
rack-ssl (1.3.4)
|
84
|
+
rack
|
85
|
+
rack-test (0.6.2)
|
86
|
+
rack (>= 1.0)
|
87
|
+
railties (3.2.17)
|
88
|
+
actionpack (= 3.2.17)
|
89
|
+
activesupport (= 3.2.17)
|
90
|
+
rack-ssl (~> 1.3.2)
|
91
|
+
rake (>= 0.8.7)
|
92
|
+
rdoc (~> 3.4)
|
93
|
+
thor (>= 0.14.6, < 2.0)
|
94
|
+
rake (10.3.1)
|
95
|
+
rb-fchange (0.0.6)
|
96
|
+
ffi
|
97
|
+
rb-fsevent (0.9.4)
|
98
|
+
rb-inotify (0.9.3)
|
99
|
+
ffi (>= 0.5.0)
|
100
|
+
rdoc (3.12.2)
|
101
|
+
json (~> 1.4)
|
102
|
+
rspec (2.14.1)
|
103
|
+
rspec-core (~> 2.14.0)
|
104
|
+
rspec-expectations (~> 2.14.0)
|
105
|
+
rspec-mocks (~> 2.14.0)
|
106
|
+
rspec-core (2.14.8)
|
107
|
+
rspec-expectations (2.14.5)
|
108
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
109
|
+
rspec-mocks (2.14.6)
|
110
|
+
simplecov (0.8.2)
|
111
|
+
docile (~> 1.1.0)
|
112
|
+
multi_json
|
113
|
+
simplecov-html (~> 0.8.0)
|
114
|
+
simplecov-html (0.8.0)
|
115
|
+
slop (3.5.0)
|
116
|
+
sprockets (2.2.2)
|
117
|
+
hike (~> 1.2)
|
118
|
+
multi_json (~> 1.0)
|
119
|
+
rack (~> 1.0)
|
120
|
+
tilt (~> 1.1, != 1.3.0)
|
121
|
+
sqlite3 (1.3.9)
|
122
|
+
thor (0.19.1)
|
123
|
+
tilt (1.4.1)
|
124
|
+
timers (1.1.0)
|
125
|
+
tzinfo (0.3.39)
|
126
|
+
|
127
|
+
PLATFORMS
|
128
|
+
ruby
|
129
|
+
|
130
|
+
DEPENDENCIES
|
131
|
+
carrierwave-activerecord-store-in-model!
|
132
|
+
colored
|
133
|
+
factory_girl_rails (~> 4.0)
|
134
|
+
guard-rspec
|
135
|
+
rb-fchange
|
136
|
+
rb-fsevent
|
137
|
+
rb-inotify
|
138
|
+
rspec (~> 2.12)
|
139
|
+
simplecov
|
140
|
+
sqlite3 (~> 1.3)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Ace Suares
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
|
4
|
+
require 'carrierwave-activerecord-store-in-model/storage/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "carrierwave-activerecord-store-in-model"
|
8
|
+
gem.version = CarrierWave::ActiverecordStoreInModel::VERSION
|
9
|
+
gem.authors = ["Ace Suares,", "Royendgel Silberie"]
|
10
|
+
gem.email = ["ace@suares.com", "royendgel@gmail.com"]
|
11
|
+
gem.homepage = "https://github.com/acesuares/carrierwave-activerecord-store-in-model"
|
12
|
+
gem.summary = %q{Store file data in the database using ActiveRecord.}
|
13
|
+
gem.description = %q{Store file data in the database using ActiveRecord, but not in a seperate table, but in the model itself, instead.}
|
14
|
+
gem.require_paths = ['lib']
|
15
|
+
gem.licenses = ["MIT"]
|
16
|
+
|
17
|
+
gem.rubyforge_project = "carrierwave-activerecord-store-in-model"
|
18
|
+
|
19
|
+
gem.files = `git ls-files`.split("\n")
|
20
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
|
23
|
+
gem.add_runtime_dependency 'carrierwave', '~> 0.8.0'
|
24
|
+
|
25
|
+
# ActiveRecord 3.3 is unlikely, but prevent it just in case.
|
26
|
+
gem.add_runtime_dependency 'activerecord', '~> 3.2.0'
|
27
|
+
|
28
|
+
gem.add_development_dependency 'sqlite3', '~> 1.3'
|
29
|
+
gem.add_development_dependency 'rspec', '~> 2.12'
|
30
|
+
|
31
|
+
gem.platform = Gem::Platform::RUBY
|
32
|
+
gem.required_ruby_version = '>= 1.9.3'
|
33
|
+
end
|
data/db/.gitkeep
ADDED
File without changes
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
require 'active_record'
|
3
|
+
require 'carrierwave'
|
4
|
+
|
5
|
+
module CarrierWave
|
6
|
+
|
7
|
+
module Storage
|
8
|
+
module ActiverecordStoreInModel
|
9
|
+
autoload :VERSION, 'carrierwave-activerecord-store-in-model/storage/version'
|
10
|
+
autoload :StorageProvider, 'carrierwave-activerecord-store-in-model/storage/storage_provider'
|
11
|
+
autoload :File, 'carrierwave-activerecord-store-in-model/storage/file'
|
12
|
+
autoload :ActiveRecordFile, 'carrierwave-activerecord-store-in-model/storage/active_record_file'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Uploader
|
17
|
+
class Base
|
18
|
+
|
19
|
+
configure do |config|
|
20
|
+
config.storage_engines[:activerecord_store_in_model] = 'CarrierWave::Storage::ActiverecordStoreInModel::StorageProvider'
|
21
|
+
end
|
22
|
+
|
23
|
+
end # Base
|
24
|
+
end # Uploader
|
25
|
+
|
26
|
+
end # CarrierWave
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Storage
|
3
|
+
module ActiverecordStoreInModel
|
4
|
+
class ActiveRecordFile < ::ActiveRecord::Base
|
5
|
+
|
6
|
+
alias_method :delete, :destroy
|
7
|
+
alias_attribute :read, :data
|
8
|
+
|
9
|
+
attr_accessible :identifier,
|
10
|
+
:original_filename,
|
11
|
+
:content_type,
|
12
|
+
:size,
|
13
|
+
:data
|
14
|
+
|
15
|
+
def self.new (table_name, attributes)
|
16
|
+
self.table_name = table_name
|
17
|
+
super attributes
|
18
|
+
end
|
19
|
+
|
20
|
+
end # ActiveRecordFile
|
21
|
+
end # ActiveRecord
|
22
|
+
end # Storage
|
23
|
+
end # CarrierWave
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Storage
|
3
|
+
module ActiverecordStoreInModel
|
4
|
+
class File
|
5
|
+
def self.create!(new_file, identifier, store_dir)
|
6
|
+
attributes = { :identifier => identifier,
|
7
|
+
:original_filename => new_file.original_filename,
|
8
|
+
:content_type => new_file.content_type,
|
9
|
+
:size => new_file.size,
|
10
|
+
:data => new_file.read }
|
11
|
+
self.new ActiveRecordFile.new store_dir, attributes
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.fetch! identifier
|
15
|
+
self.new ActiveRecordFile.find_by_identifier! identifier
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.delete_all
|
19
|
+
ActiveRecordFile.delete_all
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :file
|
23
|
+
attr_accessor :url
|
24
|
+
|
25
|
+
def initialize(file = nil)
|
26
|
+
@file = file
|
27
|
+
end
|
28
|
+
|
29
|
+
def blank?
|
30
|
+
file.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
def read
|
34
|
+
file.data if file
|
35
|
+
end
|
36
|
+
|
37
|
+
def size
|
38
|
+
file.size if file
|
39
|
+
end
|
40
|
+
|
41
|
+
def content_type
|
42
|
+
file.content_type if file
|
43
|
+
end
|
44
|
+
|
45
|
+
def content_type= content_type
|
46
|
+
if file
|
47
|
+
file.content_type = content_type
|
48
|
+
file.save
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def identifier
|
53
|
+
file.identifier if file
|
54
|
+
end
|
55
|
+
|
56
|
+
def original_filename
|
57
|
+
file.original_filename if file
|
58
|
+
end
|
59
|
+
alias_method :filename, :original_filename
|
60
|
+
|
61
|
+
def delete
|
62
|
+
if file
|
63
|
+
file.destroy
|
64
|
+
else
|
65
|
+
false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
alias_method :destroy!, :delete
|
69
|
+
|
70
|
+
end # File
|
71
|
+
|
72
|
+
end # ActiveRecord
|
73
|
+
end # Storage
|
74
|
+
end # CarrierWave
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Storage
|
3
|
+
module ActiverecordStoreInModel
|
4
|
+
|
5
|
+
class StorageProvider
|
6
|
+
|
7
|
+
attr_reader :uploader
|
8
|
+
|
9
|
+
def initialize(uploader)
|
10
|
+
@uploader = uploader
|
11
|
+
end
|
12
|
+
|
13
|
+
def store! sanitized_file
|
14
|
+
@file = File.create! sanitized_file, uploader.identifier, uploader.model.class.table_name
|
15
|
+
set_file_properties
|
16
|
+
end
|
17
|
+
|
18
|
+
def retrieve! identifier
|
19
|
+
@file = File.fetch! identifier
|
20
|
+
set_file_properties
|
21
|
+
end
|
22
|
+
|
23
|
+
# This is from carrierwave-activerecord. It's 70% appropriate.
|
24
|
+
|
25
|
+
# All uploaded files are stored together in a single table. This means
|
26
|
+
# we cannot use filenames or model IDs as the identifier, e.g. there
|
27
|
+
# could be many files with the same filename uploaded; or, one model
|
28
|
+
# could have multiple uploaded files; or, two models could have the
|
29
|
+
# same ID such as @house.id = @book.id = 42.
|
30
|
+
#
|
31
|
+
# Override CarrierWave::Storage::Abstract#identifier() (by default
|
32
|
+
# returns @uploader.filename) to use a SHA1 of the time and a random
|
33
|
+
# number (to avoid time collisions) as the identifier stored in the
|
34
|
+
# mounted column. The identifier should be nil if there is no filename.
|
35
|
+
def identifier
|
36
|
+
if uploader.filename
|
37
|
+
@identifier ||= Digest::SHA1.hexdigest( "#{Time.now.to_i + rand(1000)}" )
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def set_file_properties
|
44
|
+
@file.url = compute_url
|
45
|
+
@file
|
46
|
+
end
|
47
|
+
|
48
|
+
def compute_url
|
49
|
+
uploader.default_url || begin
|
50
|
+
if defined? ::Rails.application.routes.url_helpers
|
51
|
+
model = uploader.model
|
52
|
+
|
53
|
+
route_helpers = ::Rails.application.routes.url_helpers
|
54
|
+
path_method_name = "#{model.class.to_s.underscore}_path"
|
55
|
+
|
56
|
+
url = route_helpers.send(path_method_name, model)
|
57
|
+
url << "/#{uploader.mounted_as.to_s}"
|
58
|
+
else
|
59
|
+
[ @file.identifier ].join '/'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end # StorageProvider
|
65
|
+
|
66
|
+
end # ActiveRecord
|
67
|
+
end # Storage
|
68
|
+
end # CarrierWave
|
69
|
+
|
70
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
require 'rails/generators/active_record'
|
4
|
+
|
5
|
+
class CarrierwaveActiverecordGenerator < Rails::Generators::Base
|
6
|
+
|
7
|
+
desc 'Generate a migration to create a database table suitable for file data storage.'
|
8
|
+
include Rails::Generators::Migration
|
9
|
+
|
10
|
+
def self.next_migration_number dirname
|
11
|
+
ActiveRecord::Generators::Base.next_migration_number dirname
|
12
|
+
end
|
13
|
+
|
14
|
+
source_root File.expand_path('templates', File.dirname(__FILE__))
|
15
|
+
|
16
|
+
def create_migration_file
|
17
|
+
migration_template 'create_carrierwave_files.rb', 'db/migrate/create_carrierwave_files.rb'
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class CreateCarrierwaveFiles < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :carrier_wave_files do |t|
|
4
|
+
t.string :identifier
|
5
|
+
t.string :original_filename
|
6
|
+
t.string :content_type
|
7
|
+
t.string :size
|
8
|
+
t.binary :data
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/locale/nl.yml
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'carrierwave-activerecord-store-in-model'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection(
|
5
|
+
:adapter => "sqlite3",
|
6
|
+
:database => "db/database.db"
|
7
|
+
)
|
8
|
+
# added this to test quickly
|
9
|
+
ActiveRecord::Migration.drop_table :apartments if ActiveRecord::Base.connection.table_exists? 'apartments'
|
10
|
+
|
11
|
+
ActiveRecord::Migration.create_table :apartments do |t|
|
12
|
+
t.string :name
|
13
|
+
t.string :description
|
14
|
+
t.string :picture
|
15
|
+
|
16
|
+
t.timestamps
|
17
|
+
|
18
|
+
t.string :identifier
|
19
|
+
t.string :original_filename
|
20
|
+
t.string :content_type
|
21
|
+
t.integer :size
|
22
|
+
t.binary :data
|
23
|
+
end
|
24
|
+
|
25
|
+
class ApartmentUploader < CarrierWave::Uploader::Base
|
26
|
+
storage :activerecord_store_in_model
|
27
|
+
end
|
28
|
+
|
29
|
+
class Apartment < ActiveRecord::Base
|
30
|
+
mount_uploader :picture, ApartmentUploader
|
31
|
+
skip_callback :save, :after, :store_picture!
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def write_picture_identifier
|
36
|
+
# we skip the after_save 'store_picture!' and in the before_save 'write_picture_identifier' we call super and then store_picture!
|
37
|
+
# and then copy our file attributes into the yet unsaved record. After that, it's saved...
|
38
|
+
super
|
39
|
+
store_picture!
|
40
|
+
%w(identifier original_filename content_type size data).each do |attr|
|
41
|
+
self[attr] = self.picture.file.file[attr]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
ap = FactoryGirl.create(:apartment)
|
48
|
+
puts ap.inspect
|
49
|
+
bp = Apartment.first
|
50
|
+
puts bp.inspect
|
data/spec/factories.rb
ADDED
Binary file
|
Binary file
|
Binary file
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'carrierwave'
|
3
|
+
require 'tempfile'
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'rspec'
|
6
|
+
require 'carrierwave/orm/activerecord'
|
7
|
+
require 'colored'
|
8
|
+
require 'factory_girl_rails'
|
9
|
+
|
10
|
+
include ActionDispatch::TestProcess
|
11
|
+
FactoryGirl.define do
|
12
|
+
factory :apartment do
|
13
|
+
name "Curacao_Apartment"
|
14
|
+
description "Nice Apartment near the ocean good for drinking Blue-Curacao"
|
15
|
+
picture File.open('spec/fixtures/images/Curacao-Punda-1956.jpg', 'r')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.include FactoryGirl::Syntax::Methods
|
21
|
+
end
|
22
|
+
|
23
|
+
def file_path( *paths )
|
24
|
+
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
|
25
|
+
end
|
26
|
+
|
27
|
+
def public_path( *paths )
|
28
|
+
File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
|
29
|
+
end
|
30
|
+
|
31
|
+
CarrierWave.root = public_path
|
32
|
+
|
33
|
+
module CarrierWave
|
34
|
+
module Test
|
35
|
+
# taken from here : http://www.ruby-doc.org/gems/docs/m/mm-carrierwave-0.0.2/CarrierWave/Test/MockFiles.html
|
36
|
+
module MockFiles
|
37
|
+
def stub_file(filename, mime_type=nil, fake_name=nil)
|
38
|
+
f = File.open(file_path(filename))
|
39
|
+
return f
|
40
|
+
end
|
41
|
+
|
42
|
+
def stub_tempfile(filename, mime_type=nil, fake_name=nil)
|
43
|
+
raise "#{path} file does not exist" unless File.exist?(file_path(filename))
|
44
|
+
|
45
|
+
t = Tempfile.new(filename)
|
46
|
+
FileUtils.copy_file(file_path(filename), t.path)
|
47
|
+
|
48
|
+
t.stub(:local_path => "",
|
49
|
+
:original_filename => filename || fake_name,
|
50
|
+
:content_type => mime_type)
|
51
|
+
|
52
|
+
return t
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
module I18nHelpers
|
57
|
+
def change_locale_and_store_translations(locale, translations, &block)
|
58
|
+
current_locale = I18n.locale
|
59
|
+
begin
|
60
|
+
I18n.backend.store_translations locale, translations
|
61
|
+
I18n.locale = locale
|
62
|
+
yield
|
63
|
+
ensure
|
64
|
+
I18n.reload!
|
65
|
+
I18n.locale = current_locale
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
RSpec.configure do |config|
|
73
|
+
config.include CarrierWave::Test::MockFiles
|
74
|
+
config.include CarrierWave::Test::I18nHelpers
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carrierwave-activerecord-store-in-model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ace Suares,
|
8
|
+
- Royendgel Silberie
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: carrierwave
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 0.8.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 0.8.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activerecord
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 3.2.0
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 3.2.0
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: sqlite3
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ~>
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.3'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.3'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '2.12'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.12'
|
70
|
+
description: Store file data in the database using ActiveRecord, but not in a seperate
|
71
|
+
table, but in the model itself, instead.
|
72
|
+
email:
|
73
|
+
- ace@suares.com
|
74
|
+
- royendgel@gmail.com
|
75
|
+
executables: []
|
76
|
+
extensions: []
|
77
|
+
extra_rdoc_files: []
|
78
|
+
files:
|
79
|
+
- .gitignore
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- carrierwave-activerecord-store-in-model.gemspec
|
86
|
+
- db/.gitkeep
|
87
|
+
- lib/carrierwave-activerecord-store-in-model.rb
|
88
|
+
- lib/carrierwave-activerecord-store-in-model/storage/active_record_file.rb
|
89
|
+
- lib/carrierwave-activerecord-store-in-model/storage/file.rb
|
90
|
+
- lib/carrierwave-activerecord-store-in-model/storage/storage_provider.rb
|
91
|
+
- lib/carrierwave-activerecord-store-in-model/storage/version.rb
|
92
|
+
- lib/generators/carrierwave_activerecord_generator.rb
|
93
|
+
- lib/generators/templates/create_carrierwave_files.rb
|
94
|
+
- lib/locale/nl.yml
|
95
|
+
- spec/carrierwave-activerecord-store-in-model_spec.rb
|
96
|
+
- spec/factories.rb
|
97
|
+
- spec/fixtures/images/Curacao-Airport-2010.jpg
|
98
|
+
- spec/fixtures/images/Curacao-Artis-2009.jpg
|
99
|
+
- spec/fixtures/images/Curacao-Punda-1956.jpg
|
100
|
+
- spec/fixtures/images/license
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
homepage: https://github.com/acesuares/carrierwave-activerecord-store-in-model
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: 1.9.3
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project: carrierwave-activerecord-store-in-model
|
122
|
+
rubygems_version: 2.1.9
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Store file data in the database using ActiveRecord.
|
126
|
+
test_files: []
|