Class: Uploader::Helpers::FieldTag

Inherits:
Object
  • Object
show all
Defined in:
lib/uploader/helpers/field_tag.rb

Constant Summary collapse

RESERVED_OPTIONS_KEYS =
%w[method_name object_name theme value object sortable].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name, method_name, template, options = {}) ⇒ FieldTag

Wrapper for render uploader field Usage:

uploader = FieldTag.new(object_name, method_name, template, options)
uploader.render


18
19
20
21
22
23
24
25
26
27
# File 'lib/uploader/helpers/field_tag.rb', line 18

def initialize(object_name, method_name, template, options = {}) #:nodoc:
  @options = { object_name: object_name, method_name: method_name }.merge(options)
  @template = template

  @theme = (@options.delete(:theme) || 'default')
  @value = @options.delete(:value) if @options.key?(:value)

  @object = @options.delete(:object) if @options.key?(:object)
  @object ||= @template.instance_variable_get("@#{object_name}")
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



8
9
10
# File 'lib/uploader/helpers/field_tag.rb', line 8

def object
  @object
end

#templateObject (readonly)

Returns the value of attribute template.



8
9
10
# File 'lib/uploader/helpers/field_tag.rb', line 8

def template
  @template
end

#themeObject (readonly)

Returns the value of attribute theme.



8
9
10
# File 'lib/uploader/helpers/field_tag.rb', line 8

def theme
  @theme
end

Instance Method Details

#attachments_path(options = {}) ⇒ Object



70
71
72
73
# File 'lib/uploader/helpers/field_tag.rb', line 70

def attachments_path(options = {})
  options = @object.fileupload_params(method_name).merge(options)
  uploader.attachments_path(options)
end

#exists?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/uploader/helpers/field_tag.rb', line 58

def exists?
  values.map(&:persisted?).any?
end

#extension_whitelistObject



87
88
89
# File 'lib/uploader/helpers/field_tag.rb', line 87

def extension_whitelist
  @extension_whitelist ||= extract_extension_whitelist
end

#idObject



34
35
36
# File 'lib/uploader/helpers/field_tag.rb', line 34

def id
  @id ||= @template.dom_id(@object, [method_name, 'uploader'].join('_'))
end

#input_htmlObject



75
76
77
78
79
80
81
# File 'lib/uploader/helpers/field_tag.rb', line 75

def input_html
  @input_html ||= { multiple: multiple?, class: 'uploader' }.merge(input_html_options)
  @input_html[:data] ||= {}
  @input_html[:data][:url] ||= attachments_path(singular: !multiple?)
  @input_html[:accept] ||= extension_whitelist
  @input_html
end

#input_html_optionsObject



83
84
85
# File 'lib/uploader/helpers/field_tag.rb', line 83

def input_html_options
  @options.reject { |key, _value| RESERVED_OPTIONS_KEYS.include?(key.to_s) }
end

#klassObject



66
67
68
# File 'lib/uploader/helpers/field_tag.rb', line 66

def klass
  @klass ||= @object.fileupload_klass(method_name)
end

#method_nameObject



38
39
40
# File 'lib/uploader/helpers/field_tag.rb', line 38

def method_name
  @options[:method_name]
end

#multiple?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/uploader/helpers/field_tag.rb', line 46

def multiple?
  @object.fileupload_multiple?(method_name)
end

#object_nameObject



42
43
44
# File 'lib/uploader/helpers/field_tag.rb', line 42

def object_name
  @options[:object_name]
end

#render(locals = {}) ⇒ Object

:nodoc:



29
30
31
32
# File 'lib/uploader/helpers/field_tag.rb', line 29

def render(locals = {}) #:nodoc:
  locals = { field: self }.merge(locals)
  @template.render(partial: "uploader/#{@theme}/container", locals: locals)
end

#sortable?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/uploader/helpers/field_tag.rb', line 62

def sortable?
  @options[:sortable] == true
end

#valueObject



50
51
52
# File 'lib/uploader/helpers/field_tag.rb', line 50

def value
  @value ||= @object.fileupload_asset(method_name)
end

#valuesObject



54
55
56
# File 'lib/uploader/helpers/field_tag.rb', line 54

def values
  Array.wrap(value)
end