Skip to content

Instantly share code, notes, and snippets.

Created April 21, 2013 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2afe1956e7565731ff20 to your computer and use it in GitHub Desktop.
Save anonymous/2afe1956e7565731ff20 to your computer and use it in GitHub Desktop.
I have the following code in my helper. My views are really slow loading when I have many links on the page. Can anyone show me a refactored version that would be DRY and speed it up?
module ApplicationHelper
def link_to_doc(document)
if document.document_type.downcase == "image"
link_to "#{document.name}", "#{document.url}", class: "sublime"
elsif document.document_type.downcase == "video"
sublime_video_link_to(document)
elsif document.url && document.url.length > 0
link_to "#{document.name}", "#{document.url}", download: "#{document.name.parameterize}"
elsif document.admin_url && document.admin_url.length > 0
content_tag :a, href: document.admin_url do
link_to "#{document.name}", document.admin_url
end
else
link_to "#{document.name}", document_url(document)
end
end
def btn_link_to_doc(document)
if document.document_type.downcase == "image"
link_to "View #{ document.document_type.downcase }", "#{document.url}", class: "document-link btn sublime"
elsif document.document_type.downcase == "video"
sublime_video_btn_link_to(document)
elsif document.url && document.url.length > 0
link_to "Download #{ document.document_type.downcase }", "#{document.url}", download: "#{document.name.parameterize}", class: "document-link btn"
elsif document.admin_url && document.admin_url.length > 0
content_tag :a, href: document.admin_url do
link_to "View", document.admin_url, class: "document-link btn"
end
else
link_to "View", document_url(document), class: "document-link btn"
end
end
private
def sublime_video_btn_link_to(document)
tag=[]
tag<<link_to("View #{ document.document_type.downcase}", "#video#{document.id}", class: "document-link btn sublime", data: { settings: 'close-button-visibility:visible' })
tag<< content_tag(:video,{ id: "video#{document.id}", style: "display:none;", width:'480', height:'270', preload: true }) do
content_tag(:source, nil,{src: document.url})
end
tag.join.html_safe
end
def sublime_video_link_to(document)
tag=[]
tag<<link_to("#{document.name}", "#video#{document.id}", class: "sublime", data: { settings: 'close-button-visibility:visible' })
tag<< content_tag(:video,{ id: "video#{document.id}", style: "display:none;", width:'480', height:'270', preload: true }) do
content_tag(:source, nil,{src: document.url})
end
tag.join.html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment