Class: Voltron::Uploader
- Inherits:
-
Object
- Object
- Voltron::Uploader
- Defined in:
- lib/voltron/uploader.rb
Instance Attribute Summary collapse
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
-
#columns ⇒ Object
Get a hash of uploader columns and whether or not it accepts multiple uploads i.e.
- #files_from(model) ⇒ Object
-
#initialize(resource) ⇒ Uploader
constructor
A new instance of Uploader.
- #multiple?(column) ⇒ Boolean
-
#permitted_params ⇒ Object
List of permitted parameters needed for upload action.
- #process!(params) ⇒ Object
-
#resource_name ⇒ Object
Resource name as it would appear in the params hash.
Constructor Details
#initialize(resource) ⇒ Uploader
Returns a new instance of Uploader.
6 7 8 |
# File 'lib/voltron/uploader.rb', line 6 def initialize(resource) @resource = resource.to_s.classify.safe_constantize end |
Instance Attribute Details
#resource ⇒ Object
Returns the value of attribute resource.
4 5 6 |
# File 'lib/voltron/uploader.rb', line 4 def resource @resource end |
Instance Method Details
#columns ⇒ Object
Get a hash of uploader columns and whether or not it accepts multiple uploads i.e. - { column => multiple_uploads? } i.e. - { avatar: false }
46 47 48 49 50 |
# File 'lib/voltron/uploader.rb', line 46 def columns @instance ||= resource.new uploaders = resource.uploaders.keys.map(&:to_s) resource.uploaders.map { |k,v| { k.to_s => multiple?(k) } }.reduce(Hash.new, :merge) end |
#files_from(model) ⇒ Object
39 40 41 |
# File 'lib/voltron/uploader.rb', line 39 def files_from(model) model.slice(columns.keys).values.flatten.reject(&:blank?).map(&:to_upload_json) end |
#multiple?(column) ⇒ Boolean
52 53 54 55 |
# File 'lib/voltron/uploader.rb', line 52 def multiple?(column) @instance ||= resource.new @instance.respond_to?("#{column}_urls") end |
#permitted_params ⇒ Object
List of permitted parameters needed for upload action
16 17 18 19 |
# File 'lib/voltron/uploader.rb', line 16 def permitted_params columns.keys.map(&:to_sym) #.map { |name, multiple| multiple ? { name => [] } : name } end |
#process!(params) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/voltron/uploader.rb', line 21 def process!(params) params = params.map { |column, value| { column => multiple?(column) && value.is_a?(Array) ? value.map(&:values).flatten : value } }.reduce(Hash.new, :merge) model = resource.new(params) # Test the validity, get the errors if any model.valid? # Remove all errors that were not related to an uploader, they're expected in this case (model.errors.keys - resource.uploaders.keys).each { |k| model.errors.delete k } if model.errors.any? # If any errors, return the messages raise ::Voltron::Upload::Error.new(model.errors.) else { uploads: files_from(model) } end end |
#resource_name ⇒ Object
Resource name as it would appear in the params hash
11 12 13 |
# File 'lib/voltron/uploader.rb', line 11 def resource_name resource.name.singularize.underscore.to_sym end |