shrine-mongoid 0.2.4 → 1.0.0.beta

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e2b1e8d99cc0c91b6d0803af1c5a43127937b515
4
- data.tar.gz: b1ff874fa3e96079fc196082a246825cdbaa25c1
2
+ SHA256:
3
+ metadata.gz: 45431f2e800408a6e913a40b3dfa5987d78f431554ebf526bfed1b5e250814e9
4
+ data.tar.gz: 592c042cfbeca066f1399b458b933350d109ccd304e835d84245c6f930cc1c7f
5
5
  SHA512:
6
- metadata.gz: 75a577a6573eb7af7f8844de8f2b70b6789fa714614c0648bc33ded56a60ee8b23f93c944005ec82a13e5bdbb62972058697706351037b6efd08930e568ea4a0
7
- data.tar.gz: 2f4da99a99ceae93fd5d75e92e18a0542c9cfe03bacae81645b09bae8e9844ff6bcf8602de069944adff6088d20ab3ca510b99bbe64cbeb91ea0ba6c981629d2
6
+ metadata.gz: 121e53e6388fbdc2577be588825e464fcd4f6e50ae8d9358fd954cfc687e6fb0ecc8d8624621e66c234e84b89290ebc664335243922878231da28a5949760b78
7
+ data.tar.gz: 6daa8495e1780e7e638120169cb7f4ff95e47d46c6c5721653bdb5c15770f8213a60dd30dbf35df927e44c082e8547736a7d560cca886a657ee493a3710e716b
data/README.md CHANGED
@@ -5,7 +5,7 @@ Provides [Mongoid] integration for [Shrine].
5
5
  ## Installation
6
6
 
7
7
  ```ruby
8
- gem "shrine-mongoid"
8
+ gem "shrine-mongoid", "~> 1.0"
9
9
  ```
10
10
 
11
11
  ## Usage
@@ -14,33 +14,165 @@ gem "shrine-mongoid"
14
14
  Shrine.plugin :mongoid
15
15
  ```
16
16
  ```rb
17
- class Post
17
+ class ImageUploader < Shrine
18
+ end
19
+ ```
20
+ ```rb
21
+ class Photo
22
+ include Mongoid::Document
23
+ include ImageUploader::Attachment(:image)
24
+
25
+ field :image_data, type: String # or `type: Hash`
26
+ end
27
+ ```
28
+
29
+ The `Shrine::Attachment` module will add [model] methods, as well as
30
+ [callbacks](#callbacks) and [validations](#validations) to tie attachment
31
+ process to the record lifecycle:
32
+
33
+ ```rb
34
+ photo = Photo.new
35
+
36
+ photo.image = file # cache attachment
37
+
38
+ photo.image #=> #<Shrine::UploadedFile @id="bc2e13.jpg" @storage_key=:cache ...>
39
+ photo.image_data #=> '{"id":"bc2e13.jpg","storage":"cache","metadata":{...}}'
40
+
41
+ photo.save # persist, promote attachment, then persist again
42
+
43
+ photo.image #=> #<Shrine::UploadedFile @id="397eca.jpg" @storage_key=:store ...>
44
+ photo.image_data #=> '{"id":"397eca.jpg","storage":"store","metadata":{...}}'
45
+
46
+ photo.destroy # delete attachment
47
+
48
+ photo.image.exists? #=> false
49
+ ```
50
+
51
+ ### Callbacks
52
+
53
+ #### After Save
54
+
55
+ After a record is saved, `Attacher#finalize` is called, which promotes cached
56
+ file to permanent storage and deletes previous file if any.
57
+
58
+ ```rb
59
+ photo = Photo.new
60
+
61
+ photo.image = file
62
+ photo.image.storage_key #=> :cache
63
+
64
+ photo.save
65
+ photo.image.storage_key #=> :store
66
+ ```
67
+
68
+ #### After Destroy
69
+
70
+ After a record is destroyed, `Attacher#destroy_attached` method is called,
71
+ which deletes stored attached file if any.
72
+
73
+ ```rb
74
+ photo = Photo.find(photo_id)
75
+ photo.image #=> #<Shrine::UploadedFile>
76
+ photo.image.exists? #=> true
77
+
78
+ photo.destroy
79
+ photo.image.exists? #=> false
80
+ ```
81
+
82
+ #### Skipping Callbacks
83
+
84
+ If you don't want the attachment module to add any callbacks to your model, you
85
+ can set `:callbacks` to `false`:
86
+
87
+ ```rb
88
+ plugin :mongoid, callbacks: false
89
+ ```
90
+
91
+ ### Validations
92
+
93
+ If you're using the [`validation`][validation] plugin, the attachment module
94
+ will automatically merge attacher errors with model errors.
95
+
96
+ ```rb
97
+ class ImageUploader < Shrine
98
+ plugin :validation_helpers
99
+
100
+ Attacher.validate do
101
+ validate_max_size 10 * 1024 * 1024
102
+ end
103
+ end
104
+ ```
105
+ ```rb
106
+ photo = Photo.new
107
+ photo.image = file
108
+ photo.valid?
109
+ photo.errors #=> { image: ["size must not be greater than 10.0 MB"] }
110
+ ```
111
+
112
+ #### Attachment Presence
113
+
114
+ If you want to validate presence of the attachment, you can use ActiveModel's
115
+ presence validator:
116
+
117
+ ```rb
118
+ class Photo
18
119
  include Mongoid::Document
19
- include ImageUploader[:image]
120
+ include ImageUploader::Attachment(:image)
20
121
 
21
- field :image_data, type: String
122
+ validates_presence_of :image
22
123
  end
23
124
  ```
