-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdragonfly_input.rb
128 lines (109 loc) · 3.65 KB
/
dragonfly_input.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module Formtastic
module Inputs
class DragonflyInput < Formtastic::Inputs::FileInput
def to_html
components = input_html_options[:components] || [ :preview, :upload, :remove ]
input_wrapping do
fragments_wrapping do
fragments_label <<
template.content_tag(:ol) do
components.map do |component|
template.content_tag(:li, class: "input component-#{component}") do
fragment_html(component)
end
end.join.html_safe
end
end
end
end
def fragments_wrapping(&block)
template.content_tag(:fieldset,
template.capture(&block).html_safe,
fragments_wrapping_html_options
)
end
def fragments_wrapping_html_options
{ :class => "fragments" }
end
def fragments_label
if render_label?
template.content_tag(:legend, builder.label(method, label_text, :class => "label"))
else
"".html_safe
end
end
def fragment_label_html(fragment)
text = fragment_label(fragment)
text.blank? ? "".html_safe : template.content_tag(:label, text, :for => fragment_id(fragment))
end
def fragment_id(fragment)
"#{input_html_options[:id]}_#{fragment}"
end
def fragment_label(fragment)
labels_from_options = options[:labels] || {}
if labels_from_options.key?(fragment)
labels_from_options[fragment]
else
t(fragment.to_s, :default => fragment.to_s.humanize)
end
end
def fragment_html(fragment)
send("fragment_#{fragment}_html")
end
def fragment_upload_html
fragment_label_html(:upload) <<
builder.file_field(method, input_html_options) <<
builder.hidden_field("retained_#{method}")
end
def is_image?(file)
file.mime_type =~ /png|bmp|gif|tif|jpe?g/
rescue Dragonfly::Job::Fetch::NotFound
false
end
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'>#{t(:no_image)}</div>".html_safe
end
end
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'>#{t(:no_file)}</span>".html_safe
end
fragment_label_html(:download) << download
end
def fragment_url_html
fragment_label_html(:url) <<
builder.text_field("#{method}_url")
end
def fragment_remove_html
if object.send("#{method}_uid")
template.content_tag(:label, for: fragment_id(:remove)) do
builder.check_box("remove_#{method}") <<
" ".html_safe <<
t(:remove)
end
end
end
private
def t(key, options = {})
::I18n.t key, options.reverse_merge(:scope => [:formtastic, :dragonfly])
end
end
end
end