이 컨트롤러를 가장 잘 리팩터링하는 방법에 대한 조언이 필요합니다. 컨트롤러는 영역 및 모듈 페이지를 작성합니다. 페이지 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
수백 개의 모듈과 추가 컨트롤러 동작 점수가있는 사이트를 상상해보십시오. 나는 그 코드를 옮겨서 리팩터링하여 좀더 구성처럼 행동 할 수 있다면 훨씬 더 깔끔하다는 것에 대부분 동의한다고 생각한다.
이 컨트롤러에서는 제대로 작동하지 않을 것이라고 생각하지만 관리자 컨트롤러에서는 사용하지 않을 것입니다. 팁 고마워! –