rails_pallet 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +24 -0
- data/.gitignore +10 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +69 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +213 -0
- data/Guardfile +11 -0
- data/MIT-LICENSE +20 -0
- data/README.md +233 -0
- data/Rakefile +36 -0
- data/app/assets/images/rails_pallet/.keep +0 -0
- data/app/assets/javascripts/rails_pallet/application.js +13 -0
- data/app/assets/stylesheets/rails_pallet/application.css +15 -0
- data/app/controllers/rails_pallet/application_controller.rb +4 -0
- data/app/controllers/rails_pallet/uploads_controller.rb +19 -0
- data/app/helpers/rails_pallet/application_helper.rb +4 -0
- data/app/models/rails_pallet/upload.rb +61 -0
- data/app/responders/rails_pallet_responder.rb +36 -0
- data/app/serializers/rails_pallet/upload_serializer.rb +3 -0
- data/app/views/layouts/rails_pallet/application.html.erb +14 -0
- data/bin/rails +12 -0
- data/config/routes.rb +3 -0
- data/db/migrate/20150612152328_create_rails_pallet_uploads.rb +7 -0
- data/db/migrate/20150612152417_add_attachment_file_to_uploads.rb +11 -0
- data/lib/generators/rails_pallet/install/USAGE +5 -0
- data/lib/generators/rails_pallet/install/install_generator.rb +21 -0
- data/lib/generators/rails_pallet/install/templates/initializer.rb +9 -0
- data/lib/generators/rails_pallet/upload_controller/USAGE +8 -0
- data/lib/generators/rails_pallet/upload_controller/templates/controller.rb +15 -0
- data/lib/generators/rails_pallet/upload_controller/upload_controller_generator.rb +47 -0
- data/lib/rails_pallet/active_record_extension.rb +78 -0
- data/lib/rails_pallet/engine.rb +5 -0
- data/lib/rails_pallet/version.rb +3 -0
- data/lib/rails_pallet.rb +26 -0
- data/lib/tasks/rails_pallet_tasks.rake +4 -0
- data/paperclip_upload.gemspec +34 -0
- metadata +343 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7fcaada380ac4657d466ca952c050755c92dcdc0
|
4
|
+
data.tar.gz: d73eae740295f2df2d7488645a5254ceffc5bcee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00d141071caff25e6f4d1a8b543fe515cc876526cc8d370c60b7d5a3dfb7efa426c3bcb56964f4cf6f68328d4d68d37d00df0a9182f935c6619f0b4c8aca3fc3
|
7
|
+
data.tar.gz: aad0ec5f6da60775205274ab9a4c63df622337c6b953a4ec950e6b9b79e3ea491e4e073f227411b31704acc485fad9c97400c040aa7d4177abffd8c80d3c7612
|
data/.editorconfig
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# EditorConfig helps developers define and maintain consistent
|
2
|
+
# coding styles between different editors and IDEs
|
3
|
+
# editorconfig.org
|
4
|
+
|
5
|
+
root = true
|
6
|
+
|
7
|
+
[*]
|
8
|
+
|
9
|
+
# Change these settings to your own preference
|
10
|
+
indent_style = space
|
11
|
+
indent_size = 2
|
12
|
+
|
13
|
+
# We recommend you to keep these unchanged
|
14
|
+
end_of_line = lf
|
15
|
+
charset = utf-8
|
16
|
+
trim_trailing_whitespace = true
|
17
|
+
insert_final_newline = true
|
18
|
+
|
19
|
+
[*.js]
|
20
|
+
indent_style = space
|
21
|
+
indent_size = 2
|
22
|
+
|
23
|
+
[*.md]
|
24
|
+
trim_trailing_whitespace = false
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
|
+
|
5
|
+
### v2.0.0
|
6
|
+
|
7
|
+
##### Fixed
|
8
|
+
|
9
|
+
* `skip_before_action :verify_authenticity_token` in uploads controller
|
10
|
+
|
11
|
+
##### Changed
|
12
|
+
|
13
|
+
* gem name from paperclip_upload to rails_pallet
|
14
|
+
|
15
|
+
### v1.2.4
|
16
|
+
|
17
|
+
##### Fixed
|
18
|
+
* Allow `encoded_[file]` accessor to work with base64 headers.
|
19
|
+
|
20
|
+
### v1.2.3
|
21
|
+
|
22
|
+
##### Fixed
|
23
|
+
|
24
|
+
* Removed `uploads#download` endpoint. Use always `file.url` instead of `file.path`. `file.path` does not work with S3 config.
|
25
|
+
|
26
|
+
### v1.2.2
|
27
|
+
|
28
|
+
##### Fixed
|
29
|
+
|
30
|
+
* Removed tmp and spec files from gemspec definition.
|
31
|
+
|
32
|
+
### v1.2.1
|
33
|
+
|
34
|
+
##### Changed
|
35
|
+
|
36
|
+
* A little refactor to controllers generator.
|
37
|
+
|
38
|
+
##### Fixed
|
39
|
+
* Autogenerated download url uses `identifier` attribute instead `id`.
|
40
|
+
|
41
|
+
### v1.2.0
|
42
|
+
|
43
|
+
##### Added
|
44
|
+
|
45
|
+
* Add file name, extension and download url to json response.
|
46
|
+
|
47
|
+
### v1.1.0
|
48
|
+
|
49
|
+
##### Added
|
50
|
+
|
51
|
+
* Allow multiple paperclip attributes on same host model to work with uploads.
|
52
|
+
|
53
|
+
### v1.0.0
|
54
|
+
|
55
|
+
##### Changed
|
56
|
+
|
57
|
+
* Use hashids gem to pass upload identifier in a more secure way.
|
58
|
+
|
59
|
+
##### Added
|
60
|
+
|
61
|
+
* Add uploads controller generator.
|
62
|
+
|
63
|
+
##### Removed
|
64
|
+
|
65
|
+
* Remove `additional_upload_endpoints` configuration option in favor of uploads controller generator.
|
66
|
+
|
67
|
+
### v0.1.0
|
68
|
+
|
69
|
+
* Initial release.
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in rails_pallet.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use a debugger
|
14
|
+
# gem 'byebug', group: [:development, :test]
|
15
|
+
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rails_pallet (2.0.0)
|
5
|
+
active_model_serializers (~> 0.9.3)
|
6
|
+
hashids (~> 1.0, >= 1.0.2)
|
7
|
+
paperclip (~> 4.2, >= 4.2.0)
|
8
|
+
rails (~> 4.2, >= 4.2.1)
|
9
|
+
responders (~> 2.1, >= 2.1.0)
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
actionmailer (4.2.1)
|
15
|
+
actionpack (= 4.2.1)
|
16
|
+
actionview (= 4.2.1)
|
17
|
+
activejob (= 4.2.1)
|
18
|
+
mail (~> 2.5, >= 2.5.4)
|
19
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
20
|
+
actionpack (4.2.1)
|
21
|
+
actionview (= 4.2.1)
|
22
|
+
activesupport (= 4.2.1)
|
23
|
+
rack (~> 1.6)
|
24
|
+
rack-test (~> 0.6.2)
|
25
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
27
|
+
actionview (4.2.1)
|
28
|
+
activesupport (= 4.2.1)
|
29
|
+
builder (~> 3.1)
|
30
|
+
erubis (~> 2.7.0)
|
31
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
33
|
+
active_model_serializers (0.9.3)
|
34
|
+
activemodel (>= 3.2)
|
35
|
+
activejob (4.2.1)
|
36
|
+
activesupport (= 4.2.1)
|
37
|
+
globalid (>= 0.3.0)
|
38
|
+
activemodel (4.2.1)
|
39
|
+
activesupport (= 4.2.1)
|
40
|
+
builder (~> 3.1)
|
41
|
+
activerecord (4.2.1)
|
42
|
+
activemodel (= 4.2.1)
|
43
|
+
activesupport (= 4.2.1)
|
44
|
+
arel (~> 6.0)
|
45
|
+
activesupport (4.2.1)
|
46
|
+
i18n (~> 0.7)
|
47
|
+
json (~> 1.7, >= 1.7.7)
|
48
|
+
minitest (~> 5.1)
|
49
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
50
|
+
tzinfo (~> 1.1)
|
51
|
+
annotate (2.6.8)
|
52
|
+
activerecord (>= 3.2, <= 4.3)
|
53
|
+
rake (~> 10.4)
|
54
|
+
arel (6.0.0)
|
55
|
+
builder (3.2.2)
|
56
|
+
celluloid (0.16.0)
|
57
|
+
timers (~> 4.0.0)
|
58
|
+
climate_control (0.0.3)
|
59
|
+
activesupport (>= 3.0)
|
60
|
+
cocaine (0.5.7)
|
61
|
+
climate_control (>= 0.0.3, < 1.0)
|
62
|
+
coderay (1.1.0)
|
63
|
+
diff-lcs (1.2.5)
|
64
|
+
erubis (2.7.0)
|
65
|
+
factory_girl (4.5.0)
|
66
|
+
activesupport (>= 3.0.0)
|
67
|
+
factory_girl_rails (4.5.0)
|
68
|
+
factory_girl (~> 4.5.0)
|
69
|
+
railties (>= 3.0.0)
|
70
|
+
ffi (1.9.8)
|
71
|
+
formatador (0.2.5)
|
72
|
+
globalid (0.3.6)
|
73
|
+
activesupport (>= 4.1.0)
|
74
|
+
guard (2.12.5)
|
75
|
+
formatador (>= 0.2.4)
|
76
|
+
listen (~> 2.7)
|
77
|
+
lumberjack (~> 1.0)
|
78
|
+
nenv (~> 0.1)
|
79
|
+
notiffany (~> 0.0)
|
80
|
+
pry (>= 0.9.12)
|
81
|
+
shellany (~> 0.0)
|
82
|
+
thor (>= 0.18.1)
|
83
|
+
guard-compat (1.2.1)
|
84
|
+
guard-rspec (4.5.2)
|
85
|
+
guard (~> 2.1)
|
86
|
+
guard-compat (~> 1.1)
|
87
|
+
rspec (>= 2.99.0, < 4.0)
|
88
|
+
hashids (1.0.2)
|
89
|
+
hitimes (1.2.2)
|
90
|
+
i18n (0.7.0)
|
91
|
+
json (1.8.3)
|
92
|
+
listen (2.10.0)
|
93
|
+
celluloid (~> 0.16.0)
|
94
|
+
rb-fsevent (>= 0.9.3)
|
95
|
+
rb-inotify (>= 0.9)
|
96
|
+
loofah (2.0.2)
|
97
|
+
nokogiri (>= 1.5.9)
|
98
|
+
lumberjack (1.0.9)
|
99
|
+
mail (2.6.3)
|
100
|
+
mime-types (>= 1.16, < 3)
|
101
|
+
method_source (0.8.2)
|
102
|
+
mime-types (2.6.2)
|
103
|
+
mimemagic (0.3.0)
|
104
|
+
mini_portile (0.6.2)
|
105
|
+
minitest (5.7.0)
|
106
|
+
nenv (0.2.0)
|
107
|
+
nokogiri (1.6.6.2)
|
108
|
+
mini_portile (~> 0.6.0)
|
109
|
+
notiffany (0.0.6)
|
110
|
+
nenv (~> 0.1)
|
111
|
+
shellany (~> 0.0)
|
112
|
+
paperclip (4.3.0)
|
113
|
+
activemodel (>= 3.2.0)
|
114
|
+
activesupport (>= 3.2.0)
|
115
|
+
cocaine (~> 0.5.5)
|
116
|
+
mime-types
|
117
|
+
mimemagic (= 0.3.0)
|
118
|
+
pry (0.10.1)
|
119
|
+
coderay (~> 1.1.0)
|
120
|
+
method_source (~> 0.8.1)
|
121
|
+
slop (~> 3.4)
|
122
|
+
pry-rails (0.3.4)
|
123
|
+
pry (>= 0.9.10)
|
124
|
+
rack (1.6.1)
|
125
|
+
rack-test (0.6.3)
|
126
|
+
rack (>= 1.0)
|
127
|
+
rails (4.2.1)
|
128
|
+
actionmailer (= 4.2.1)
|
129
|
+
actionpack (= 4.2.1)
|
130
|
+
actionview (= 4.2.1)
|
131
|
+
activejob (= 4.2.1)
|
132
|
+
activemodel (= 4.2.1)
|
133
|
+
activerecord (= 4.2.1)
|
134
|
+
activesupport (= 4.2.1)
|
135
|
+
bundler (>= 1.3.0, < 2.0)
|
136
|
+
railties (= 4.2.1)
|
137
|
+
sprockets-rails
|
138
|
+
rails-deprecated_sanitizer (1.0.3)
|
139
|
+
activesupport (>= 4.2.0.alpha)
|
140
|
+
rails-dom-testing (1.0.6)
|
141
|
+
activesupport (>= 4.2.0.beta, < 5.0)
|
142
|
+
nokogiri (~> 1.6.0)
|
143
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
144
|
+
rails-html-sanitizer (1.0.2)
|
145
|
+
loofah (~> 2.0)
|
146
|
+
railties (4.2.1)
|
147
|
+
actionpack (= 4.2.1)
|
148
|
+
activesupport (= 4.2.1)
|
149
|
+
rake (>= 0.8.7)
|
150
|
+
thor (>= 0.18.1, < 2.0)
|
151
|
+
rake (10.4.2)
|
152
|
+
rb-fsevent (0.9.5)
|
153
|
+
rb-inotify (0.9.5)
|
154
|
+
ffi (>= 0.5.0)
|
155
|
+
recursive-open-struct (0.6.4)
|
156
|
+
responders (2.1.0)
|
157
|
+
railties (>= 4.2.0, < 5)
|
158
|
+
rspec (3.2.0)
|
159
|
+
rspec-core (~> 3.2.0)
|
160
|
+
rspec-expectations (~> 3.2.0)
|
161
|
+
rspec-mocks (~> 3.2.0)
|
162
|
+
rspec-core (3.2.3)
|
163
|
+
rspec-support (~> 3.2.0)
|
164
|
+
rspec-expectations (3.2.1)
|
165
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
166
|
+
rspec-support (~> 3.2.0)
|
167
|
+
rspec-mocks (3.2.1)
|
168
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
169
|
+
rspec-support (~> 3.2.0)
|
170
|
+
rspec-rails (3.2.2)
|
171
|
+
actionpack (>= 3.0, < 4.3)
|
172
|
+
activesupport (>= 3.0, < 4.3)
|
173
|
+
railties (>= 3.0, < 4.3)
|
174
|
+
rspec-core (~> 3.2.0)
|
175
|
+
rspec-expectations (~> 3.2.0)
|
176
|
+
rspec-mocks (~> 3.2.0)
|
177
|
+
rspec-support (~> 3.2.0)
|
178
|
+
rspec-support (3.2.2)
|
179
|
+
shellany (0.0.1)
|
180
|
+
shoulda-matchers (2.8.0)
|
181
|
+
activesupport (>= 3.0.0)
|
182
|
+
slop (3.6.0)
|
183
|
+
sprockets (3.4.0)
|
184
|
+
rack (> 1, < 3)
|
185
|
+
sprockets-rails (2.3.3)
|
186
|
+
actionpack (>= 3.0)
|
187
|
+
activesupport (>= 3.0)
|
188
|
+
sprockets (>= 2.8, < 4.0)
|
189
|
+
sqlite3 (1.3.10)
|
190
|
+
thor (0.19.1)
|
191
|
+
thread_safe (0.3.5)
|
192
|
+
timers (4.0.1)
|
193
|
+
hitimes
|
194
|
+
tzinfo (1.2.2)
|
195
|
+
thread_safe (~> 0.1)
|
196
|
+
|
197
|
+
PLATFORMS
|
198
|
+
ruby
|
199
|
+
|
200
|
+
DEPENDENCIES
|
201
|
+
annotate (~> 2.6, >= 2.6.6)
|
202
|
+
factory_girl_rails (~> 4.5, >= 4.5.0)
|
203
|
+
guard (~> 2.12, >= 2.12.5)
|
204
|
+
guard-rspec (~> 4.5, >= 4.5.0)
|
205
|
+
pry-rails (~> 0.3.3)
|
206
|
+
rails_pallet!
|
207
|
+
recursive-open-struct (~> 0.6.4)
|
208
|
+
rspec-rails (~> 3.2, >= 3.2.1)
|
209
|
+
shoulda-matchers (~> 2.8, >= 2.8.0)
|
210
|
+
sqlite3 (~> 1.3, >= 1.3.10)
|
211
|
+
|
212
|
+
BUNDLED WITH
|
213
|
+
1.10.6
|
data/Guardfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
guard :rspec, cmd: 'zeus rspec' do
|
2
|
+
watch(%r{^spec/.+_spec\.rb$})
|
3
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/dummy/spec/lib/#{m[1]}_spec.rb" }
|
4
|
+
watch('spec/spec_helper.rb') { "spec" }
|
5
|
+
|
6
|
+
# Rails example
|
7
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/dummy/spec/#{m[1]}_spec.rb" }
|
8
|
+
watch(%r{^spec/dummy/spec/support/(.+)\.rb$}) { "spec" }
|
9
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
10
|
+
watch('spec/rails_helper.rb') { "spec" }
|
11
|
+
end
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Leandro Segovia
|
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
@@ -0,0 +1,233 @@
|
|
1
|
+
# Rails Pallet
|
2
|
+
|
3
|
+
Rails engine to save [paperclip](https://github.com/thoughtbot/paperclip) attachments asynchronously. For example, if you have an `User` model with `avatar` paperclip attribute, you will:
|
4
|
+
|
5
|
+
1. Create a new upload instance to hold the avatar file temporarily.
|
6
|
+
1. Create a new user passing the upload id or instance as parameter.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add to your Gemfile...
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'rails_pallet'
|
14
|
+
```
|
15
|
+
|
16
|
+
Run the installer:
|
17
|
+
|
18
|
+
```shell
|
19
|
+
$ rails generate rails_pallet:install
|
20
|
+
```
|
21
|
+
|
22
|
+
Then, add an attachment, as usually do, using paperclip.
|
23
|
+
|
24
|
+
```shell
|
25
|
+
$ rails generate paperclip user avatar
|
26
|
+
```
|
27
|
+
|
28
|
+
Inside `User` model, you need to execute `has_attached_upload` instead the `has_attached_file` paperclip's method:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class User < ActiveRecord::Base
|
32
|
+
has_attached_upload :avatar, path: ':rails_root/tmp/users/:id/:filename'
|
33
|
+
do_not_validate_attachment_file_type :avatar
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
|
39
|
+
### Using `upload_identifier`
|
40
|
+
|
41
|
+
**First**: you need to save an upload instance.
|
42
|
+
The engine creates the `POST /uploads` endpoint to achieve this.
|
43
|
+
After perform a `POST` to `/uploads` with a `file` param, you will get this response:
|
44
|
+
|
45
|
+
```json
|
46
|
+
{
|
47
|
+
"upload": {
|
48
|
+
"identifier": "rW6q2QZM",
|
49
|
+
"file_extension": "jpg",
|
50
|
+
"file_name": "90033441_BLO_20150607",
|
51
|
+
"download_url": "http://my-server.com/uploads/rW6q2QZM/download"
|
52
|
+
}
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
**Second**: supposing you have an `UsersController`, you need to perform a `POST /users` to create a new user passing the upload identifier you get previously.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class UsersController < ApplicationController
|
60
|
+
def create
|
61
|
+
respond_with User.create(permitted_params)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def permitted_params
|
67
|
+
params.require(:user).permit(:upload_identifier)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
### Using `upload`
|
73
|
+
|
74
|
+
You can do something like this:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class UsersController < ApplicationController
|
78
|
+
def create
|
79
|
+
@user = User.new
|
80
|
+
@user.upload = RailsPallet::Upload.create(file: params[:file])
|
81
|
+
@user.save!
|
82
|
+
respond_with @user
|
83
|
+
end
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
### Using prefix
|
88
|
+
|
89
|
+
You will need a way to match an upload resource with a specific attribute if you have two or more paperclip attributes on the same model. To do this, you can pass the option `upload: { use_prefix: true }` on `has_attached_upload` like this:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class User < ActiveRecord::Base
|
93
|
+
has_attached_upload :avatar, path: ':rails_root/tmp/users/:id/:filename', upload: { use_prefix: true }
|
94
|
+
has_attached_upload :document, path: ':rails_root/tmp/users/:id/:filename', upload: { use_prefix: true }
|
95
|
+
has_attached_upload :photo, path: ':rails_root/tmp/users/:id/:filename'
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
Then, in your controller...
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
class UsersController < ApplicationController
|
103
|
+
def create
|
104
|
+
respond_with User.create(permitted_params)
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def permitted_params
|
110
|
+
params.require(:user).permit(:avatar_upload_identifier, :document_upload_identifier, :upload_identifier)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
or using the upload object...
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
class UsersController < ApplicationController
|
119
|
+
def create
|
120
|
+
@user = User.new
|
121
|
+
@user.avatar_upload = RailsPallet::Upload.create(file: params[:avatar])
|
122
|
+
@user.document_upload = RailsPallet::Upload.create(file: params[:document])
|
123
|
+
@user.upload = RailsPallet::Upload.create(file: params[:photo])
|
124
|
+
@user.save!
|
125
|
+
respond_with @user
|
126
|
+
end
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
> If you execute `has_attached_upload` method two or more times in same model with `upload: { use_prefix: false }` (false is the default value), you will be obligated to use the `upload: { use_prefix: true }` option. If you don't, an exception will be raised.
|
131
|
+
|
132
|
+
### Base64 Encoding
|
133
|
+
|
134
|
+
If you want to save base64 encoded attachments, you can execute the `allow_encoded_file_for` method:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
class User < ActiveRecord::Base
|
138
|
+
has_attached_upload :avatar, path: ':rails_root/tmp/users/:id/:filename'
|
139
|
+
allow_encoded_file_for :avatar
|
140
|
+
do_not_validate_attachment_file_type :avatar
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
And then...
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
class UsersController < ApplicationController
|
148
|
+
def create
|
149
|
+
respond_with User.create(permitted_params)
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
|
154
|
+
def permitted_params
|
155
|
+
params.require(:user).permit(:encoded_avatar)
|
156
|
+
# encoded_avatar param must hold the encoded file
|
157
|
+
end
|
158
|
+
end
|
159
|
+
```
|
160
|
+
|
161
|
+
### Creating your own `UploadsController`
|
162
|
+
|
163
|
+
You can generate your own `UploadsController` if for example:
|
164
|
+
|
165
|
+
* your controllers don't inherit from the `ApplicationController`
|
166
|
+
* you want to change the default route for `UploadsController`. (Also you can map the default controller with another route in your `routes.rb`)
|
167
|
+
* you want to add extra logic to the default `UploadsController`
|
168
|
+
|
169
|
+
Running...
|
170
|
+
|
171
|
+
```shell
|
172
|
+
$ rails generate rails_pallet:upload_controller api/uploads api/base
|
173
|
+
```
|
174
|
+
|
175
|
+
You will get in `your_app/app/controllers/api/uploads_controller.rb`
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
class Api::UploadsController < Api::BaseController
|
179
|
+
self.responder = RailsPalletResponder
|
180
|
+
respond_to :json
|
181
|
+
|
182
|
+
def create
|
183
|
+
new_upload = RailsPallet::Upload.create(permitted_params)
|
184
|
+
respond_with new_upload, status: :created
|
185
|
+
end
|
186
|
+
|
187
|
+
private
|
188
|
+
|
189
|
+
def permitted_params
|
190
|
+
params.permit(:file)
|
191
|
+
end
|
192
|
+
|
193
|
+
def upload
|
194
|
+
@upload ||= RailsPallet::Upload.find_by_identifier(params[:identifier])
|
195
|
+
end
|
196
|
+
end
|
197
|
+
```
|
198
|
+
|
199
|
+
and the route...
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
post "api/uploads", to: "api/uploads#create", defaults: { format: :json }
|
203
|
+
```
|
204
|
+
|
205
|
+
## Configuration
|
206
|
+
|
207
|
+
You can change the engine configuration from `your_app/config/initializers/rails_pallet.rb`
|
208
|
+
|
209
|
+
#### Configuration Options:
|
210
|
+
|
211
|
+
* `hash_salt`: The upload module uses a salt string to generate an unique hash for each instance. A salt string can be defined here to replace the default and increase the module's security.
|
212
|
+
|
213
|
+
* `use_prefix`: false by default. If true, you will need to pass `[host_model_paperclip_attribute_name]_+upload|upload_identifier` instead just `upload` or `upload_identifier` to the host model to use an upload resource.
|
214
|
+
|
215
|
+
## Contributing
|
216
|
+
|
217
|
+
1. Fork it
|
218
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
219
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
220
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
221
|
+
5. Create new Pull Request
|
222
|
+
|
223
|
+
## Credits
|
224
|
+
|
225
|
+
Thank you [contributors](https://github.com/platanus/rails_pallet/graphs/contributors)!
|
226
|
+
|
227
|
+
<img src="http://platan.us/gravatar_with_text.png" alt="Platanus" width="250"/>
|
228
|
+
|
229
|
+
rails_pallet is maintained by [platanus](http://platan.us).
|
230
|
+
|
231
|
+
## License
|
232
|
+
|
233
|
+
Guides is © 2015 platanus, spa. It is free software and may be redistributed under the terms specified in the LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RailsPallet'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
begin
|
21
|
+
require 'rspec/core/rake_task'
|
22
|
+
RSpec::Core::RakeTask.new(:spec)
|
23
|
+
rescue LoadError
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'thor'
|
27
|
+
|
28
|
+
task default: :spec
|
29
|
+
|
30
|
+
task :run_dummy_app do
|
31
|
+
Dir.chdir("spec/dummy") do
|
32
|
+
exec("bundle exec rails s -p 3000")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Bundler::GemHelper.install_tasks
|
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|