I think you could put most of this logic into your model, ProjectFileor something else, corresponding to the model name:
ProjectFile < ActiveRecord::Base
def node_list(project_id, folder_id, user_id)
node_list = []
if project_id == nil and folder_id == nil
projects = Project.find_all_by_user_id(user_id)
projects.each do |project|
node_list << {
:text => project.name,
:leaf => false,
:id => project.id.to_s + '|0',
:cls => 'project',
:draggable => false
}
end
else
if project_id != nil and folder_id == nil
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']
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
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
node_list . , , ( ), .
:
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
nodes = node_list(@project_id, @folder_id, @user_id)
render :json => nodes
end
end
-. " , ".