effective_assets 1.7.1 → 1.7.2

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
2
  SHA1:
3
- metadata.gz: 15d7f1fbde41fa5f9ff34ef4f5bd305b93577857
4
- data.tar.gz: 3c480a731adcffefb4e1ff08ace835720554fa95
3
+ metadata.gz: b046d827dbdb53336ae75966f6e0b583d89eb392
4
+ data.tar.gz: df4319599899bb44a4ca80712f2825f9f02cf092
5
5
  SHA512:
6
- metadata.gz: a10fe066f8b42ff90545dee4952500dc5f599011adab2f249fb9b603b533452b49ff4a0c4219263296ad09bff6664e4447469d8543457d744805f2d73d96c68a
7
- data.tar.gz: 54179c2d4751645677c8da64df9a17d4a630b7b7e31fedfee0521286c13a3d262f48fd1dca6e9711a0678a002f3f598aded57b216884412c2d5f75e1f1e722a6
6
+ metadata.gz: eddcb8b9ddf413f78f399653b7fd8afe48ff51c332ec6bb354a99f43ade3362e64c16742bd295c09bdc90b0130b3355cba05f5276684abe5cbb429a68e02619d
7
+ data.tar.gz: fbf9f513853d9b99c1d216333c30eec15c0ee64c38a9feaf9176e0c4e893c4dcd3a467017eaeed28236a23f4792c0b2b965a9cc72e032efa7c1cea82ddca76b1
data/README.md CHANGED
@@ -193,6 +193,8 @@ user.photos
193
193
 
194
194
  ### Validations
195
195
 
196
+ You can validate the presence and length of the assets with a simple syntax:
197
+
196
198
  ```ruby
197
199
  class User
198
200
  acts_as_asset_box :avatar => true, :photos => false, :videos => 2, :mp3s => 5..10
@@ -201,8 +203,18 @@ end
201
203
 
202
204
  true means presence, false means no validations applied.
203
205
 
206
+ or a more involved syntax:
207
+
208
+ ```ruby
209
+ class User
210
+ acts_as_asset_box avatar: [presence: true], photos: false, videos: [length: 2], mp3s: [length: 5..10]
211
+ end
212
+ ```
213
+
204
214
  The user in this example is only valid if exists an avatar, 2 videos, and 5..10 mp3s.
205
215
 
216
+ Procs are not supported.
217
+
206
218
  ### Form Input
207
219
 
208
220
  There is a standard rails form input:
@@ -251,10 +263,10 @@ Use the custom form input for uploading (direct to S3) and attaching assets to t
251
263
  = f.input :pictures, :as => :asset_box, :dialog => true, :dialog_url => '/admin/effective_assets' # Use the attach dialog
252
264
  ```
253
265
 
254
- You may also upload secure (AWS: 'authenticated-read') assets with the same uploader
266
+ You may also upload secure (AWS: 'authenticated-read') assets with the same uploader:
255
267
 
256
268
  ```ruby
257
- = f.input :pictures, :as => :asset_box, :aws_acl => 'authenticated-read'
269
+ = f.input :pictures, :as => :asset_box, :private => true
258
270
  ```
259
271
 
260
272
  There is also a mechanism for collecting additional information from the upload form which will be set in the `asset.extra` field.
@@ -305,6 +317,41 @@ The permitted parameters are:
305
317
  :attachments_attributes => [:id, :asset_id, :attachable_type, :attachable_id, :position, :box, :_destroy]
