2009-08-12 5 views
0

이 컨트롤러를 가장 잘 리팩터링하는 방법에 대한 조언이 필요합니다. 컨트롤러는 영역 및 모듈 페이지를 작성합니다. 페이지 has_many 영역, 영역 has_many 모듈. 따라서 영역은 컨테이너에 래핑 된 모듈의 클러스터입니다.이 Rails 컨트롤러를 리팩토링하는 가장 좋은 방법은 무엇입니까?

내가 겪고있는 문제는 일부 모듈에는 모든 페이지에서 실행하고 싶지 않은 특정 쿼리가있을 수 있으므로 조건을 추가해야한다는 것입니다. 조건은 쿼리가 실행되는 경우 모듈이 페이지에 있는지 테스트합니다. 하나의 문제는 내가 100 개의 특수 모듈 쿼리를 추가하면 컨트롤러가 각 모듈을 반복해야한다는 것입니다.

나는이 모듈 상태가 모든 추가 사용자 지정 작업뿐만 아니라 컨트롤러 밖으로 옮겨 졌음을보고 싶습니다. 이 컨트롤러에 모든 것을 보관할 수 있지만이 컨트롤러를 사용하는 많은 앱을 가지고 있으므로 지저분해질 수 있습니다.

class PagesController < ApplicationController 
    # GET /pages/1 
    # GET /pages/1.xml 
    # Show is the main page rendering action, page routes are aliased in routes.rb 

    def show 
    #-+-+-+-+-Core Page Queries-+-+-+-+- 
    @page = Page.find(params[:id]) 
    @zones = @page.zones.find(:all, :order => 'zones.list_order ASC') 
    @mods = @page.mods.find(:all) 
    @columns = Page.columns 

    # restful params to influence page rendering, see routes.rb 
    @fragment = params[:fragment] # render single module 
    @cluster = params[:cluster]  # render single zone 
    @head = params[:head]   # render html, body and head 

    #-+-+-+-+-Page Level Json Conversions-+-+-+-+- 
    @metas = @page.metas ? ActiveSupport::JSON.decode(@page.metas) : nil 
    @javascripts = @page.javascripts ? ActiveSupport::JSON.decode(@page.javascripts) : nil 

    #-+-+-+-+-Module Specific Queries-+-+-+-+- 
    # would like to refactor this process 

    @mods.each do |mod| 
     # Reps Module Custom Queries 
     if mod.name == "reps" 
     @reps = User.find(:all, :joins => :roles, :conditions => { :roles => { :name => 'rep' } }) 
     end 
     # Listing-poc Module Custom Queries 
     if mod.name == "listing-poc" 
     limit = params[:limit].to_i < 1 ? 10 : params[:limit] 
     PropertyEntry.update_from_listing(mod.service_url) 
     @properties = PropertyEntry.all(:limit => limit, :order => "city desc") 
     end  
     # Talents-index Module Custom Queries 
     if mod.name == "talents-index" 
     @talent = params[:type] 
     @reps = User.find(:all, :joins => :talents, :conditions => { :talents => { :name => @talent } }) 
     end 
    end  

    respond_to do |format| 
     format.html # show.html.erb 
     format.xml { render :xml => @page.to_xml(:include => { :zones => { :include => :mods } }) } 
     format.json { render :json => @page.to_json } 
     format.css # show.css.erb, CSS dependency manager template 
    end 
    end 

    # for property listing ajax request 
    def update_properties 
    limit = params[:limit].to_i < 1 ? 10 : params[:limit] 
    offset = params[:offset] 
    @properties = PropertyEntry.all(:limit => limit, :offset => offset, :order => "city desc") 
    #render :nothing => true 
    end 
end 

수백 개의 모듈과 추가 컨트롤러 동작 점수가있는 사이트를 상상해보십시오. 나는 그 코드를 옮겨서 리팩터링하여 좀더 구성처럼 행동 할 수 있다면 훨씬 더 깔끔하다는 것에 대부분 동의한다고 생각한다.

답변

0

이 보석을 확인해야합니다 :

http://github.com/josevalim/inherited_resources/tree/master

매우 우아하고, 당신이 가진 모든 문제를 해결합니다.

+0

이 컨트롤러에서는 제대로 작동하지 않을 것이라고 생각하지만 관리자 컨트롤러에서는 사용하지 않을 것입니다. 팁 고마워! –

0

스 니펫 특정 쿼리를 도우미 메서드로 옮기고 컨트롤러에서 가져와 스 니펫 자체가 erb를 통해 쿼리를 실행하고 DRY를 유지하고 도우미를 통해 읽을 수있게합니다. 따라서 모듈에서 @refs를 참조하는 대신 find_all_refs를 참조하거나 모듈에서 somesuch를 실행하여 응답을 실행하고 메모 해줄 수 있습니다.

+0

이것은 훌륭한 아이디어입니다. 왜 내가 도우미로 옮겨야하는지 생각하지 못했습니다. 코드를 리팩토링하고 멋지게 작동합니다. memoization 또는 || =이 경우에 작동하는지, 몇 가지 테스트를 수행해야하는지에 대한 확실하지 않은 쿼리 캐싱에 대해 지금 작업해야합니다. 페이지가 약 110ms 렌더링되므로 너무 걱정하지 않아도됩니다. 감사! –

관련 문제