私は次のコントローラとメソッドを持っているとしましょう。私はこのコントローラのメソッド(リスト)が重すぎるように感じる。私はこれをどのように分割すべきですか?ビューレイヤーに移動する対象と、モデルレイヤーに移動する対象は何ですか?このコントローラーメソッドの重さを軽減する方法を教えてください。
注:返される形式(text、:leaf、id、:clsを含むハッシュ)には、ProjectFileデータベーステーブルのフィールドとは異なるフィールドが含まれているため、実際にこのコントローラメソッドをどれだけ移動させるべきかモデルレイヤーに追加します。そして、より管理しやすい方法にnode_list
を破る
ProjectFile < ActiveRecord::Base
def node_list(project_id, folder_id, user_id)
node_list = []
# If no project id was specified, return a list of all projects.
if project_id == nil and folder_id == nil
# Get a list of projects for the current user.
projects = Project.find_all_by_user_id(user_id)
# Add each project to the node list.
projects.each do |project|
node_list << {
:text => project.name,
:leaf => false,
:id => project.id.to_s + '|0',
:cls => 'project',
:draggable => false
}
end
else
# If a project id was specfied, but no file id was also specified, return a
# list of all top-level folders for the given project.
if project_id != nil and folder_id == nil
# Look for top-level folders for the project.
folder_id = 0
end
directory_list = []
file_list = []
known_file_extensions = ['rb', 'erb', 'rhtml', 'php', 'py', 'css', 'html', 'txt', 'js', 'bmp', 'gif', 'h', 'jpg', 'mov', 'mp3', 'pdf', 'png', 'psd', 'svg', 'wav', 'xsl']
# Get a list of files by project and parent directory.
project_files = ProjectFile.find_all_by_project_id(project_id,
:conditions => "ancestry like '%#{folder_id}'",
:order => 'name')
project_files.each do |project_file|
file_extension = File.extname(project_file.name).gsub('.', '')
if known_file_extensions.include? file_extension
css_class_name = file_extension
else
css_class_name = 'unknown'
end
# Determine whether this is a file or directory.
if project_file.is_directory
directory_list << {
:text => project_file.name,
:leaf => false,
:id => project_id + '|' + project_file.id.to_s,
:cls => css_class_name
}
else
file_list << {
:text => project_file.name,
:leaf => true,
:id => project_id + '|' + project_file.id.to_s,
:cls => css_class_name
}
end
end
node_list = directory_list | file_list
end
end
:
class ProjectFileController < ApplicationController
before_filter :require_user
def list
@project_id = params[:project_id]
@folder_id = params[:folder_id]
current_user = UserSession.find
@user_id = current_user && current_user.record.id
node_list = []
# If no project id was specified, return a list of all projects.
if @project_id == nil and @folder_id == nil
# Get a list of projects for the current user.
projects = Project.find_all_by_user_id(@user_id)
# Add each project to the node list.
projects.each do |project|
node_list << {
:text => project.name,
:leaf => false,
:id => project.id.to_s + '|0',
:cls => 'project',
:draggable => false
}
end
else
# If a project id was specfied, but no file id was also specified, return a
# list of all top-level folders for the given project.
if @project_id != nil and @folder_id == nil
# Look for top-level folders for the project.
@folder_id = 0
end
directory_list = []
file_list = []
known_file_extensions = ['rb', 'erb', 'rhtml', 'php', 'py', 'css', 'html', 'txt', 'js', 'bmp', 'gif', 'h', 'jpg', 'mov', 'mp3', 'pdf', 'png', 'psd', 'svg', 'wav', 'xsl']
# Get a list of files by project and parent directory.
project_files = ProjectFile.find_all_by_project_id(@project_id,
:conditions => "ancestry like '%#{@folder_id}'",
:order => 'name')
project_files.each do |project_file|
file_extension = File.extname(project_file.name).gsub('.', '')
if known_file_extensions.include? file_extension
css_class_name = file_extension
else
css_class_name = 'unknown'
end
# Determine whether this is a file or directory.
if project_file.is_directory
directory_list << {
:text => project_file.name,
:leaf => false,
:id => @project_id + '|' + project_file.id.to_s,
:cls => css_class_name
}
else
file_list << {
:text => project_file.name,
:leaf => true,
:id => @project_id + '|' + project_file.id.to_s,
:cls => css_class_name
}
end
end
node_list = directory_list | file_list
end
render :json => node_list
end
end
ビジネスとプレゼンテーションのロジックをコントローラに置くことをやめることができます。それはたくさんの助けになります。 –
@teresko質問は* how *です。 –
@jeriley質問を言い換えることができますか? –