Class: Uploader::Helpers::FieldTag

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

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


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

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

  @template, @options = template, options.dup
  
  @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.



4
5
6
# File 'lib/uploader/helpers/field_tag.rb', line 4

def object
  @object
end

#templateObject (readonly)

Returns the value of attribute template.



4
5
6
# File 'lib/uploader/helpers/field_tag.rb', line 4

def template
  @template
end

#themeObject (readonly)

Returns the value of attribute theme.



4
5
6
# File 'lib/uploader/helpers/field_tag.rb', line 4

def theme
  @theme
end

Instance Method Details

#attachments_path(options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/uploader/helpers/field_tag.rb', line 67

def attachments_path(options = {})
  options = {
    :guid => @object.fileupload_guid, 
    :assetable_type => @object.class.base_class.name.to_s,
    :klass => klass.to_s
  }.merge(options)
  
  options[:assetable_id] = @object.id if @object.persisted?
  
  uploader.attachments_path(options)
end

#exists?Boolean

Returns:

  • (Boolean)


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

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

#idObject



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

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

#input_htmlObject



79
80
81
82
83
84
85
# File 'lib/uploader/helpers/field_tag.rb', line 79

def input_html
  @input_html ||= {
    :"data-url" => attachments_path, 
    :multiple => multiple?,
    :class => "uploader"
  }.merge(@options[:input_html] || {})
end

#klassObject



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

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

#method_nameObject



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

def method_name
  @options[:method_name]
end

#multiple?Boolean

Returns:

  • (Boolean)


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

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

#object_nameObject



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

def object_name
  @options[:object_name]
end

#render(locals = {}) ⇒ Object

:nodoc:



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

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

#sortable?Boolean

Returns:

  • (Boolean)


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

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

#valueObject



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

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

#valuesObject



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

def values
  Array.wrap(value)
end