carrierwave-nobrainer 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5def1ba3bd9e7a25b97f9aa628cbafc6ae13c8d2
4
- data.tar.gz: 3eb9088f108c21079e5cfb8de2e75c6761a96b92
2
+ SHA256:
3
+ metadata.gz: d4f69d9ab557b646896682713e0c9bc060a0beecea27b094e08c83c795271167
4
+ data.tar.gz: cfa6a25959c90443be683ebe7d8eda26ed1cd330a16636dfeed3b5b393a68465
5
5
  SHA512:
6
- metadata.gz: aace3fa1e98b3253238b6f9cda65e7588b7d6340e26e0e8989fcf094850bc3dcdbba4c1a7ec5c52045570b9af44be89e2260235517c5df670388d6176ca03ebb
7
- data.tar.gz: 10e44c4574a305d4b948274a3b0e2cf9442a5cc8554829582885e5b606b72dbc027df9d0d431111c680a28743a8e937b167932a569e90781a7402b93e77e3419
6
+ metadata.gz: 3e558164d9523964beafe880655343b0d8db79a067b8a0a69537d7ea4bf28406e8f9c94365d1e1f267b4caf85d2b02a6ac5d69433a1eb556d72607fceb440174
7
+ data.tar.gz: 7ff73e48baa6885849fc9d7085b4bea8b75ece9542950042054726d245f37421eacfa15a4498fbba87b1880f4d75d2a8229e5b6b72276e67614dc0bec1015b0e
data/README.md CHANGED
@@ -1,40 +1,48 @@
1
- # CarrierWave::NoBrainer
1
+ # NoBrainer CarrierWave Adapter
2
2
 
