从另一个控制器方法重用控制器方法(rails)

时间:2011-03-17 14:22:18

标签: ruby-on-rails

我的控制器中有一个相当复杂的方法,基本上输出要在视图中使用的数据来创建圆环图。

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仪表板。 (仪表板方法基本上会创建一堆不同的图形)

我应该制作一个他们可以分享的私人方法吗?

1 个答案:

答案 0 :(得分:0)

如果逻辑相同,那么您可以将仪表板别名为courses_allocated。为此,您可以将其放在courses_allocated操作方法下面。

alias dashboard courses_allocated