开发者

reusing a controller method from another controller method (rails)

I have a fairly complex method in my controller that basically outputs data to be used in a view to create a donut graph.

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").
    orde开发者_如何学Gor("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

The View for the donut graph (besides the input for a form is:)

<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>

I want to reuse the logic from courses_allocated from a method in the same class; def dashboard. (The dashboard method basically creates a bunch of different graphs)

Should I make a private method that they can both share?


If the logic is identical, then you can just alias dashboard to courses_allocated. To do that, you can put this right below the courses_allocated action method.

alias dashboard courses_allocated
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