Class: Formtastic::Inputs::DragonflyInput

Inherits:
FileInput
  • Object
show all
Defined in:
lib/formtastic/inputs/dragonfly_input.rb

Instance Method Summary collapse

Instance Method Details

#fragment_download_htmlObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 89

def fragment_download_html
  file = object.send(method)
  download = if file.present?
    original_url = file.url
    name = object.send("#{method}_name") rescue "Download"
    name = name.blank? ? "Download" : name
    template.link_to name, original_url
  else
    "<span class='no-file'>#{I18n.t("dragonfly.no_file")}</span>".html_safe
  end
  fragment_label_html(:download) << download
end

#fragment_html(fragment) ⇒ Object



59
60
61
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 59

def fragment_html(fragment)
  send("fragment_#{fragment}_html")
end

#fragment_id(fragment) ⇒ Object



46
47
48
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 46

def fragment_id(fragment)
  "#{input_html_options[:id]}_#{fragment}"
end

#fragment_label(fragment) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 50

def fragment_label(fragment)
  labels_from_options = options[:labels] || {}
  if labels_from_options.key?(fragment)
    labels_from_options[fragment]
  else
    ::I18n.t(fragment.to_s, :default => fragment.to_s.humanize, :scope => [:dragonfly])
  end
end

#fragment_label_html(fragment) ⇒ Object



41
42
43
44
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 41

def fragment_label_html(fragment)
  text = fragment_label(fragment)
  text.blank? ? "".html_safe : template.(:label, text, :for => fragment_id(fragment))
end

#fragment_preview_htmlObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 73

def fragment_preview_html
  file = object.send(method)
  if file.present?
    if is_image?(file)
      original_url = object.send(method).url
      preview_size = input_html_options[:preview_size] || [ 75, 75 ]
      preview_url = object.send(method).thumb("#{preview_size.first}x#{preview_size.last}#").url
      fragment_label_html(:preview) << template.link_to(template.image_tag(preview_url), original_url)
    else
      fragment_download_html
    end
  else
    fragment_label_html(:preview) << "<div class='no-image'>#{I18n.t("dragonfly.no_image")}</div>".html_safe
  end
end

#fragment_remove_htmlObject



107
108
109
110
111
112
113
114
115
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 107

def fragment_remove_html
  if object.send("#{method}_uid")
    template.(:label, for: fragment_id(:remove)) do
      builder.check_box("remove_#{method}") <<
      " ".html_safe <<
      I18n.t("dragonfly.remove")
    end
  end
end

#fragment_upload_htmlObject



63
64
65
66
67
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 63

def fragment_upload_html
  fragment_label_html(:upload) <<
  builder.file_field(method, input_html_options) <<
  builder.hidden_field("retained_#{method}")
end

#fragment_url_htmlObject



102
103
104
105
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 102

def fragment_url_html
  fragment_label_html(:url) <<
  builder.text_field("#{method}_url")
end

#fragments_labelObject



33
34
35
36
37
38
39
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 33

def fragments_label
  if render_label?
    template.(:legend, builder.label(method, label_text, :class => "label"))
  else
    "".html_safe
  end
end

#fragments_wrapping(&block) ⇒ Object



22
23
24
25
26
27
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 22

def fragments_wrapping(&block)
  template.(:fieldset,
    template.capture(&block).html_safe,
    fragments_wrapping_html_options
  )
end

#fragments_wrapping_html_optionsObject



29
30
31
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 29

def fragments_wrapping_html_options
  { :class => "fragments" }
end

#is_image?(file) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 69

def is_image?(file)
  file.mime_type =~ /png|bmp|gif|tif|jpe?g/
end

#to_htmlObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/formtastic/inputs/dragonfly_input.rb', line 6

def to_html
  components = input_html_options[:components] || [ :preview, :upload, :remove ]
  input_wrapping do
    fragments_wrapping do
      fragments_label <<
      template.(:ol) do
        components.map do |component|
          template.(:li, class: "input component-#{component}") do
            fragment_html(component)
          end
        end.join.html_safe
      end
    end
  end
end