activemodel-datastore 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +42 -0
- data/lib/active_model/datastore/carrier_wave_uploader.rb +117 -0
- data/lib/active_model/datastore/version.rb +1 -1
- metadata +20 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43c4bfc5e295411cd88d6bdff0897e564dea9e23
|
4
|
+
data.tar.gz: 4ed3b3a28213026e2c99cb0f616372c82958d78f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebe8bb990217d55648209e66be3114176d83e85c81fdecf3f4b1396346f1b063cb823e8aed2e7599e9568e1bd0c92bb4e5ae0dd7c2801a8114eeaf6513e41fb1
|
7
|
+
data.tar.gz: 6b623ad9ccc3094e96b19969df904e844c4659f9b7dbbabebfd97ae6c8e98311c437c9b3ee4cbdaa2b6733ff2ceb085b0d242baf0be8e5a67802dea83d5043dd
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
### 0.2.3 / 2017-05-24
|
2
|
+
* adding CarrierWave file upload support
|
3
|
+
* updating example Cloud Datastore Rails app to 5.1
|
4
|
+
* adding image upload example to example Rails app
|
5
|
+
|
1
6
|
### 0.2.2 / 2017-04-27
|
2
7
|
|
3
8
|
* now store a hash of entity properties during entity to model conversion
|
data/README.md
CHANGED
@@ -24,6 +24,7 @@ suited for unstructured or semi-structured application data.
|
|
24
24
|
- [Datastore Indexes](#indexes)
|
25
25
|
- [Datastore Emulator](#emulator)
|
26
26
|
- [Example Rails App](#rails)
|
27
|
+
- [CarrierWave File Uploads](#carrierwave)
|
27
28
|
- [Track Changes](#track_changes)
|
28
29
|
- [Nested Forms](#nested)
|
29
30
|
- [Datastore Gotchas](#gotchas)
|
@@ -471,6 +472,47 @@ There is an example Rails 5 app in the test directory [here](https://github.com/
|
|
471
472
|
```
|
472
473
|
|
473
474
|
Navigate to http://localhost:3000.
|
475
|
+
|
476
|
+
## <a name="carrierwave"></a>CarrierWave File Uploads
|
477
|
+
|
478
|
+
Active Model Datastore has built in support for [CarrierWave](https://github.com/carrierwaveuploader/carrierwave)
|
479
|
+
which is a simple and extremely flexible way to upload files from Rails applications. You can use
|
480
|
+
different stores, including filesystem and cloud storage such as Google Cloud Storage or AWS.
|
481
|
+
|
482
|
+
Simply require `active_model/datastore/carrier_wave_uploader` and extend your model with the
|
483
|
+
CarrierWaveUploader (after including ActiveModel::Datastore). Follow the CarrierWave
|
484
|
+
[instructions](https://github.com/carrierwaveuploader/carrierwave#getting-started) for generating
|
485
|
+
an uploader.
|
486
|
+
|
487
|
+
In this example it will be something like:
|
488
|
+
|
489
|
+
`rails generate uploader ProfileImage`
|
490
|
+
|
491
|
+
Define an attribute on the model for your file(s). You can then mount the uploaders using
|
492
|
+
`mount_uploader` (single file) or `mount_uploaders` (array of files). Don't forget to add the new
|
493
|
+
attribute to `entity_properties` and whitelist the attribute in the controller if using strong
|
494
|
+
parameters.
|
495
|
+
|
496
|
+
```ruby
|
497
|
+
require 'active_model/datastore/carrier_wave_uploader'
|
498
|
+
|
499
|
+
class User
|
500
|
+
include ActiveModel::Datastore
|
501
|
+
extend CarrierWaveUploader
|
502
|
+
|
503
|
+
attr_accessor :email, :enabled, :name, :profile_image, :role
|
504
|
+
|
505
|
+
mount_uploader :profile_image, ProfileImageUploader
|
506
|
+
|
507
|
+
def entity_properties
|
508
|
+
%w[email enabled name profile_image role]
|
509
|
+
end
|
510
|
+
end
|
511
|
+
```
|
512
|
+
|
513
|
+
You will want to add something like this to your Rails form:
|
514
|
+
|
515
|
+
`<%= form.file_field :profile_image %>`
|
474
516
|
|
475
517
|
## <a name="track_changes"></a>Track Changes
|
476
518
|
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module CarrierWaveUploader
|
2
|
+
include CarrierWave::Mount
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def mount_base(column, uploader = nil, options = {}, &block)
|
7
|
+
super
|
8
|
+
|
9
|
+
# include CarrierWave::Validations::ActiveModel
|
10
|
+
#
|
11
|
+
# validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
|
12
|
+
# validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
|
13
|
+
# validates_download_of column if uploader_option(column.to_sym, :validate_download)
|
14
|
+
|
15
|
+
after_save :"store_#{column}!"
|
16
|
+
after_update :"store_#{column}!"
|
17
|
+
after_destroy :"remove_#{column}!"
|
18
|
+
|
19
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
20
|
+
##
|
21
|
+
# Override for setting the file urls on the entity.
|
22
|
+
#
|
23
|
+
def build_entity(parent = nil)
|
24
|
+
entity = super(parent)
|
25
|
+
self.class.uploaders.keys.each do |col|
|
26
|
+
entity[col.to_s] = send("get_" + col.to_s + "_identifiers")
|
27
|
+
end
|
28
|
+
entity
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Override to append file names for mount_uploaders.
|
33
|
+
# Works with multiple files stored as an Array.
|
34
|
+
#
|
35
|
+
def update(params)
|
36
|
+
existing_files = {}
|
37
|
+
self.class.uploaders.keys.each do |attr_name|
|
38
|
+
existing_files[attr_name] = uploader_file_names(attr_name) if send(attr_name).is_a? Array
|
39
|
+
end
|
40
|
+
assign_attributes(params)
|
41
|
+
return unless valid?
|
42
|
+
run_callbacks :update do
|
43
|
+
entity = build_entity
|
44
|
+
self.class.uploaders.keys.each do |attr_name|
|
45
|
+
entity[attr_name] = append_files(entity[attr_name], existing_files[attr_name])
|
46
|
+
end
|
47
|
+
self.class.retry_on_exception? { CloudDatastore.dataset.save entity }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# For new entities, set the identifiers (file names).
|
53
|
+
# For deleted entities, set the identifier (which will be nil).
|
54
|
+
# For updated entities, set the identifiers if they have changed. The
|
55
|
+
# identifier will be nil if files were not uploaded during the update.
|
56
|
+
#
|
57
|
+
def get_#{column}_identifiers
|
58
|
+
identifier = write_#{column}_identifier
|
59
|
+
if persisted? && !remove_#{column}? && identifier.nil?
|
60
|
+
if defined?(#{column}_identifier) && #{column}_identifier.present?
|
61
|
+
#{column}_identifier if defined?(#{column}_identifier) && #{column}_identifier.present?
|
62
|
+
elsif defined?(#{column}_identifiers) && #{column}_identifiers.present?
|
63
|
+
#{column}_identifiers
|
64
|
+
end
|
65
|
+
else
|
66
|
+
identifier
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
##
|
71
|
+
# Called by CarrierWave::Mount.mount_uploaders -> write_#{column}_identifier.
|
72
|
+
#
|
73
|
+
def write_uploader(column, identifier)
|
74
|
+
identifier
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# This gets called whenever the uploaders instance variable is nil.
|
79
|
+
# It returns the uploader identifiers (file names) for the desired column.
|
80
|
+
#
|
81
|
+
def read_uploader(column)
|
82
|
+
if entity_property_values.present? && entity_property_values.key?(column.to_s)
|
83
|
+
entity_property_values[column.to_s]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
# Reset cached mounter on record reload.
|
89
|
+
#
|
90
|
+
def reload!
|
91
|
+
@_mounters = nil
|
92
|
+
super
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Reset cached mounter on record dup.
|
97
|
+
#
|
98
|
+
def initialize_dup(other)
|
99
|
+
@_mounters = nil
|
100
|
+
super
|
101
|
+
end
|
102
|
+
|
103
|
+
# private
|
104
|
+
|
105
|
+
def uploader_file_names(attr_name)
|
106
|
+
send(attr_name).map { |x| x.file.filename }
|
107
|
+
end
|
108
|
+
|
109
|
+
def append_files(files, new_files)
|
110
|
+
if files.is_a?(Array) && !new_files.nil?
|
111
|
+
files = files.push(*new_files).flatten.compact.uniq
|
112
|
+
end
|
113
|
+
files
|
114
|
+
end
|
115
|
+
RUBY
|
116
|
+
end
|
117
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activemodel-datastore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bryce McLean
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -154,14 +154,28 @@ dependencies:
|
|
154
154
|
requirements:
|
155
155
|
- - "~>"
|
156
156
|
- !ruby/object:Gem::Version
|
157
|
-
version: 0.48
|
157
|
+
version: '0.48'
|
158
158
|
type: :development
|
159
159
|
prerelease: false
|
160
160
|
version_requirements: !ruby/object:Gem::Requirement
|
161
161
|
requirements:
|
162
162
|
- - "~>"
|
163
163
|
- !ruby/object:Gem::Version
|
164
|
-
version: 0.48
|
164
|
+
version: '0.48'
|
165
|
+
- !ruby/object:Gem::Dependency
|
166
|
+
name: carrierwave
|
167
|
+
requirement: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - "~>"
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '1.1'
|
172
|
+
type: :development
|
173
|
+
prerelease: false
|
174
|
+
version_requirements: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - "~>"
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '1.1'
|
165
179
|
description: Makes the google-cloud-datastore gem compliant with active_model conventions
|
166
180
|
and compatible with your Rails 5+ applications.
|
167
181
|
email:
|
@@ -174,6 +188,7 @@ files:
|
|
174
188
|
- LICENSE.txt
|
175
189
|
- README.md
|
176
190
|
- lib/active_model/datastore.rb
|
191
|
+
- lib/active_model/datastore/carrier_wave_uploader.rb
|
177
192
|
- lib/active_model/datastore/connection.rb
|
178
193
|
- lib/active_model/datastore/errors.rb
|
179
194
|
- lib/active_model/datastore/nested_attr.rb
|
@@ -201,7 +216,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
201
216
|
version: '0'
|
202
217
|
requirements: []
|
203
218
|
rubyforge_project:
|
204
|
-
rubygems_version: 2.6.
|
219
|
+
rubygems_version: 2.6.11
|
205
220
|
signing_key:
|
206
221
|
specification_version: 4
|
207
222
|
summary: Cloud Datastore integration with Active Model
|