У меня есть следующий код Ruby on Rails в моем помощнике. Мои взгляды медленны, когда у меня много ссылок на странице.Нужна помощь в рефакторинге и ускорении, если инструкция для представления
Может ли кто-нибудь показать мне отредактированную версию, которая была бы СУХОЙ и ускорить ее?
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 o проверить, что это на самом деле эти помощники, а не что-то еще? Инструмент профилирования также может помочь вам выяснить, какая часть этих проблем вызывает проблему. – FluffyJack
Не возражаете ли вы копировать и вставлять свой код в свой первоначальный вопрос? Я не против щелчка и просмотра кода, но соглашения о переполнении стека диктуют наличие правильного кода в вопросе (кроме того, это, вероятно, будет способствовать увеличению количества запросов и ответов!). – aceofbassgreg