class Voltron::Upload::Field::UploadField

Attributes

options[R]
template[R]

Public Class Methods

new(model, method, template, options) click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 31
def initialize(model, method, template, options)
  @model = model
  @method = method.to_sym
  @template = template
  @options = options.with_indifferent_access
  prepare
end

Public Instance Methods

accept() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 92
def accept
  @accept ||= options.delete(:accept).to_s
end
caches() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 136
def caches
  if multiple?
    cache = @model.send("cache_#{@method}") rescue []
    Array.wrap(cache)
  else
    Array.wrap(@model.send("cache_#{@method}"))
  end
end
files() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 121
def files
  # If set to not preserve files, return an empty array so nothing is shown
  return [] if options[:preserve] === false

  # Return an array of files' json data
  uploads = Array.wrap(@model.send(@method)).map(&:to_upload_json).reject(&:blank?)

  # Remove all uploads from the array that have been flagged for removal
  removals.each do |removal|
    uploads.reject! { |upload| upload[:id] == removal }
  end

  uploads
end
has_preview_markup?() click to toggle source

Eventually, consider utilizing Nokogiri to detect whether content also is actually HTML markup Right now the overhead and frustration of that gem is not worth it

# File lib/voltron/upload/action_view/field.rb, line 102
def has_preview_markup?
  preview.present?
end
has_preview_template?() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 96
def has_preview_template?
  preview_name.present? && template.lookup_context.exists?(preview_name, 'voltron/upload/preview', true)
end
input_name() click to toggle source

def add_preview_class

options[:class] ||= ''
classes = options[:class].split(/\s+/)
classes << "dz-layout-#{preview_name}"
options[:class] = classes.join(' ')

end

# File lib/voltron/upload/action_view/field.rb, line 113
def input_name
  ActionView::Helpers::Tags::Base.new(ActiveModel::Naming.param_key(@model), @method, nil).send(:tag_name) + (multiple? ? '[]' : '')
end
multiple?() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 117
def multiple?
  @model.respond_to?("#{@method}_urls")
end
prepare() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 39
def prepare
  #add_preview_class if has_preview_template?

  options.merge!({
    ':multiple'       => multiple?,
    ':files'          => files.to_json,
    ':cached'         => caches.to_json,
    ':removed'        => removals.to_json,
    ':upload_options' => preview_options.to_json,
    'accept'          => accept,
    'preview'         => preview_name,
    'param'           => input_name,
    'url'             => polymorphic_path(@model.class, action: :upload)
  })

  #options[:data] ||= {}
  #options[:data].merge!({
  #  upload_files: files,
  #  upload_cache: caches,
  #  upload_remove: removals,
  #  upload_options: preview_options
  #})
end
preview() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 88
def preview
  @preview ||= options.delete(:preview).to_s
end
preview_markup() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 73
def preview_markup
  if has_preview_template?
    # Fetch the html found in the partial provided
    ActionController::Base.new.render_to_string(partial: "voltron/upload/preview/#{preview_name}").squish
  elsif has_preview_markup?
    # If not blank, value of +preview+ is likely (should be) raw html, in which case, just return that markup
    preview.squish
  end
end
preview_name() click to toggle source

Strip tags, they cause problems in the lookup_context exists? and render_to_string

# File lib/voltron/upload/action_view/field.rb, line 84
def preview_name
  strip_tags(preview)
end
preview_options() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 63
def preview_options
  previews = Voltron.config.upload.previews || {}
  opts = previews.with_indifferent_access.try(:[], preview_name) || {}
  opts.merge!({
    preview_template: preview_markup,
  })
  opts.merge!(options.delete(:options) || {})
  opts.map { |k,v| { k.to_s.camelize(:lower) => v } }.reduce(Hash.new, :merge).compact
end
removals() click to toggle source
# File lib/voltron/upload/action_view/field.rb, line 145
def removals
  Array.wrap(@model.send("remove_#{@method}")).compact.reject { |i| !i }
end