这是重构的正确方法吗?我还能做点更好的事吗?

发布于 2024-11-16 02:42:18 字数 1428 浏览 1 评论 0原文

我制作了一个 Impressions 模型,它可以跟踪特定照片或事件的“观看次数”。它通过remote_url、remote_ip 和user_agent 进行跟踪。它按照我想要的方式工作。

照片控制器:

def show

  @photo.impressions.create(:request_url => request.url, :controller_name => controller_name, :action_name => action_name,
                            :ip_address => request.remote_ip, :user_agent => request.user_agent)
....

和我的事件控制器:

def show

  @event.impressions.create(:request_url => request.url, :controller_name => controller_name, :action_name => action_name,
                            :ip_address => request.remote_ip, :user_agent => request.user_agent)
....

但是,我正在考虑重构它(以使代码更清晰)。也许在 PhotoEvent 模型中创建一个名为 add_impression 的方法。它的作用与上面完全相同,但封装了如何创建印象的功能。它看起来像:

照片控制器:

def show

  @photo.add_impression(request.url, controller_name, action_name, request.remote_ip, request.user_agent)
....

和我的事件控制器:

def show

  @event.add_impression(request.url, controller_name, action_name, request.remote_ip, request.user_agent)
....

通过这种方法,我最终会在 PhotoEvent 模型中得到一个 add_impression 方法。不完全是 DRY(不要重复自己)。我正在考虑创建一个 Impression 模块来解决这个问题。

这是正确的方法吗?或者还有什么我应该做的吗?

PS 我还没有很好地调整模块。因此,如果有人愿意留下代码片段或值得阅读的文章链接。请这样做。

I've made an Impressions model, which keeps track of how many "views" a certain photo or event has. It tracks this by remote_url, remote_ip and user_agent. It works the way I want it.

Photos controller:

def show

  @photo.impressions.create(:request_url => request.url, :controller_name => controller_name, :action_name => action_name,
                            :ip_address => request.remote_ip, :user_agent => request.user_agent)
....

And my Events controller:

def show

  @event.impressions.create(:request_url => request.url, :controller_name => controller_name, :action_name => action_name,
                            :ip_address => request.remote_ip, :user_agent => request.user_agent)
....

However, I am thinking of refactoring this (to make the code cleaner). Perhaps creating a method in the Photo and Event models called add_impression. Which does exactly the same thing as above, but encapsulates the functionality of how impressions are created. It would look something like:

Photos controller:

def show

  @photo.add_impression(request.url, controller_name, action_name, request.remote_ip, request.user_agent)
....

And my Events controller:

def show

  @event.add_impression(request.url, controller_name, action_name, request.remote_ip, request.user_agent)
....

With this approach, I would end up with an add_impression method in the Photo and Event model. Not exactly DRY (Don't Repeat Yourself). I'm considering creating an Impression module to tackle this.

Is this the right approach? Or is there something else that I should be doing?

PS Am not very well tuned with modules yet. So if anyone would like to leave a code snippet or links to articles worth reading. Please do so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-11-23 02:42:18

就我个人而言,我会让 Impression 负责使用类方法创建印象,如下所示:

class Impression < ActiveRecord::Base

  def self.add_impression impressionable, options
    impressionable.impressions.create options
  end

end

然后在控制器中:

Impression.add_impression @photo, options

Personally I would make Impression responsible for creating an impression with a class method, as such:

class Impression < ActiveRecord::Base

  def self.add_impression impressionable, options
    impressionable.impressions.create options
  end

end

Then in the controller:

Impression.add_impression @photo, options
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文