Class: Uploader::Helpers::FieldTag

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

Constant Summary collapse

RESERVED_OPTIONS_KEYS =
%(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


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

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.



6
7
8
# File 'lib/uploader/helpers/field_tag.rb', line 6

def object
  @object
end

#templateObject (readonly)

Returns the value of attribute template.



6
7
8
# File 'lib/uploader/helpers/field_tag.rb', line 6

def template
  @template
end

#themeObject (readonly)

Returns the value of attribute theme.



6
7
8
# File 'lib/uploader/helpers/field_tag.rb', line 6

def theme
  @theme
end

Instance Method Details

#attachments_path(options = {}) ⇒ Object



68
69
70
71
# File 'lib/uploader/helpers/field_tag.rb', line 68

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

#exists?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/uploader/helpers/field_tag.rb', line 56

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

#idObject



32
33
34
# File 'lib/uploader/helpers/field_tag.rb', line 32

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

#input_htmlObject



73
74
75
76
77
78
# File 'lib/uploader/helpers/field_tag.rb', line 73

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

#input_html_optionsObject



80
81
82
# File 'lib/uploader/helpers/field_tag.rb', line 80

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

#klassObject



64
65
66
# File 'lib/uploader/helpers/field_tag.rb', line 64

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

#method_nameObject



36
37
38
# File 'lib/uploader/helpers/field_tag.rb', line 36

def method_name
  @options[:method_name]
end

#multiple?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/uploader/helpers/field_tag.rb', line 44

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

#object_nameObject



40
41
42
# File 'lib/uploader/helpers/field_tag.rb', line 40

def object_name
  @options[:object_name]
end

#render(locals = {}) ⇒ Object

:nodoc:



27
28
29
30
# File 'lib/uploader/helpers/field_tag.rb', line 27

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

#sortable?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/uploader/helpers/field_tag.rb', line 60

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

#valueObject



48
49
50
# File 'lib/uploader/helpers/field_tag.rb', line 48

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

#valuesObject



52
53
54
# File 'lib/uploader/helpers/field_tag.rb', line 52

def values
  Array.wrap(value)
end