306
318
  ```
307
319
 
320
+ ### Amazon S3 Public / Private
321
+
322
+ When an asset is uploaded, it is created with an `aws_acl` of either `public-read` or `authenticated-read`.
323
+
324
+ When in `authenticated-read` mode, calling `@asset.url` will return a URL that is valid for just 60 minutes.
325
+
326
+ This privacy level can be configured in the following 3 ways:
327
+
328
+ 1. The app wide default is set in `config/initializers/effective_assets.rb`. All Effective::Asset objects will be created with this ACL unless specified below.
329
+
330
+ 2. When you call `acts_as_asset_box` on a model, the `:private` or `:public` keys will define the behavior for just this asset box.
331
+
332
+ ```ruby
333
+ acts_as_asset_box :avatar => :private
334
+ acts_as_asset_box :avatar => :public
335
+ ```
336
+
337
+ or, with validations:
338
+
339
+ ```ruby
340
+ acts_as_asset_box :avatar => [presence: true, private: true]
341
+ acts_as_asset_box :avatar => [presence: true, public: true]
342
+ ```
343
+
344
+ All assets uploaded into this box will be created with this ACL unless overridden below.
345
+
346
+ 3. Set the ACL on the form upload field
347
+
348
+ ```ruby
349
+ = f.input :avatar, :as => :asset_box, :private => true
350
+ = f.input :avatar, :as => :asset_box, :public => true
351
+ ```
352
+
353
+ Has final say over the privacy setting when uploaded from this form.
354
+
308
355
 
309
356
  ### Image Processing and Resizing
310
357
 
@@ -97,7 +97,13 @@ if defined?(ActiveAdmin)
97
97
  end
98
98
 
99
99
  member_action :reprocess, :method => :get do
100
- asset = Effective::Asset.find(params[:id]).reprocess!
100
+ begin
101
+ Effective::Asset.find(params[:id]).reprocess!
102
+ flash[:success] = "Successfully reprocessed asset"
103
+ rescue => e
104
+ flash[:danger] = "Unable to process asset: #{e.message}"
105
+ end
106
+
101
107
  redirect_to admin_effective_assets_path
102
108
  end
103
109
  end
@@ -82,6 +82,8 @@ module Effective
82
82
  asset.aws_acl = opts[:aws_acl]
83
83
  asset[:data] = opts[:title]
84
84
 
85
+ asset.processed = true unless asset.image?
86
+
85
87
  # If our S3 Uploader has any issue uploading/saving the asset, destroy the placeholder empty one
86
88
  asset.save ? true : (asset.try(:destroy) and false)
87
89
  end
@@ -57,7 +57,7 @@ module EffectiveAssetsS3Helper
57
57
  end
58
58
 
59
59
  def signature
60
- Rails.logger.info 'WARNING: effective_assets config.aws_secret_access_key is blank. Please provide a value in config/initializers/effective_assets.rb' if @options[:aws_secret_access_key].blank?
60
+ raise 'effective_assets config.aws_secret_access_key is blank. Please provide a value in config/initializers/effective_assets.rb' if @options[:aws_secret_access_key].blank?
61
61
 
62
62
  Base64.encode64(
63
63
  OpenSSL::HMAC.digest(
@@ -26,6 +26,8 @@
26
26
  module ActsAsAssetBox
27
27
  extend ActiveSupport::Concern
28
28
 
29
+ VALIDATION_KEYS = [:presence, :length, :inclusion, :private, :public]
30
+
29
31
  module ActiveRecord
30
32
  def acts_as_asset_box(*options)
31
33
  @acts_as_asset_box_opts = options || []
@@ -50,10 +52,40 @@ module ActsAsAssetBox
50
52
  boxes.each do |box, validation|
51
53
  self.send(:define_method, box) { effective_assets(box) }
52
54
 
53
- if validation == true
55
+ # So we want to support 2 kinds of syntax here
56
+ # Syntax 1:
57
+ # acts_as_asset_box files: true # Presence
58
+ # acts_as_asset_box files: false # Nothing
59
+ # acts_as_asset_box files: 2 # Exactly 2 files
60
+ # acts_as_asset_box files: 5..10 # 5 to 10 photos
61
+ # acts_as_asset_box files: :private # AWS ACL private
62
+ # acts_as_asset_box files: :public # AWS ACL private
63
+ #
64
+ # Syntax 2:
65
+ # acts_as_asset_box files: [presence: true, length: 5..2, private: true], another: :private
66
+
67
+ if validation == true || validation == :presence
54
68
  validates box, :asset_box_presence => true
55
69
  elsif validation.kind_of?(Integer) or validation.kind_of?(Range)
56
70
  validates box, :asset_box_length => validation
71
+ elsif validation == :private || validation == :public
72
+ # Do nothing
73
+ else
74
+ validations = Array(validation).first
75
+
76
+ if validations.kind_of?(Hash) != true
77
+ raise 'invalid parameters passed to acts_as_asset_box. Example Usage: acts_as_asset_box photos: [presence: true, length: 1..3, private: true]'
78
+ elsif (validations.keys - VALIDATION_KEYS).present?
79
+ raise "invalid key #{validations.keys - VALIDATION_KEYS} passed to acts_as_asset_box. Valid keys are: #{VALIDATION_KEYS}"
80
+ end
81
+
82
+ if validations[:presence]
83
+ validates box, :asset_box_presence => true
84
+ end
85
+
86
+ if validations[:length] || validations[:inclusion]
87
+ validates box, :asset_box_length => (validations[:length] || validations[:inclusion])
88
+ end
57
89
  end
58
90
  end
59
91
  else
@@ -181,23 +181,17 @@ module Effective
181
181
  end
182
182
 
183
183
  def process!
184
- begin
185
- Rails.logger.info "Processing ##{id}..."
186
- print "Processing ##{id}..."
187
-
188
- data.cache_stored_file!
189
- data.retrieve_from_cache!(data.cache_name)
190
- data.recreate_versions!
191
- save!
192
-
193
- Rails.logger.info "Successfully processed ##{id}"
194
- print "success"; puts ''
195
- true
196
- rescue => e
197
- Rails.logger.info "Error: #{id} -> #{e.to_s}"
198
- print "error: #{e.to_s}"; puts ''
199
- false
184
+ if data.respond_to?(:aws_acl) && aws_acl.present?
185
+ data.aws_acl = self.aws_acl
186
+ data.versions.each { |_, data| data.aws_acl = self.aws_acl }
200
187
  end
188
+
189
+ data.cache_stored_file!
190
+ data.retrieve_from_cache!(data.cache_name)
191
+ data.recreate_versions!
192
+
193
+ self.processed = true
194
+ save!
201
195
  end
202
196
  alias_method :reprocess!, :process!
203
197
 
@@ -242,11 +236,10 @@ module Effective
242
236
  end
243
237
 
244
238
  def enqueue_delayed_job
245
- if !self.processed && self.upload_file.present? && self.upload_file != 'placeholder'
246
- Effective::DelayedJob.new.process_asset(self.id)
239
+ if !processed? && upload_file.present? && upload_file != 'placeholder'
240
+ Effective::DelayedJob.new.process_asset(id)
247
241
  end
248
242
  end
249
-
250
243
  end
251
244
 
252
245
  class AssetStringIO < StringIO
@@ -49,7 +49,19 @@ module Effective
49
49
  handle_asynchronously :process_asset
50
50
 
51
51
  def reprocess_asset(id)
52
- Effective::Asset.find(id).reprocess!
52
+ Rails.logger.info "Processing ##{id}..."
53
+ print "Processing ##{id}..."
54
+
55
+ begin
56
+ Effective::Asset.find(id).reprocess!
57
+
58
+ Rails.logger.info "Successfully processed ##{id}"
59
+ print "success"; puts ''
60
+ rescue => e
61
+ Rails.logger.info "Error: #{id} -> #{e.to_s}"
62
+ print "error: #{e.to_s}"; puts ''
63
+ end
64
+
53
65
  (GC.start rescue nil)
54
66
  end
55
67
  handle_asynchronously :reprocess_asset
@@ -157,7 +157,6 @@ module Inputs
157
157
  :dialog_url => @template.effective_assets.effective_assets_path,
158
158
  :disabled => false,
159
159
  :file_types => [:any],
160
- :aws_acl => EffectiveAssets.aws_acl,
161
160
  :btn_label => "Upload..."
162
161
  }.merge(opts).tap do |options|
163
162
  options[:method] = method.to_s
@@ -165,6 +164,28 @@ module Inputs
165
164
  options[:attachable_id] ||= (@object.try(:id) rescue nil)
166
165
  options[:attachable_type] ||= @object.class.name.titleize.gsub(" ", "_").gsub('/', '_').downcase
167
166
 
167
+ # The logic for the AWS ACL is such that:
168
+ # 1.) If the :private or :aws_acl keys are set on the asset_box input, use those values
169
+ # 2.) If the :private or :public keys are set on the acts_as_asset_box declaration, use those values
170
+ # 3.) Fall back to default EffectiveAssets.aws_acl as per config file
171
+
172
+ uploader_private = (opts[:private] == true || opts[:public] == false || opts[:aws_acl] == EffectiveAssets::AWS_PRIVATE)
173
+ uploader_public = (opts[:private] == false || opts[:public] == true || opts[:aws_acl] == EffectiveAssets::AWS_PUBLIC)
174
+ object_private = ((@object.asset_boxes[method] == :private || @object.asset_boxes[method].first[:private] == true || @object.asset_boxes[method].first[:public] == false) rescue false)
175
+ object_public = ((@object.asset_boxes[method] == :public || @object.asset_boxes[method].first[:public] == true || @object.asset_boxes[method].first[:private] == false) rescue false)
176
+
177
+ if uploader_private
178
+ options[:aws_acl] = EffectiveAssets::AWS_PRIVATE
179
+ elsif uploader_public
180
+ options[:aws_acl] = EffectiveAssets::AWS_PUBLIC
181
+ elsif object_private
182
+ options[:aws_acl] = EffectiveAssets::AWS_PRIVATE
183
+ elsif object_public
184
+ options[:aws_acl] = EffectiveAssets::AWS_PUBLIC
185
+ else
186
+ options[:aws_acl] = EffectiveAssets.aws_acl
187
+ end
188
+
168
189
  options[:uid] = "#{options[:attachable_type]}-#{options[:attachable_id]}-#{options[:method]}-#{Time.zone.now.to_f}".parameterize
169
190
  options[:limit] = (options[:method] == options[:box] ? (options[:limit] || 10000) : 1)
170
191
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveAssets
2
- VERSION = '1.7.1'.freeze
2
+ VERSION = '1.7.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 1.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-17 00:00:00.000000000 Z
11
+ date: 2016-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails