Class: Voltron::Upload::Field::UploadField

Inherits:
Object
  • Object
show all
Includes:
ActionDispatch::Routing::PolymorphicRoutes, ActionView::Helpers::TextHelper
Defined in:
lib/voltron/upload/action_view/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, method, template, options) ⇒ UploadField

Returns a new instance of UploadField.



31
32
33
34
35
36
37
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/voltron/upload/action_view/field.rb', line 29

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template.



29
30
31
# File 'lib/voltron/upload/action_view/field.rb', line 29

def template
  @template
end

Instance Method Details

#acceptObject



92
93
94
# File 'lib/voltron/upload/action_view/field.rb', line 92

def accept
  @accept ||= options.delete(:accept).to_s
end

#cachesObject



136
137
138
139
140
141
142
143
# 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

#filesObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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?Boolean

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

Returns:

  • (Boolean)


102
103
104
# File 'lib/voltron/upload/action_view/field.rb', line 102

def has_preview_markup?
  preview.present?
end

#has_preview_template?Boolean

Returns:

  • (Boolean)


96
97
98
# 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_nameObject

def add_preview_class

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

end



113
114
115
# 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?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/voltron/upload/action_view/field.rb', line 117

def multiple?
  @model.respond_to?("#{@method}_urls")
end

#prepareObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

#previewObject



88
89
90
# File 'lib/voltron/upload/action_view/field.rb', line 88

def preview
  @preview ||= options.delete(:preview).to_s
end

#preview_markupObject



73
74
75
76
77
78
79
80
81
# 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_nameObject

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



84
85
86
# File 'lib/voltron/upload/action_view/field.rb', line 84

def preview_name
  strip_tags(preview)
end

#preview_optionsObject



63
64
65
66
67
68
69
70
71
# 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

#removalsObject



145
146
147
# File 'lib/voltron/upload/action_view/field.rb', line 145

def removals
  Array.wrap(@model.send("remove_#{@method}")).compact.reject { |i| !i }
end