如何重构以下控制器获取操作,以及如何在获取操作中使用本地
变量(website not @website
,本地因为它没有视图)和start_fetching方法
def fetch
@website = Website.find(params[:website_id])
retun redirect_to :back, :alert => t('crawl_counter_error') if @website.fetch_counter < 1
authorize! :update, @website, :message => t("website_authorize_error")
@website.confirmed? || @website.confirmed_key? ? start_fetching : flash[:error] = t('key_error')
redirect_to :back
end
private
def start_fetching
if @website.working_status
flash[:error] = t('crawling_in_progress_error')
else
@website.start_fetch
flash[:notice] = t('crawler_success_notice')
end
end
答案 0 :(得分:2)
这可能有点清洁
# controller
def fetch
website.find(params[:website_id])
authorize! :update, website, :message => t("website_authorize_error")
status = website.fetch
flash[status[:type]] = t(status[:message])
redirect_to :back
end
# move to model/website.rb
def fetch
status = nil
if (fetch_counter < 1)
status = {:error, 'crawl_counter_error'}
elsif confirmed? || confirmed_key?
status = {:error, 'key_error'}
elsif working_status
status = {:error, 'crawling_in_progress_error'}
else
start_fetch
status = {:notice, 'crawler_success_notice'}
end
status
end