carrierwave-activerecord-store-in-model 0.10.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/LICENSE.txt +1 -1
- data/README.md +188 -0
- data/lib/carrierwave-activerecord-store-in-model/storage/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZWI2MmNlNmI1ODA3OWY1MTI4ZWEyNzk1OWU1ZDg5NGYzZjVlZjUxNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZmU3MDJlMjNkOGNmMzY5YWNlNDMwOGRkYzNkYWU4OTE0Y2Q2YjNjOA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZjU2NjM0MTU4ZTM5NzAwZmNlZjNhMDNkZTE4MWE2OWIyNWVmYTEyMzhlMGQ1
|
10
|
+
MGEyNGYzNjM1ZWZiNDhmNjFlOTg4NTE2MGU1YTcxZmY3YjcyOGJiM2IxOWRk
|
11
|
+
NDg4ZmVlY2EwZmE4MTFhZGUwMzhlNDZlOWRkYTNjZjEyZGQ2ZjI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Y2IyNGY2MTI2YTAyMTIwNWQ3ZDE0MTA3OWRlYzQyNjY4ZjI2NmQ4ZGE3NWRi
|
14
|
+
ZTk5MWNiNWYzNGZhNDU4NTA0OGQzMTNhODRjNGFjZjRhNzEwODhhZjgzZmRk
|
15
|
+
NjM0MzE5ZmE4ODFiODNkOTk1OWI0ZWZiYzhhY2YwNTZjM2UyMjQ=
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -0,0 +1,188 @@
|
|
1
|
+
# Carrierwave::ActiverecordStoreInModel
|
2
|
+
|
3
|
+
CarrierWave::ActiverecordStoreInModel is a CarrierWave plugin which stores file data
|
4
|
+
using ActiveRecord. It relies on ActiveRecord for database
|
5
|
+
independence. It is based on Carrierwave::ActiveRecord.
|
6
|
+
|
7
|
+
## Why?
|
8
|
+
|
9
|
+
Carrierwave::ActiveRecord stores all files in one table, and retreives files by and identifier. In situations
|
10
|
+
where not everyone is supposed to retrieve all files (i.e. CanCan), this model doesn't work. Hence the need to store
|
11
|
+
the files in the table that is associated with the model.
|
12
|
+
|
13
|
+
## Caveat
|
14
|
+
|
15
|
+
At the moment, only one Uploader per model is supported. This is due to the fact that the columns that hold size, data,
|
16
|
+
content_type and identifier are not namespaced.
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
### Add the gem
|
21
|
+
|
22
|
+
Add it to your Gemfile:
|
23
|
+
|
24
|
+
gem 'carrierwave-activerecord-store-in-model'
|
25
|
+
|
26
|
+
And install:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or manually:
|
31
|
+
|
32
|
+
$ gem install carrierwave-activerecord-store-in-model
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
To use the ActiveRecord store, add the following to your uploader:
|
37
|
+
|
38
|
+
storage :activerecord_store_in_model
|
39
|
+
|
40
|
+
## Storing files
|
41
|
+
|
42
|
+
By default, the gem uses the same table as the model, derived from 'model.table_name'.
|
43
|
+
A migration is needed to add the following columns:
|
44
|
+
|
45
|
+
* identifier: string
|
46
|
+
* original_filename: string
|
47
|
+
* content_type: string
|
48
|
+
* size: integer
|
49
|
+
* data: binary
|
50
|
+
|
51
|
+
|
52
|
+
### Rails
|
53
|
+
|
54
|
+
If you do not have a suitable table, you may generate a migration to
|
55
|
+
create the default table:
|
56
|
+
|
57
|
+
$ rails g migration AddPictureToClient picture:string identifier:string original_filename:string content_type:string size:integer data:binary
|
58
|
+
$ rake db:migrate
|
59
|
+
|
60
|
+
## The following part is copied from the original Carrierwave::ActiveRecord documentation and may not be accurate for this gem.
|
61
|
+
|
62
|
+
### Outside Rails
|
63
|
+
|
64
|
+
If you are already using ActiveRecord as your ORM, the storage provider
|
65
|
+
will use the existing connection. Thus, it will work in Rails without
|
66
|
+
any additional configuration.
|
67
|
+
|
68
|
+
If you are not using ActiveRecord as your ORM, you will need to setup
|
69
|
+
the connection to the database.
|
70
|
+
|
71
|
+
|
72
|
+
## Serving files
|
73
|
+
|
74
|
+
|
75
|
+
### Rails
|
76
|
+
|
77
|
+
When used with Rails, the gem attempts to follow routing conventions.
|
78
|
+
|
79
|
+
File URLs point to the resource on which the uploader is mounted, using
|
80
|
+
the mounted attribute name as the tail portion of the URL.
|
81
|
+
|
82
|
+
To serve the files while preserving security, add to your application's
|
83
|
+
routes and appropriate controllers.
|
84
|
+
|
85
|
+
For example:
|
86
|
+
|
87
|
+
Given a people resource with an uploader mounted on the avatar field:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# app/models/person.rb
|
91
|
+
class Person < ActiveRecord::Base
|
92
|
+
attr_accessible :avatar
|
93
|
+
mount_uploader :avatar, AvatarUploader
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
Each avatar file will be available underneath it's corresponding person:
|
98
|
+
|
99
|
+
`/person/1/avatar`
|
100
|
+
|
101
|
+
Adding a member GET route to the resource will generate a named route
|
102
|
+
for use in controllers and views:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
# config/routes.rb
|
106
|
+
MyApp::Application.routes.draw do
|
107
|
+
resources :people do
|
108
|
+
# At the present time, resourcing files has not been tested.
|
109
|
+
member { get 'avatar' }
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
Then implement the method `PeopleController#avatar` to serve the avatar:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
# app/controllers/people_controller.rb
|
117
|
+
class PeopleController
|
118
|
+
|
119
|
+
# before_filters for auth, etc.
|
120
|
+
# ...
|
121
|
+
|
122
|
+
def avatar
|
123
|
+
person = Person.find(params[:id])
|
124
|
+
send_data(person.avatar.read, filename: person.avatar.file.filename)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
|
130
|
+
### Outside Rails: default routes
|
131
|
+
|
132
|
+
Without Rails, file URLs are generated from two parts.
|
133
|
+
|
134
|
+
* the `downloader_path_prefix`, common to all files
|
135
|
+
* the `identifier`, a SHA1 particular to each file
|
136
|
+
|
137
|
+
The path prefix is configurable in your CarrierWave configure block, as
|
138
|
+
`downloader_path_prefix`, the default is `/files`.
|
139
|
+
|
140
|
+
For example:
|
141
|
+
|
142
|
+
`GET /files/afdd0c3f8578270aae2bd1784b46cefa0bec8fa6 HTTP/1.1`
|
143
|
+
|
144
|
+
|
145
|
+
### Outside Rails: custom routes
|
146
|
+
|
147
|
+
Finally, you have the option of overriding the URL in each uploader:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
# app/uploaders/avatar_uploader.rb
|
151
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
152
|
+
def url
|
153
|
+
"/a/custom/url/to/avatar/#{uploader.model.id}"
|
154
|
+
end
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
|
159
|
+
## Further reading
|
160
|
+
|
161
|
+
### An example project
|
162
|
+
|
163
|
+
The following example and test project tracks the gem: https://github.com/richardkmichael/carrierwave-activerecord-project
|
164
|
+
|
165
|
+
### How to add a storage provider
|
166
|
+
|
167
|
+
A work-in-progress guide to writing a CarrierWave storage provider is here: https://github.com/richardkmichael/carrierwave-activerecord/wiki/Howto:-Adding-a-new-storage-engine
|
168
|
+
|
169
|
+
```
|
170
|
+
[3] pry(#<CarrierWave::Mount::Mounter>)> self
|
171
|
+
=> #<CarrierWave::Mount::Mounter:0x00000102b67aa8
|
172
|
+
@column=:file,
|
173
|
+
@integrity_error=nil,
|
174
|
+
@options={},
|
175
|
+
@processing_error=nil,
|
176
|
+
@record=
|
177
|
+
#<ArticleFile id: 5, file: "my_uploaded_file.txt", article_id: 4, created_at: "2012-06-02 11:42:10", updated_at: "2012-06-02 11:42:10">,
|
178
|
+
@uploader=,
|
179
|
+
@uploader_options={:mount_on=>nil}>
|
180
|
+
```
|
181
|
+
|
182
|
+
## Contributing
|
183
|
+
|
184
|
+
1. Fork it
|
185
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
186
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
187
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
188
|
+
5. Create new Pull Request
|