24
125
 
25
- This plugin will add validations and callbacks:
126
+ #### Skipping Validations
127
+
128
+ If don't want the attachment module to merge file validations errors into
129
+ model errors, you can set `:validations` to `false`:
26
130
 
27
131
  ```rb
28
- post = Post.new
29
- post.image = file
30
- post.image.storage_key #=> "cache"
31
- post.save
32
- post.image.storage_key #=> "store"
33
- post.destroy
34
- post.image.exists? #=> false
132
+ plugin :mongoid, validations: false
35
133
  ```
36
134
 
37
- If for some reason you don't want callbacks and/or validations, you can turn
38
- them off:
135
+ ## Attacher
136
+
137
+ You can also use `Shrine::Attacher` directly (with or without the
138
+ `Shrine::Attachment` module):
139
+
140
+ ```rb
141
+ class Photo
142
+ include Mongoid::Document
39
143
 
144
+ field :image_data, type: String # or `type: Hash`
145
+ end
146
+ ```
40
147
  ```rb
41
- plugin :mongoid, callbacks: false, validations: false
148
+ photo = Photo.new
149
+ attacher = ImageUploader::Attacher.from_model(photo, :image)
150
+
151
+ attacher.assign(file) # cache
152
+
153
+ attacher.file #=> #<Shrine::UploadedFile @id="bc2e13.jpg" @storage_key=:cache ...>
154
+ photo.image_data #=> '{"id":"bc2e13.jpg","storage":"cache","metadata":{...}}'
155
+
156
+ photo.save # persist
157
+ attacher.finalize # promote
158
+ photo.save # persist
159
+
160
+ attacher.file #=> #<Shrine::UploadedFile @id="397eca.jpg" @storage_key=:store ...>
161
+ photo.image_data #=> '{"id":"397eca.jpg","storage":"store","metadata":{...}}'
42
162
  ```
43
163
 
164
+ ### Pesistence
165
+
166
+ The following persistence methods are added to `Shrine::Attacher`:
167
+
168
+ | Method | Description |
169
+ | :----- | :---------- |
170
+ | `Attacher#atomic_promote` | calls `Attacher#promote` and persists if the attachment hasn't changed |
171
+ | `Attacher#atomic_persist` | saves changes if the attachment hasn't changed |
172
+ | `Attacher#persist` | saves any changes to the underlying record |
173
+
174
+ See [persistence] docs for more details.
175
+
44
176
  ## Contributing
45
177
 
46
178
  You can run the tests with the Rake task:
@@ -54,4 +186,7 @@ $ bundle exec rake test
54
186
  [MIT](LICENSE.txt)
55
187
 
