我在幫助程序中有以下Ruby on Rails代碼。當我在頁面上有很多鏈接時,我的視圖加載緩慢。需要幫助重構和加速if語句的視圖
任何人都可以告訴我一個重構的版本,這將是乾燥和加速?
https://gist.github.com/anonymous/2afe1956e7565731ff20
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
你有沒有使用任何分析工具t檢查它實際上是這些助手而不是別的?分析工具也可能幫助您找出問題的部分原因。 – FluffyJack 2013-04-21 13:16:09
您是否介意將您的代碼複製並粘貼到最初的問題中?我不介意點擊並查看代碼,但Stack Overflow約定規定代碼在問題中是正確的(此外,它可能會鼓勵更多的ppl查看和回答!)。 – aceofbassgreg 2013-04-21 13:21:54