##// END OF EJS Templates
Don't reload attachments if they are already loaded....
Jean-Philippe Lang -
r15731:d521e49dbd3c
parent child
Show More
@@ -1,76 +1,81
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2016 Jean-Philippe Lang
4 # Copyright (C) 2006-2016 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 module AttachmentsHelper
20 module AttachmentsHelper
21
21
22 def container_attachments_edit_path(container)
22 def container_attachments_edit_path(container)
23 object_attachments_edit_path container.class.name.underscore.pluralize, container.id
23 object_attachments_edit_path container.class.name.underscore.pluralize, container.id
24 end
24 end
25
25
26 def container_attachments_path(container)
26 def container_attachments_path(container)
27 object_attachments_path container.class.name.underscore.pluralize, container.id
27 object_attachments_path container.class.name.underscore.pluralize, container.id
28 end
28 end
29
29
30 # Displays view/delete links to the attachments of the given object
30 # Displays view/delete links to the attachments of the given object
31 # Options:
31 # Options:
32 # :author -- author names are not displayed if set to false
32 # :author -- author names are not displayed if set to false
33 # :thumbails -- display thumbnails if enabled in settings
33 # :thumbails -- display thumbnails if enabled in settings
34 def link_to_attachments(container, options = {})
34 def link_to_attachments(container, options = {})
35 options.assert_valid_keys(:author, :thumbnails)
35 options.assert_valid_keys(:author, :thumbnails)
36
36
37 attachments = container.attachments.preload(:author).to_a
37 attachments = if container.attachments.loaded?
38 container.attachments
39 else
40 container.attachments.preload(:author).to_a
41 end
42
38 if attachments.any?
43 if attachments.any?
39 options = {
44 options = {
40 :editable => container.attachments_editable?,
45 :editable => container.attachments_editable?,
41 :deletable => container.attachments_deletable?,
46 :deletable => container.attachments_deletable?,
42 :author => true
47 :author => true
43 }.merge(options)
48 }.merge(options)
44 render :partial => 'attachments/links',
49 render :partial => 'attachments/links',
45 :locals => {
50 :locals => {
46 :container => container,
51 :container => container,
47 :attachments => attachments,
52 :attachments => attachments,
48 :options => options,
53 :options => options,
49 :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)
54 :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)
50 }
55 }
51 end
56 end
52 end
57 end
53
58
54 def render_api_attachment(attachment, api, options={})
59 def render_api_attachment(attachment, api, options={})
55 api.attachment do
60 api.attachment do
56 render_api_attachment_attributes(attachment, api)
61 render_api_attachment_attributes(attachment, api)
57 options.each { |key, value| eval("api.#{key} value") }
62 options.each { |key, value| eval("api.#{key} value") }
58 end
63 end
59 end
64 end
60
65
61 def render_api_attachment_attributes(attachment, api)
66 def render_api_attachment_attributes(attachment, api)
62 api.id attachment.id
67 api.id attachment.id
63 api.filename attachment.filename
68 api.filename attachment.filename
64 api.filesize attachment.filesize
69 api.filesize attachment.filesize
65 api.content_type attachment.content_type
70 api.content_type attachment.content_type
66 api.description attachment.description
71 api.description attachment.description
67 api.content_url download_named_attachment_url(attachment, attachment.filename)
72 api.content_url download_named_attachment_url(attachment, attachment.filename)
68 if attachment.thumbnailable?
73 if attachment.thumbnailable?
69 api.thumbnail_url thumbnail_url(attachment)
74 api.thumbnail_url thumbnail_url(attachment)
70 end
75 end
71 if attachment.author
76 if attachment.author
72 api.author(:id => attachment.author.id, :name => attachment.author.name)
77 api.author(:id => attachment.author.id, :name => attachment.author.name)
73 end
78 end
74 api.created_on attachment.created_on
79 api.created_on attachment.created_on
75 end
80 end
76 end
81 end
General Comments 0
You need to be logged in to leave comments. Login now