56
188
  [Mongoid]: https://github.com/mongodb/mongoid
57
- [Shrine]: https://github.com/janko-m/shrine
189
+ [Shrine]: https://github.com/shrinerb/shrine
190
+ [model]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/model.md#readme
191
+ [validation]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/validation.md#readme
192
+ [persistence]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/persistence.md#readme
@@ -1,11 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "mongoid"
2
4
 
3
5
  class Shrine
4
6
  module Plugins
5
7
  module Mongoid
6
- def self.configure(uploader, opts = {})
7
- uploader.opts[:mongoid_callbacks] = opts.fetch(:callbacks, uploader.opts.fetch(:mongoid_callbacks, true))
8
- uploader.opts[:mongoid_validations] = opts.fetch(:validations, uploader.opts.fetch(:mongoid_validations, true))
8
+ def self.load_dependencies(uploader, *)
9
+ uploader.plugin :model
10
+ uploader.plugin :_persistence, plugin: self
11
+ end
12
+
13
+ def self.configure(uploader, **opts)
14
+ uploader.opts[:mongoid] ||= { validations: true, callbacks: true }
15
+ uploader.opts[:mongoid].merge!(opts)
9
16
  end
10
17
 
11
18
  module AttachmentMethods
@@ -14,59 +21,81 @@ class Shrine
14
21
 
15
22
  return unless model < ::Mongoid::Document
16
23
 
17
- if shrine_class.opts[:mongoid_validations]
18
- model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
19
- validate do
20
- #{@name}_attacher.errors.each do |message|
21
- errors.add(:#{@name}, message)
22
- end
24
+ name = @name
25
+
26
+ if shrine_class.opts[:mongoid][:validations]
27
+ # add validation plugin integration
28
+ model.validate do
29
+ next unless send(:"#{name}_attacher").respond_to?(:errors)
30
+
31
+ send(:"#{name}_attacher").errors.each do |message|
32
+ errors.add(name, message)
23
33
  end
24
- RUBY
34
+ end
25
35
  end
26
36
 
27
- if shrine_class.opts[:mongoid_callbacks]
28
- model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
29
- before_save do
30
- #{@name}_attacher.save if #{@name}_attacher.attached?
37
+ if shrine_class.opts[:mongoid][:callbacks]
38
+ model.before_save do
39
+ if send(:"#{name}_attacher").changed?
40
+ send(:"#{name}_attacher").save
31
41
  end
42
+ end
32
43
 
33
- after_save do
34
- #{@name}_attacher.finalize if #{@name}_attacher.attached?
44
+ model.after_save do
45
+ if send(:"#{name}_attacher").changed?
46
+ send(:"#{name}_attacher").finalize
47
+ send(:"#{name}_attacher").persist
35
48
  end
49
+ end
36
50
 
37
- after_destroy do
38
- #{@name}_attacher.destroy
39
- end
40
- RUBY
51
+ model.after_destroy do
52
+ send(:"#{name}_attacher").destroy_attached
53
+ end
41
54
  end
42
- end
43
- end
44
55
 
45
- module AttacherClassMethods
46
- # Needed by the backgrounding plugin.
47
- def find_record(record_class, record_id)
48
- record_class.where(id: record_id).first
56
+ define_method :reload do |*args|
57
+ result = super(*args)
58
+ instance_variable_set(:"@#{name}_attacher", nil)
59
+ result
60
+ end
49
61
  end
50
62
  end
51
63
 
64
+ # The _persistence plugin uses #mongoid_persist, #mongoid_reload and
65
+ # #mongoid? to implement the following methods:
66
+ #
67
+ # * Attacher#persist
68
+ # * Attacher#atomic_persist
69
+ # * Attacher#atomic_promote
52
70
  module AttacherMethods
53
71
  private
54
72
 
55
- # We save the record after updating, raising any validation errors.
56
- def update(uploaded_file)
57
- super
58
- record.save!
73
+ # Saves changes to the model instance, raising exception on validation
74
+ # errors. Used by the _persistence plugin.
75
+ def mongoid_persist
76
+ record.save(validate: false)
59
77
  end
60
78
 
61
- def convert_before_write(value)
62
- mongoid_hash_field? ? value : super
79
+ # Yields the reloaded record. Used by the _persistence plugin.
80
+ def mongoid_reload
81
+ record_copy = record.dup
82
+ record_copy.id = record.id
83
+
84
+ yield record_copy.reload
63
85
  end
64
86
 
65
- def mongoid_hash_field?
66
- return false unless record.is_a?(::Mongoid::Document)
67
- return false unless field = record.class.fields[data_attribute.to_s]
87
+ # Returns true if the data attribute represents a Hash field. Used by
88
+ # the _persistence plugin to determine whether serialization should be
89
+ # skipped.
90
+ def mongoid_hash_attribute?
91
+ field = record.class.fields[attribute.to_s]
92
+ field && field.type == Hash
93
+ end
68
94
 
69
- field.type == Hash
95
+ # Returns whether the record is a Mongoid document. Used by the
96
+ # _persistence plugin.
97
+ def mongoid?
98
+ record.is_a?(::Mongoid::Document)
70
99
  end
71
100
  end
72
101
  end
@@ -1,11 +1,11 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-mongoid"
3
- gem.version = "0.2.4"
3
+ gem.version = "1.0.0.beta"
4
4
 
5
- gem.required_ruby_version = ">= 2.1"
5
+ gem.required_ruby_version = ">= 2.3"
6
6
 
7
7
  gem.summary = "Provides Mongoid integration for Shrine."
8
- gem.homepage = "https://github.com/janko-m/shrine-mongoid"
8
+ gem.homepage = "https://github.com/shrinerb/shrine-mongoid"
9
9
  gem.authors = ["Janko Marohnić"]
10
10
  gem.email = ["janko.marohnic@gmail.com"]
11
11
  gem.license = "MIT"
@@ -13,10 +13,9 @@ Gem::Specification.new do |gem|
13
13
  gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "*.gemspec"]
