2013-04-21 57 views
1

我在幫助程序中有以下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 
+0

你有沒有使用任何分析工具t檢查它實際上是這些助手而不是別的?分析工具也可能幫助您找出問題的部分原因。 – FluffyJack 2013-04-21 13:16:09

+0

您是否介意將您的代碼複製並粘貼到最初的問題中?我不介意點擊並查看代碼,但Stack Overflow約定規定代碼在問題中是正確的(此外,它可能會鼓勵更多的ppl查看和回答!)。 – aceofbassgreg 2013-04-21 13:21:54

回答

2

,而不是這種雙重檢查

document.admin_url && document.admin_url.length > 0 

可以使用

document.url.to_s.blank? 

它將檢查零和空字符串

+0

更改爲.blank?大大提高了性能。但是,頁面加載時間仍然是1.5秒,並且大部分來自if語句,因爲它正在遍歷列表並且重複遍歷每個文檔的函數。所以我正在努力進一步改進它。還有什麼建議? – user2012677 2013-04-21 14:50:41

+0

這取決於這個列表是多久,但你可以給他們一些提示,看看他們是否可以幫助 array.compact! **將刪除陣列中的所有nils ** array.flatten! **在數組上創建陣列** array.uniq! **刪除所有重複的項目** – 2013-04-22 04:26:20

+0

也可以使用2種方法** delete_if **,如果您過濾大部分數組用戶** keep_if ** – 2013-04-22 04:28:52