3
- This is a [NoBrainer](https://github.com/nviennot/nobrainer) adapter for CarrierWave gem.
3
+ carrierwave-nobrainer is an adapter to make
4
+ [CarrierWave](https://github.com/carrierwaveuploader/carrierwave/) work nicely with
5
+ [NoBrainer](http://nobrainer.io).
4
6
 
5
7
  ## Installation
6
8
 
7
- Add this line to your application's Gemfile:
9
+ Include in your Gemfile:
8
10
 
9
11
  ```ruby
10
- gem 'carrierwave-nobrainer'
12
+ gem 'carrierwave', '>= 2', '< 3'
13
+ gem 'carrierwave-nobrainer', github: 'nviennot/carrierwave-nobrainer'
11
14
  ```
12
15
 
13
- And then execute:
14
-
15
- $ bundle
16
+ Note: this adapter has only been tested with the master branch of carrierwave,
17
+ not the 0.10.0 version from Feb. 2014.
16
18
 
17
19
  ## Usage
18
20
 
19
- In your model put `extend CarrierWave::NoBrainer` and then follow normal CarrierWave
20
- procedure. For example:
21
+ Use carrierwave as usual.
22
+
23
+ As an added feature, you may pass `filename: 'some_filename.png' to
24
+ the `mount_uploader` method options. For example:
25
+
26
+ ```ruby
27
+ mount_uploader :icon, SomeUploader, filename: 'icon.png'
28
+ ```
29
+
30
+ This will have the effect of not storing this static filename in the document to
31
+ avoid polluting the DB with useless fields.
32
+
33
+ ### Storing files in RethinkDB
34
+
35
+ In the case you need to store files in the RethinkDB database, this gem also
36
+ add a Carrierwave storage for NoBrainer.
37
+
38
+ To use it, in your uploader set the storage to `:nobrainer`
21
39
 
22
40
  ```ruby
23
- class User
24
- include NoBrainer::Document
25
- include NoBrainer::Document::Timestamps
26
- include CarrierWave::NoBrainer
27
-
28
- field :name, type: String, required: true
29
- field :avatar, type: String
30
- mount_uploader :avatar, AvatarUploader
41
+ class AvatarUploader < CarrierWave::Uploader::Base
42
+ storage :nobrainer
31
43
  end
32
44
  ```
33
45
 
34
- ## Contributing
46
+ ## License
35
47
 
36
- 1. Fork it ( https://github.com/katafrakt/carrierwave-nobrainer/fork )
37
- 2. Create your feature branch (`git checkout -b my-new-feature`)
38
- 3. Commit your changes (`git commit -am 'Add some feature'`)
39
- 4. Push to the branch (`git push origin my-new-feature`)
40
- 5. Create a new Pull Request
48
+ MIT license.
@@ -0,0 +1,230 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CarrierWave
4
+ module Storage
5
+ class NoBrainer < Abstract
6
+ ##
7
+ # Store a file
8
+ #
9
+ # === Parameters
10
+ #
11
+ # [file (CarrierWave::SanitizedFile)] the file to store
12
+ #
13
+ # === Returns
14
+ #
15
+ # [CarrierWave::Storage::NoBrainer::File] the stored file
16
+ #
17
+ def store!(file)
18
+ f = CarrierWave::Storage::NoBrainer::File.new(uploader, self, uploader.store_path)
19
+ f.store(file)
20
+ f
21
+ end
22
+
23
+ ##
24
+ # Retrieve a file
25
+ #
26
+ # === Parameters
27
+ #
28
+ # [identifier (String)] unique identifier for file
29
+ #
30
+ # === Returns
31
+ #
32
+ # [CarrierWave::Storage::NoBrainer::File] the stored file
33
+ #
34
+ def retrieve!(identifier)
35
+ CarrierWave::Storage::NoBrainer::File.new(uploader, self, uploader.store_path(identifier))
36
+ end
37
+
38
+ ##
39
+ # Stores given file to cache directory.
40
+ #
41
+ # === Parameters
42
+ #
43
+ # [new_file (File, IOString, Tempfile)] any kind of file object
44
+ #
45
+ # === Returns
46
+ #
47
+ # [CarrierWave::SanitizedFile] a sanitized file
48
+ #
49
+ def cache!(new_file)
50
+ f = CarrierWave::Storage::NoBrainer::File.new(uploader, self, uploader.cache_path)
51
+ f.store(new_file)
52
+ f
53
+ end
54
+
55
+ ##
56
+ # Retrieves the file with the given cache_name from the cache.
57
+ #
58
+ # === Parameters
59
+ #
60
+ # [cache_name (String)] uniquely identifies a cache file
61
+ #
62
+ # === Raises
63
+ #
64
+ # [CarrierWave::InvalidParameter] if the cache_name is incorrectly formatted.
65
+ #
66
+ def retrieve_from_cache!(identifier)
67
+ CarrierWave::Storage::NoBrainer::File.new(uploader, self, uploader.cache_path(identifier))
68
+ end
69
+
70
+ ##
71
+ # Deletes a cache dir
72
+ #
73
+ def delete_dir!(path)
74
+ # do nothing, because there's no such things as 'empty directory'
75
+ end
76
+
77
+ def clean_cache!(seconds)
78
+ path_regex = %r{#{Regexp.escape(uploader.cache_dir)}/\d+-\d+-\d+-\d+/.+}
79
+
80
+ ::NoBrainer::FileCache.where(
81
+ path: path_regex
82
+ ).pluck(:path).without_ordering.raw.to_a.each do |doc|
83
+ matched = doc['path'].match(/(\d+)-\d+-\d+-\d+/)
84
+
85
+ next unless matched
86
+ next unless Time.at(matched[1].to_i) < (Time.now.utc - seconds)
87
+
88
+ ::NoBrainer::FileCache.where(path: doc['path']).first.delete
89
+ end
90
+ end
91
+
92
+ class File
93
+ ##
94
+ # Current local path to file
95
+ #
96
+ # === Returns
97
+ #
98
+ # [String] a path to file
99
+ #
100
+ attr_reader :path
101
+
102
+ ##
103
+ # Lookup value for file content-type header
104
+ #
105
+ # === Returns
106
+ #
107
+ # [String] value of content-type
108
+ #
109
+ def content_type
110
+ @content_type || file.try(:content_type)
111
+ end
112
+
113
+ ##
114
+ # Removes the file from the filesystem.
115
+ #
116
+ def delete
117
+ criteria = storage_place_from(path).where(path: path).without_ordering
118
+
119
+ return unless criteria.present?
120
+
121
+ criteria.first.delete
122
+
123
+ @content_type = nil
124
+ @file = nil
125
+ @path = nil
126
+ end
127
+
128
+ ##
129
+ # lookup file
130
+ #
131
+ # === Returns
132
+ #
133
+ # [NoBrainer::Document] file data from RethinkDB
134
+ #
135
+ def file
136
+ return nil unless path
137
+
138
+ storage_place_from(path).where(path: path).without_ordering.first
139
+ end
140
+
141
+ ##
142
+ # Return file name, if available
143
+ #
144
+ # === Returns
145
+ #
146
+ # [String] file name
147
+ # or
148
+ # [NilClass] no file name available
149
+ #
150
+ def filename
151
+ ::File.basename(file.path)
152
+ end
153
+
154
+ def initialize(uploader, base, path)
155
+ @uploader, @base, @path, @content_type = uploader, base, path, nil
156
+ end
157
+
158
+ ##
159
+ # Read content of file from service
160
+ #
161
+ # === Returns
162
+ #
163
+ # [String] contents of file
164
+ def read
165
+ file && file.body
166
+ end
167
+
168
+ ##
169
+ # Return size of file body
170
+ #
171
+ # === Returns
172
+ #
173
+ # [Integer] size of file body
174
+ #
175
+ def size
176
+ file.nil? ? 0 : file.body.length
177
+ end
178
+
179
+ ##
180
+ # Check if the file exists on the remote service
181
+ #
182
+ # === Returns
183
+ #
184
+ # [Boolean] true if file exists or false
185
+ def exists?
186
+ !!file
187
+ end
188
+
189
+ ##
190
+ # Write file to service
191
+ #
192
+ # === Returns
193
+ #
194
+ # [Boolean] true on success or raises error
195
+ def store(new_file)
196
+ nobrainer_file = new_file.file.body if new_file.is_a?(self.class)
197
+ nobrainer_file ||= new_file.to_file.read
198
+
199
+ @content_type ||= new_file.content_type
200
+
201
+ @file = storage_place_for(new_file).where(path: path).first_or_create(
202
+ content_type: new_file.content_type,
203
+ body: nobrainer_file
204
+ )
205
+
206
+ @file.save!
207
+
208
+ true
209
+ end
210
+
211
+ private
212
+
213
+ def storage_place_for(file)
214
+ return ::NoBrainer::FileStorage if file.is_a?(self.class)
215
+
216
+ ::NoBrainer::FileCache
217
+ end
218
+
219
+ def storage_place_from(path)
220
+ return ::NoBrainer::FileCache if path.start_with?(@uploader.cache_dir)
221
+
222
+ ::NoBrainer::FileStorage
223
+ end
224
+ end
225
+ end
226
+ end
227
+ end
228
+
229
+ # Adds the `:nobrainer` storage engine
230
+ CarrierWave::Uploader::Base.storage_engines[:nobrainer] = 'CarrierWave::Storage::NoBrainer'
@@ -0,0 +1,160 @@
1
+ # rubocop:disable Naming/FileName
2
+ # frozen_string_literal: true
3
+
4
+ require 'nobrainer'
5
+ require 'nobrainer/file_cache'
6
+ require 'nobrainer/file_storage'
7
+ require 'carrierwave'
8
+ require 'carrierwave/storage/nobrainer'
9
+ require 'carrierwave/validations/active_model'
10
+
11
+ module CarrierWave
12
+ module NoBrainer
13
+ include CarrierWave::Mount
14
+
15
+ ##
16
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
17
+ #
18
+ def mount_uploader(column, uploader = nil, options = {}, &block)
19
+ if options[:filename]
20
+ super(column, uploader, options) do
21
+ define_method(:filename) { options[:filename] }
22
+ end
23
+ else
24
+ super
25
+ end
26
+
27
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
28
+ def remote_#{column}_url=(url)
29
+ column = _mounter(:#{column}).serialization_column
30
+ attribute_may_change("#{column}")
31
+ super
32
+ end
33
+ RUBY
34
+ end
35
+
36
+ ##
37
+ # See +CarrierWave::Mount#mount_uploaders+ for documentation
38
+ #
39
+ def mount_uploaders(column, uploader = nil, options = {}, &block)
40
+ super
41
+
42
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
43
+ def remote_#{column}_urls=(url)
44
+ column = _mounter(:#{column}).serialization_column
45
+ attribute_may_change("#{column}")
46
+ super
47
+ end
48
+ RUBY
49
+ end
50
+
51
+ private
52
+
53
+ def mount_base(column, uploader = nil, options = {}, &block)
54
+ super
55
+
56
+ class << self; attr_accessor :uploader_static_filenames; end
57
+ self.uploader_static_filenames ||= {}.with_indifferent_access
58
+
59
+ if options[:filename]
60
+ self.uploader_static_filenames[column] = options[:filename]
61
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
62
+ def write_#{column}_identifier; end
63
+ RUBY
64
+ else
65
+ field options[:mount_on] || column
66
+ end
67
+
68
+ include CarrierWave::Validations::ActiveModel
69
+
70
+ validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
71
+ validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
72
+ validates_download_of column if uploader_option(column.to_sym, :validate_download)
73
+
74
+ before_save :"write_#{column}_identifier"
75
+ before_save :"store_#{column}!"
76
+
77
+ after_destroy :"remove_#{column}!"
78
+ after_update :"mark_remove_#{column}_false"
79
+
80
+ before_save :"store_previous_changes_for_#{column}"
81
+ after_update :"remove_previously_stored_#{column}"
82
+
83
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
84
+ def read_uploader(attr)
85
+ f = self.class.uploader_static_filenames[attr]
86
+ f ? f : _read_attribute(attr)
87
+ end
88
+
89
+ def write_uploader(attr, value)
90
+ f = self.class.uploader_static_filenames[attr]
91
+ f ? f : _write_attribute(attr, value)
92
+ end
93
+
94
+ def #{column}=(new_file)
95
+ column = _mounter(:#{column}).serialization_column
96
+ attribute_may_change(column)
97
+ super
98
+ end
99
+
100
+ def remove_#{column}=(value)
101
+ column = _mounter(:#{column}).serialization_column
102
+ attribute_may_change(column)
103
+ super
104
+ end
105
+
106
+ def remove_#{column}!
107
+ super
108
+ self.remove_#{column} = true
109
+ write_#{column}_identifier unless destroyed?
110
+ end
111
+
112
+ def store_previous_changes_for_#{column}
113
+ @_previous_changes_for_#{column} = changes[_mounter(:#{column}).serialization_column]
114
+ end
115
+
116
+ # Reset cached mounter on record reload
117
+ def reload(*)
118
+ @_mounters = nil
119
+ super
120
+ end
121
+
122
+ # Reset cached mounter on record dup
123
+ def initialize_dup(other)
124
+ @_mounters = nil
125
+ super
126
+ end
127
+ RUBY
128
+ end
129
+ end
130
+ end
131
+
132
+ module CarrierWave
133
+ module Validations
134
+ module ActiveModel
135
+ class ProcessingValidator < ::ActiveModel::EachValidator
136
+ def should_validate_field?(*)
137
+ true
138
+ end
139
+ end
140
+
141
+ class IntegrityValidator < ::ActiveModel::EachValidator
142
+ def should_validate_field?(*)
143
+ true
144
+ end
145
+ end
146
+
147
+ class DownloadValidator < ::ActiveModel::EachValidator
148
+ def should_validate_field?(*)
149
+ true
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ module NoBrainer::Document::ClassMethods
157
+ include CarrierWave::NoBrainer
158
+ end
159
+
160
+ # rubocop:enable Naming/FileName
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NoBrainer
4
+ #
5
+ # CarrierWave cache storage for uploaded files.
6
+ #
7
+ # This table will contain less documents than NoBrainer::FileStorage and
8
+ # therefore gets quicker query executions.
9
+ #
10
+ class FileCache
11
+ include NoBrainer::Document
12
+
13
+ table_config name: 'nobrainer_filecaches'
14
+
15
+ field :content_type, type: String
16
+ field :body, type: Binary
17
+ field :path, type: String, primary_key: true
18
+ end
19
+ end
20
+
21
+ NoBrainer::Document::Core._all << NoBrainer::FileCache
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NoBrainer
4
+ #
5
+ # CarrierWave store for uploaded files in a table where all uploaded files
6
+ # will remain until users request to delete them.
7
+ # Querying this table will be slower than the NoBrainer::FileCahe one.
8
+ #
9
+ class FileStorage
10
+ include NoBrainer::Document
11
+
12
+ table_config name: 'nobrainer_storages'
13
+
14
+ field :content_type, type: String
15
+ field :body, type: Binary
16
+ field :path, type: String, primary_key: true
17
+ end
18
+ end
19
+
20
+ NoBrainer::Document::Core._all << NoBrainer::FileStorage
metadata CHANGED
@@ -1,107 +1,63 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-nobrainer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - Paweł Świątkowski
7
+ - Nicolas Viennot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-08 00:00:00.000000000 Z
11
+ date: 2022-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: nobrainer
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 0.17.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: 0.17.0
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: carrierwave
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - ">="
32
18
  - !ruby/object:Gem::Version
33
- version: '0'
19
+ version: 0.10.0
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
24
  - - ">="
39
25
  - !ruby/object:Gem::Version
40
- version: '0'
26
+ version: 0.10.0
41
27
  - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.7'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.7'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '10.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '10.0'
69
- - !ruby/object:Gem::Dependency
70
- name: rspec
28
+ name: nobrainer
71
29
  requirement: !ruby/object:Gem::Requirement
72
30
  requirements:
73
31
  - - ">="
74
32
  - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
33
+ version: 0.24.0
34
+ type: :runtime
77
35
  prerelease: false
78
36
  version_requirements: !ruby/object:Gem::Requirement
79
37
  requirements:
80
38
  - - ">="
81
39
  - !ruby/object:Gem::Version
82
- version: '0'
40
+ version: 0.24.0
83
41
  description:
84
42
  email:
85
- - inquebrantable@gmail.com
43
+ - nicolas@viennot.biz
86
44
  executables: []
87
45
  extensions: []
88
46
  extra_rdoc_files: []
89
47
  files:
90
- - ".gitignore"
91
- - Gemfile
92
- - LICENSE.txt
93
48
  - README.md
94
- - Rakefile
95
- - carrierwave-nobrainer.gemspec
96
- - lib/carrierwave/nobrainer.rb
97
- - lib/carrierwave/nobrainer/version.rb
98
- - spec/carrierwave-nobrainer_spec.rb
99
- - spec/fixtures/test.jpeg
100
- - spec/spec_helper.rb
101
- homepage: https://github.com/katafrakt/carrierwave-nobrainer
49
+ - lib/carrierwave-nobrainer.rb
50
+ - lib/carrierwave/storage/nobrainer.rb
51
+ - lib/nobrainer/file_cache.rb
52
+ - lib/nobrainer/file_storage.rb
53
+ homepage: https://github.com/NoBrainerORM/carrierwave-nobrainer
102
54
  licenses:
103
55
  - MIT
104
- metadata: {}
56
+ metadata:
57
+ allowed_push_host: https://rubygems.org
58
+ homepage_uri: https://github.com/NoBrainerORM/carrierwave-nobrainer
59
+ source_code_uri: https://github.com/NoBrainerORM/carrierwave-nobrainer
60
+ changelog_uri: https://github.com/NoBrainerORM/carrierwave-nobrainer/blob/master/CHANGELOG.md
105
61
  post_install_message:
106
62
  rdoc_options: []
107
63
  require_paths:
@@ -110,20 +66,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
66
  requirements:
111
67
  - - ">="
112
68
  - !ruby/object:Gem::Version
113
- version: '0'
69
+ version: 1.9.0
114
70
  required_rubygems_version: !ruby/object:Gem::Requirement
115
71
  requirements:
116
72
  - - ">="
117
73
  - !ruby/object:Gem::Version
118
74
  version: '0'
119
75
  requirements: []
120
- rubyforge_project:
121
- rubygems_version: 2.4.5
76
+ rubygems_version: 3.1.6
122
77
  signing_key:
123
78
  specification_version: 4
124
- summary: Adds support for NoBrainer to CarrierWave
125
- test_files:
126
- - spec/carrierwave-nobrainer_spec.rb
127
- - spec/fixtures/test.jpeg
128
- - spec/spec_helper.rb
129
- has_rdoc:
79
+ summary: NoBrainer adapter for Carrierwave
80
+ test_files: []
data/.gitignore DELETED
@@ -1,15 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- spec/public
10
- /tmp/
11
- *.bundle
12
- *.so
13
- *.o
14
- *.a
15
- mkmf.log
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in carrierwave-nobrainer.gemspec
4
- gemspec
data/LICENSE.txt DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2015 katafrakt
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.
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require "bundler/gem_tasks"
2
-
@@ -1,25 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'carrierwave/nobrainer/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "carrierwave-nobrainer"
8
- spec.version = Carrierwave::Nobrainer::VERSION
9
- spec.authors = ["Paweł Świątkowski"]
10
- spec.email = ["inquebrantable@gmail.com"]
11
- spec.summary = %q{Adds support for NoBrainer to CarrierWave}
12
- spec.homepage = "https://github.com/katafrakt/carrierwave-nobrainer"
13
- spec.license = "MIT"
14
-
15
- spec.files = `git ls-files -z`.split("\x0")
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
- spec.require_paths = ["lib"]
19
-
20
- spec.add_dependency "nobrainer", '>= 0.17.0'
21
- spec.add_dependency "carrierwave"
22
- spec.add_development_dependency "bundler", "~> 1.7"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rspec"
25
- end
@@ -1,5 +0,0 @@
1
- module Carrierwave
2
- module Nobrainer
3
- VERSION = "0.1.0"
4
- end
5
- end
@@ -1,37 +0,0 @@
1
- require "carrierwave/nobrainer/version"
2
-
3
- module CarrierWave
4
- module NoBrainer
5
-
6
- def self.included(base)
7
- base.send(:extend, ClassMethods)
8
- end
9
-
10
- module ClassMethods
11
- include CarrierWave::Mount
12
- attr_accessor :uploader_definitions
13
-
14
- def mount_uploader(column, uploader, options={}, &block)
15
- super
16
-
17
- self.uploader_definitions ||= {}
18
- self.uploader_definitions[column] = {uploader: uploader, options: options}
19
-
20
- alias_method :read_uploader, :_read_attribute
21
- alias_method :write_uploader, :_write_attribute
22
-
23
- after_save :"store_#{column}!"
24
- before_save :"write_#{column}_identifier"
25
- after_destroy :"remove_#{column}!", :on => :destroy
26
- after_update :"mark_remove_#{column}_false", :on => :update
27
- before_update :"store_previous_model_for_#{column}"
28
- after_save :"remove_previously_stored_#{column}"
29
-
30
- define_method(:"#{column}=") do |file|
31
- attribute_may_change(column.to_sym)
32
- super(file)
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,108 +0,0 @@
1
- require_relative 'spec_helper'
2
-
3
- RSpec.describe CarrierWave::NoBrainer do
4
- class Model
5
- cattr_reader :uploader
6
- @@uploader = Class.new(CarrierWave::Uploader::Base)
7
- include NoBrainer::Document
8
- include CarrierWave::NoBrainer
9
-
10
- field :image, type: String
11
- mount_uploader :image, @@uploader
12
- end
13
-
14
- let(:uploader) { Model.uploader }
15
- let(:model) do
16
- Model.new
17
- end
18
-
19
- after(:each) do
20
- model.destroy
21
- end
22
-
23
- it "should return blank uploader when nothing has been assigned" do
24
- expect(model.image).to be_blank
25
- end
26
-
27
- it "should return blank uploader when an empty string has been assigned" do
28
- model.image = ''
29
- model.save
30
- id = model.id
31
- model = Model.find(id)
32
- expect(model.image).to be_blank
33
- end
34
-
35
- it "should retrieve a file from the storage if a value is stored in the database" do
36
- model.image = 'test.jpeg'
37
- model.save
38
- expect(model.image).to be_an_instance_of(uploader)
39
- end
40
-
41
- it "should set the path to the store dir" do
42
- model.image = File.open(file_path('test.jpeg'))
43
- model.save
44
- id = model.id
45
- model.reload
46
- expect(model.image.path).to eq(public_path('uploads/test.jpeg'))
47
- end
48
-
49
- it "should cache a file" do
50
- model.image = stub_file('test.jpeg')
51
- expect(model.image).to be_an_instance_of(uploader)
52
- end
53
-
54
- it "should copy a file into into the cache directory" do
55
- model.image = stub_file('test.jpeg')
56
- expect(model.image.current_path).to match(/^#{public_path('uploads/tmp')}/)
57
- end
58
-
59
- it "should do nothing when nil is assigned" do
60
- model.image = nil
61
- expect(model.image).to be_blank
62
- end
63
-
64
- it "should do nothing when an empty string is assigned" do
65
- model.image = ''
66
- expect(model.image).to be_blank
67
- end
68
-
69
- it "should do nothing when no file has been assigned" do
70
- expect(model.save?).to be_truthy
71
- expect(model.image).to be_blank
72
- end
73
-
74
- it "should copy the file to the upload directory when a file has been assigned" do
75
- model.image = stub_file('test.jpeg')
76
- expect(model.save?).to be_truthy
77
- expect(model.image).to be_an_instance_of(uploader)
78
- expect(model.image.current_path).to eq(public_path('uploads/test.jpeg'))
79
- end
80
-
81
- context 'with validation' do
82
- class InvalidModel < Model
83
- validate { errors.add(:base, "BOOM!") }
84
- end
85
-
86
- let(:model) { InvalidModel.new }
87
-
88
- it "should do nothing when a validation fails" do
89
- model.image = stub_file('test.jpeg')
90
- expect(model).not_to be_valid
91
- model.save rescue NoBrainer::Error::DocumentInvalid
92
- expect(model).to be_new_record
93
- expect(model.image).to be_an_instance_of(uploader)
94
- expect(model.image.current_path).to match(/^#{public_path('uploads/tmp')}/)
95
- end
96
- end
97
-
98
- it "should remove the image if remove_image? returns true" do
99
- model.image = stub_file('test.jpeg')
100
- model.save
101
- expect(model.image).not_to be_blank
102
- model.remove_image = true
103
- model.save
104
- model.reload
105
- expect(model.image).to be_blank
106
- end
107
-
108
- end
File without changes
data/spec/spec_helper.rb DELETED
@@ -1,37 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
- require 'rspec'
4
-
5
- require 'nobrainer'
6
- require 'carrierwave'
7
- require 'carrierwave/nobrainer'
8
-
9
- def file_path( *paths )
10
- File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
11
- end
12
-
13
- def public_path( *paths )
14
- File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
15
- end
16
-
17
- CarrierWave.root = public_path
18
-
19
- NoBrainer.configure do |config|
20
- config.app_name = 'carrierwave_nobrainer' # dashes not valid in RethinkDB world
21
- config.environment = 'test'
22
- end
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
- RSpec.configure do |config|
36
- config.include CarrierWave::Test::MockFiles
37
- end