Add render_tree helper; simplify (speed up) tree_icon
This commit is contained in:
parent
a8fad4ff9c
commit
388d72e6bf
6 changed files with 44 additions and 41 deletions
|
@ -1,31 +1,42 @@
|
|||
module TreeHelper
|
||||
def tree_icon(content)
|
||||
if content.is_a?(Grit::Blob)
|
||||
if content.text?
|
||||
image_tag "file_txt.png"
|
||||
elsif content.image?
|
||||
image_tag "file_img.png"
|
||||
# Sorts a repository's tree so that folders are before files and renders
|
||||
# their corresponding partials
|
||||
#
|
||||
# contents - A Grit::Tree object for the current tree
|
||||
def render_tree(contents)
|
||||
# Render Folders before Files/Submodules
|
||||
folders, files = contents.partition { |v| v.kind_of?(Grit::Tree) }
|
||||
|
||||
tree = ""
|
||||
|
||||
# Render folders if we have any
|
||||
tree += render partial: 'tree/tree_item', collection: folders, locals: {type: 'folder'} if folders.present?
|
||||
|
||||
files.each do |f|
|
||||
if f.respond_to?(:url)
|
||||
# Object is a Submodule
|
||||
tree += render partial: 'tree/submodule_item', object: f
|
||||
else
|
||||
image_tag "file_bin.png"
|
||||
# Object is a Blob
|
||||
tree += render partial: 'tree/tree_item', object: f, locals: {type: 'file'}
|
||||
end
|
||||
else
|
||||
image_tag "file_dir.png"
|
||||
end
|
||||
|
||||
tree.html_safe
|
||||
end
|
||||
|
||||
# Return an image icon depending on the file type
|
||||
#
|
||||
# type - String type of the tree item; either 'folder' or 'file'
|
||||
def tree_icon(type)
|
||||
image = type == 'folder' ? 'file_dir.png' : 'file_txt.png'
|
||||
image_tag(image, size: '16x16')
|
||||
end
|
||||
|
||||
def tree_hex_class(content)
|
||||
"file_#{hexdigest(content.name)}"
|
||||
end
|
||||
|
||||
def tree_full_path(content)
|
||||
content.name.force_encoding('utf-8')
|
||||
if params[:path]
|
||||
File.join(params[:path], content.name)
|
||||
else
|
||||
content.name
|
||||
end
|
||||
end
|
||||
|
||||
# Public: Determines if a given filename is compatible with GitHub::Markup.
|
||||
#
|
||||
# filename - Filename string to check
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue