在 Ruby on Rails 中自动替换模型属性中类似于 twitter 的用户名

发布于 2024-11-03 20:01:25 字数 495 浏览 2 评论 0原文

我很难弄清楚如何以 MVC 方式做到这一点。我有一个 Comment 模型,其中包含 body 属性。该属性可能包含如下提及:

嗨!这是我提到@某人。

每次有人发表评论时,模型中的访问器方法都会将所有 @mention 转换为类似 #user:231# 的内容,其中 231 是用户的 ID。这样,如果提到的用户更改了他们的用户名,我仍然可以链接 &在旧评论中毫无问题地提及他。

现在,我希望能够访问 body 属性并获取已转换为链接的提及。根据我的研究,从模型内部以 MVC 方式执行此操作似乎是不可能的。

有什么简单的方法可以做到这一点吗?我不想转换控制器上的所有提及,因为我认为这可能会导致重复的代码和不可测试的代码。

有人可以给我一些建议吗?

谢谢!

I'm having a very hard time trying to figure out how to do this the MVC way. I have a Comment model which holds a body attribute. This attribute may contain mentions such as the following:

Hi! This is me mentioning @someone.

Everytime someone posts a comment, an accessor method in the model converts all @mention to something like #user:231# where 231 would be the user's id. This way, if the mentioned user changes their username, I can still link & mention him without problems on older comments.

Now, I want to be able to access the body attribute and get the mentions already converted to links. It appears that doing this the MVC way, from within the model is not possible from what I have investigated.

Is there any easy way to do this? I don't wanna have to convert all the mentions on the controller because I think it could lead to repeated code and non-testable code.

Could anyone give me some advice on this?

Thanks!

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

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

发布评论

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

评论(3

浅暮の光 2024-11-10 20:01:25

将消息解析为特定格式,然后将其重新保存在数据库中,以便日后进行编辑,这是愚蠢的。很抱歉这么直白,但这样做从根本上来说是错误的,原因有一个:当用户稍后编辑消息时,他们会看到格式化的文本除非您将其格式化回来。你真的愿意为此负责吗?

我希望不会。因为你是一名程序员,所以你天生很懒,并且希望用尽可能少的步骤来完成事情。

我建议解决此问题的方法是在页面上显示消息时解析该消息。在你对我大喊大叫说如果你有大量的点击量,这会是计算密集型的,请听我说完。当它显示在页面上时,您可以像这样缓存它:

<% cache comment do %>
  # code goes here
<% end %>

这会将最终输出存储在您使用 Rails 设置的任何缓存中,可能是 Memcached 或 Redis,使用的缓存键包括评论的 updated_at 时间戳。注意这一点,以后会有用的。

从该缓存中检索将比解析它更快,并且比在其版本之间来回转换消息更容易。

当评论更新时,updated_at 时间戳将会不同,因此新评论将首先呈现,然后缓存。在 Memcached 中(据我所知),如果需要更多内存,它将清除尚未引用的最旧的缓存键,从而清除较旧的注释。

Parsing the message into a particular format and then re-saving it in the database where it can then be edited at a later date is silly. I'm sorry to be so blunt, but doing it this way is fundamentally broken for one major reason: when a user goes to edit the message later on, they'll see the formatted text unless you format it back. Do you really want to be responsible for doing this?

I would hope not. Because you're a programmer, you're naturally lazy and would like to do things in as few steps as possible.

What I would recommend doing to solve this problem is to parse the message when you display it on the page. Before you go screaming at me that this is computationally intensive if you've got a large amount of hits, hear me out. When it's displayed on the page, you can then cache it like this:

<% cache comment do %>
  # code goes here
<% end %>

This will store the final output in whatever cache you've set up with Rails, possibly Memcached or Redis, using a cache key which includes the comment's updated_at timestamp. Pay attention to this, it'll be useful later.

Retrieval from this cache will be faster than parsing it, and will be easier for you than to convert the message back and forth between its versions.

When a comment is updated, the updated_at timestamp will be different and so the new comment will be rendered first, then cached. In Memcached (so I'm told) it will clear the oldest cache key that hasn't been referenced if it needs more memory, thereby cleaning out the older comments.

风吹雨成花 2024-11-10 20:01:25

你最终不会破坏原始消息吗?假设我最初发布的内容是:

“嗨!这是我提到的@bob。”

据我了解,您希望将其存储为:

“嗨!这是我提到的#user:1#”

现在,如果鲍勃将其用户名更改为“fred”,我的消息现在将如下所示:

“嗨!这是我提到的@fred”,

简单地存储消息和它提到的用户之间的多对多关系可能会更容易。这样,您仍然可以轻松查看哪些消息提到了特定用户,但无需修改原始消息即可。

如果您需要将每个提及转换为链接,您可以按照它们在消息中出现的顺序对关系表中的条目进行排序。

Wouldn't you end up mangling the original message? Let's say I originally posted:

"Hi! This is me mentioning @bob."

From what I understand, you want to store this as:

"Hi! This is me mentioning #user:1#"

Now, if bob were to change his username to "fred", my message would now look like this:

"Hi! This is me mentioning @fred"

It may be easier to simply store a many-to-many relation between messages and users it mentions. That way, you still can easily see which messages mention a specific user, but you don't need to mangle the original message to do so.

If you need to convert each mention into a link, you could order the entries in the relationship table in the same order that they appear in the message.

昵称有卵用 2024-11-10 20:01:25

也许这个宝石可以帮助你https://github.com/twitter/twitter-text-rb

首先,包含来自您的类或助手的 Twitter::Autolink 模块

module ApplicationHelper
  include Twitter::Autolink
end

从视图中,您可以通过以下方式调用它:

<%= auto_link("Hi @john_doe, welcome to #ruby") %>

它将生成到 twitter john_doe 用户名和 ruby​​ 主题标签的链接

maybe this gem help you https://github.com/twitter/twitter-text-rb

First, include Twitter::Autolink module from your class or helper

module ApplicationHelper
  include Twitter::Autolink
end

From views, you can call it by :

<%= auto_link("Hi @john_doe, welcome to #ruby") %>

it will generate link to twitter john_doe username and ruby hashtag

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