在 Ruby on Rails 中在控制器之间重用代码的最佳实践 [英] Best Practices for reusing code between controllers in Ruby on Rails

查看:25
本文介绍了在 Ruby on Rails 中在控制器之间重用代码的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分享一些控制器方法.在 ruby​​ on rails 中执行此操作的最佳实践是什么?我应该创建一个我的控制器扩展的抽象类,还是应该创建模块并将其添加到每个控制器中?以下是我想分享的控制器方法:

I have some controller methods I'd like to share. What is the best practice for doing this in ruby on rails? Should I create an abstract class that my controllers extend, or should I create module and add it in to each controller? Below are the controller methods I want to share:

def driving_directions
  @address_to = params[:address_to]
  @address_from = params[:address_from]
  @map_center = params[:map_center_start]

  # if we were not given a center point to start our map on
  # let's create one.
  if !@map_center && @address_to
    @map_center = GeoKit::Geocoders::MultiGeocoder.geocode(@address_to).ll
  elsif !@map_center && @address_from
    @map_center = GeoKit::Geocoders::MultiGeocoder.geocode(@address_from).ll
  end
end

def printer_friendly
  starting_point = params[:starting_point].split(',').collect{|e|e.to_f}
  ne = params[:ne].split(',').collect{|e|e.to_f}
  sw = params[:sw].split(',').collect{|e|e.to_f}
  size = params[:size].split(',').collect{|e|e.to_f}
  address = params[:address]

  @markers = retrieve_points(ne,sw,size,false)
  @map = initialize_map([[sw[0],sw[1]],[ne[0],ne[1]]],[starting_point[0],starting_point[1]],false,@markers,true)
  @address_string = address
end

推荐答案

在我看来,正常的面向对象设计原则适用:

In my opinion, normal OO design principles apply:

  • 如果代码真的是一组不需要访问对象状态的实用程序,我会考虑将它放在一个模块中单独调用.例如,如果代码都是映射实用程序,则创建一个模块 Maps,并访问如下方法:Maps::driving_directions.
  • 如果代码需要状态并且在每个控制器中使用或可以使用,请将代码放在 ApplicationController 中.
  • 如果代码需要状态并且在所有密切相关且逻辑相关(即所有关于地图)的控制器的子集中使用,则创建一个基类(class MapController )并放置那里的共享代码.
  • 如果代码需要状态并且在所有控制器的子集中使用,但不是非常密切相关,请将其放在一个模块中并将其包含在必要的控制器中.
  • If the code is really a set of utilities that doesn't need access to object state, I would consider putting it in a module to be called separately. For instance, if the code is all mapping utilities, create a module Maps, and access the methods like: Maps::driving_directions.
  • If the code needs state and is used or could be used in every controller, put the code in ApplicationController.
  • If the code needs state and is used in a subset of all controllers that are closely and logically related (i.e. all about maps) then create a base class (class MapController < ApplicationController) and put the shared code there.
  • If the code needs state and is used in a subset of all controllers that are not very closely related, put it in a module and include it in necessary controllers.

在您的情况下,方法需要状态 (params),因此选择取决于需要它的控制器之间的逻辑关系.另外:

In your case, the methods need state (params), so the choice depends on the logical relationship between the controllers that need it. In addition:

还有:

  • 对于重复代码,尽可能使用部分代码,并将其放置在公共部分"目录中或通过特定路径包含.
  • 尽可能坚持使用 RESTful 方法(对于方法),如果您发现自己创建了许多非 RESTful 方法,请考虑将它们提取到自己的控制器中.

这篇关于在 Ruby on Rails 中在控制器之间重用代码的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