14
14
  gem.require_path = "lib"
15
15
 
16
- gem.add_dependency "shrine", "~> 2.4"
16
+ gem.add_dependency "shrine", ">= 3.0.0.beta2", "< 4"
17
17
  gem.add_dependency "mongoid", ">= 5.0"
18
18
 
19
- gem.add_development_dependency "shrine-memory"
20
19
  gem.add_development_dependency "rake"
21
20
  gem.add_development_dependency "minitest"
22
21
  gem.add_development_dependency "mocha"
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 1.0.0.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-15 00:00:00.000000000 Z
11
+ date: 2019-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shrine
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0.beta2
20
+ - - "<"
18
21
  - !ruby/object:Gem::Version
19
- version: '2.4'
22
+ version: '4'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: '2.4'
29
+ version: 3.0.0.beta2
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: mongoid
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -38,20 +44,6 @@ dependencies:
38
44
  - - ">="
39
45
  - !ruby/object:Gem::Version
40
46
  version: '5.0'
41
- - !ruby/object:Gem::Dependency
42
- name: shrine-memory
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
47
  - !ruby/object:Gem::Dependency
56
48
  name: rake
57
49
  requirement: !ruby/object:Gem::Requirement
@@ -105,7 +97,7 @@ files:
105
97
  - README.md
106
98
  - lib/shrine/plugins/mongoid.rb
107
99
  - shrine-mongoid.gemspec
108
- homepage: https://github.com/janko-m/shrine-mongoid
100
+ homepage: https://github.com/shrinerb/shrine-mongoid
109
101
  licenses:
110
102
  - MIT
111
103
  metadata: {}
@@ -117,15 +109,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
109
  requirements:
118
110
  - - ">="
119
111
  - !ruby/object:Gem::Version
120
- version: '2.1'
112
+ version: '2.3'
121
113
  required_rubygems_version: !ruby/object:Gem::Requirement
122
114
  requirements:
123
- - - ">="
115
+ - - ">"
124
116
  - !ruby/object:Gem::Version
125
- version: '0'
117
+ version: 1.3.1
126
118
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 2.5.1
119
+ rubygems_version: 3.0.3
129
120
  signing_key:
130
121
  specification_version: 4
131
122
  summary: Provides Mongoid integration for Shrine.