carrierwave-nobrainer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +2 -0
- data/carrierwave-nobrainer.gemspec +25 -0
- data/lib/carrierwave/nobrainer.rb +37 -0
- data/lib/carrierwave/nobrainer/version.rb +5 -0
- data/spec/carrierwave-nobrainer_spec.rb +108 -0
- data/spec/fixtures/test.jpeg +0 -0
- data/spec/spec_helper.rb +37 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5def1ba3bd9e7a25b97f9aa628cbafc6ae13c8d2
|
4
|
+
data.tar.gz: 3eb9088f108c21079e5cfb8de2e75c6761a96b92
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aace3fa1e98b3253238b6f9cda65e7588b7d6340e26e0e8989fcf094850bc3dcdbba4c1a7ec5c52045570b9af44be89e2260235517c5df670388d6176ca03ebb
|
7
|
+
data.tar.gz: 10e44c4574a305d4b948274a3b0e2cf9442a5cc8554829582885e5b606b72dbc027df9d0d431111c680a28743a8e937b167932a569e90781a7402b93e77e3419
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
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/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# CarrierWave::NoBrainer
|
2
|
+
|
3
|
+
This is a [NoBrainer](https://github.com/nviennot/nobrainer) adapter for CarrierWave gem.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'carrierwave-nobrainer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
In your model put `extend CarrierWave::NoBrainer` and then follow normal CarrierWave
|
20
|
+
procedure. For example:
|
21
|
+
|
22
|
+
```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
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
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
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
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
|
@@ -0,0 +1,37 @@
|
|
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
|
@@ -0,0 +1,108 @@
|
|
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
ADDED
@@ -0,0 +1,37 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carrierwave-nobrainer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paweł Świątkowski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-08 00:00:00.000000000 Z
|
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
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: carrierwave
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !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
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- inquebrantable@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- 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
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.4.5
|
122
|
+
signing_key:
|
123
|
+
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:
|