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.to_s


14
15
16
17
18
19
20
21
22
23
# 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



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

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



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

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

#idObject



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

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

#input_htmlObject



90
91
92
93
94
95
96
# File 'lib/uploader/helpers/field_tag.rb', line 90

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

#klassObject



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

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

#method_nameObject



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

def method_name
  @options[:method_name]
end

#multiple?Boolean



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

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

#object_nameObject



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

def object_name
  @options[:object_name]
end

#sort_path(options = {}) ⇒ Object



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

def sort_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.sort_attachments_path(options)
end

#to_s(locals = {}) ⇒ Object

:nodoc:



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

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

#valueObject



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

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

#valuesObject



50
51
52
53
54
55
56
# File 'lib/uploader/helpers/field_tag.rb', line 50

def values
  if !value.nil? && value.respond_to?(:first) && value.first.respond_to?(:sort)
    Array.wrap(value).sort_by(&:sort)
  else
    Array.wrap(value)
  end
end