【问题标题】:reusing a controller method from another controller method (rails)从另一个控制器方法(rails)重用控制器方法
【发布时间】:2011-03-17 14:22:18
【问题描述】:

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

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 的逻辑;定义仪表板。 (dashboard方法基本上是创建一堆不同的图表)

我应该创建一个他们都可以共享的私有方法吗?

【问题讨论】:

  • 建设性的,不是关键的:花一些时间使该控制器方法更加简洁。里面的逻辑太多了。

标签: ruby-on-rails


【解决方案1】:

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

alias dashboard courses_allocated

【讨论】:

  • 仪表板中的逻辑包含 courses_allocated + 一些其他代码。私有方法似乎有效。 (可以从这两种方法中调用,但它似乎不是最好的方法,但它有效)
  • 我还将这些查询移到 CourseEnrollment 模型中,以将它们从控制器中取出。一般来说,一个控制器动作最多不应该超过 5 行左右。如果你有更多,这表明你需要进行一些重构。您还应该查看decent_exposure,因为它可以让您将事物暴露给控制器和视图,类似于“let”在rspec 中的工作方式。将其中一些代码放入暴露块中可能会消除对私有方法的需要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多