2011-03-17 135 views
0

我有一个相当复杂的方法在我的控制器,基本上输出数据用于视图中创建一个甜甜圈图。从另一个控制器方法重新使用控制器方法(导轨)

def courses_allocated 
    course_id = params[:course_id]; 
    client_id = params[:client_id]; 

    override_client_id = get_client_id_for_current_user 

    unless override_client_id.nil? 
    client_id = override_client_id 
    end 

    category_course_enrollments = CourseEnrollment.select("course_categories.title, COUNT(*) as count"). 
    joins("INNER JOIN courses ON course_enrollments.course_id = courses.id"). 
    joins("INNER JOIN course_categories ON courses.course_category_id = course_categories.id"). 
    group("course_categories.id"). 
    order("course_categories.title") 

    course_enrollments = CourseEnrollment.select("COUNT(*) as count, course_enrollments.course_id, courses.title"). 
    joins("INNER JOIN courses ON course_enrollments.course_id = courses.id"). 
    joins("INNER JOIN course_categories ON courses.course_category_id = course_categories.id"). 
    group("course_enrollments.course_id"). 
    order("course_categories.title")     

    unless course_id.blank? 
    category_course_enrollments = category_course_enrollments.where("course_enrollments.course_id = ?" , course_id.to_i) 
    course_enrollments = course_enrollments.where("course_enrollments.course_id = ?" , course_id.to_i) 
    end 

    unless client_id.blank? 
    category_course_enrollments = category_course_enrollments.where("courses.client_id = ?", client_id) 
    course_enrollments = course_enrollments.where("courses.client_id = ?", client_id) 
    end 

    @category_data = [] 
    @course_assigned_data = [] 
    @course_assigned_detail_data = [] 

    category_course_enrollments.each do |category_course_enrollment| 
    @category_data.push([category_course_enrollment.title, category_course_enrollment.count]); 
    end 

    course_enrollments.each do |course_enrollment| 
    not_started = CourseEnrollment.select("COUNT(patient_id) AS total_not_started"). 
     where('started IS NULL'). 
     where('course_id = ?', course_enrollment.course_id).first.total_not_started 

    in_progress = CourseEnrollment.select("COUNT(patient_id) AS total_in_progress"). 
     where('started IS NOT NULL'). 
     where('completed IS NULL'). 
     where('course_id = ?', course_enrollment.course_id).first.total_in_progress 

    completed = CourseEnrollment.select("COUNT(patient_id) AS total_completed"). 
     where('completed IS NOT NULL'). 
     where('course_id = ?', course_enrollment.course_id).first.total_completed 

    @course_assigned_data.push([course_enrollment.title, course_enrollment.count]); 
    @course_assigned_detail_data.push({'name'=>course_enrollment.title + " Not Started", 'y'=> not_started, 'color'=>'#ff8800'}); 
    @course_assigned_detail_data.push({'name'=>course_enrollment.title + " In Progress", 'y'=> in_progress, 'color'=>'#0088ff'}); 
    @course_assigned_detail_data.push({'name'=>course_enrollment.title + " Completed", 'y'=> completed ,'color'=>'#44cc44'}); 
    end 
end 

观为圆环曲线图(除了输入用于形式为:)

<div id="reportcoursesallocatedgraph"> 

</div> 

<script type="text/javascript"> 
new IS.ReportCoursesAllocated('Course Allocated', <%= raw(ActiveSupport::JSON.encode(@category_data)); %>, <%= raw(ActiveSupport::JSON.encode(@course_assigned_data)); %>, <%= raw(ActiveSupport::JSON.encode(@course_assigned_detail_data)); %>, 'reportcoursesallocatedgraph'); 
</script> 

我想重用从在同一类中的方法courses_allocated逻辑; def仪表板。 (仪表板方法基本上创建了一堆不同的图表)

我应该做一个私有方法,他们都可以共享?

+2

有建设性的,不是关键的:花费一些时间让控制器的方法变得更加简洁。那里有太多的逻辑。 – ybakos 2012-08-01 18:02:39

回答

0

如果逻辑是相同的,那么您可以将仪表板别名为courses_allocated。要做到这一点,你可以把它放在courses_allocated操作方法的下面。

alias dashboard courses_allocated 
+0

仪表板中的逻辑包含courses_allocated +一些其他代码。私人方法似乎工作。 (可以从两种方法中调用它,但它看起来并不是最好的方法,但它起作用) – 2011-03-17 14:40:24

+1

我还会将这些查询移入CourseEnrollment模型以将它们从控制器中移出。一般来说,控制器的动作最多不应超过5行左右。如果你不止这些,这是一个迹象表明你需要做一些重构。你还应该检查一下decent_exposure,因为它可以让你将控件和视图公开给类似于“let”在rspec中工作的东西。将这些代码放入公开块中可能不需要私有方法。 – 2011-03-17 15:03:06

相关问题