uploaded_file.rb
3.06 KB
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
# Article type that handles uploaded files.
#
# Limitation: only file metadata are versioned. Only the latest version
# of the file itself is kept. (FIXME?)
class UploadedFile < Article
settings_items :title, :type => 'string'
validates_size_of :title, :maximum => 60, :if => (lambda { |file| !file.title.blank? })
def display_title
title.blank? ? name : title
end
def self.max_size
UploadedFile.attachment_options[:max_size]
end
# FIXME need to define min/max file size
#
# default max_size is 1.megabyte to redefine it set options:
# :min_size => 2.megabytes
# :max_size => 5.megabytes
has_attachment :storage => :file_system,
:thumbnails => { :icon => [24,24], :thumb => '130x130>', :slideshow => '320x240>', :display => '640X480>' },
:thumbnail_class => Thumbnail,
:max_size => 5.megabytes # remember to update validate message below
validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of 5.0 MB")
def icon_name
self.image? ? public_filename(:icon) : self.content_type.gsub('/', '-')
end
def mime_type
content_type
end
def self.short_description
_("Uploaded file")
end
def self.description
_('Upload any kind of file you want.')
end
alias :orig_set_filename :filename=
def filename=(value)
orig_set_filename(value)
self.name = self.filename
end
def data
File.read(self.full_filename)
end
def to_html(options = {})
article = self
if image?
lambda do
if article.display_as_gallery?
images = article.parent.images
current_index = images.index(article)
total_of_images = images.count
link_to_previous = if current_index >= 1
link_to(_('« Previous'), images[current_index - 1].view_url, :class => 'left')
else
content_tag('span', _('« Previous'), :class => 'left')
end
link_to_next = if current_index < total_of_images - 1
link_to(_('Next »'), images[current_index + 1].view_url, :class => 'right')
else
content_tag('span', _('Next »'), :class => 'right')
end
content_tag(
'div',
link_to_previous + content_tag('span', _('image %d of %d'), :class => 'total-of-images') % [current_index + 1, total_of_images] + link_to_next,
:class => 'gallery-navigation'
)
end.to_s +
image_tag(article.public_filename(:display), :class => article.css_class_name, :style => 'max-width: 100%') +
content_tag('p', article.abstract, :class => 'uploaded-file-description')
end
else
lambda do
content_tag('ul', content_tag('li', link_to(article.name, article.url, :class => article.css_class_name))) +
content_tag('p', article.abstract, :class => 'uploaded-file-description')
end
end
end
def allow_children?
false
end
def can_display_hits?
false
end
def display_as_gallery?
self.parent && self.parent.folder? && self.parent.display_as_gallery?
end
end