Rails模型的动态关注点(Dynamic concerns for a Rails model)

编程入门 行业动态 更新时间:2024-10-28 16:20:34
Rails模型的动态关注点(Dynamic concerns for a Rails model)

对于一个项目,我需要能够将视频上传到视频服务器。 起初,范围只是上传到Kaltura视频服务器。 为了实现这一点,我创建了一个简单的关注点,它为处理上传的模型添加了一些回调方法。

这些回调方法(特别是对于Kaltura)正在加载如下:

class Videofile < ActiveRecord::Base include KalturaBox::Entry end

它们包含这样的基础知识(简化版):

module KalturaBox module Entry extend ActiveSupport::Concern included do before_save :create_kaltura_item, on: :create private def create_kaltura_item puts 'create callback called' self.upload end end end

现在范围已经扩大,我被要求也包括Vimeo上传作为选项。

是否可以根据模型的属性动态嵌入关注点,还是应该考虑使用STI?

我正在考虑创建一个名为“video_type”的下拉列表。 如何根据该属性包含正确的代码?

For a project I needed the ability to upload video's to a video server. At first the scope was to only upload to a Kaltura video server. To enable this, I created a simple concern that added some callback methods to the model which handle the uploads.

These callback methods (specifically for Kaltura) are being loaded like so:

class Videofile < ActiveRecord::Base include KalturaBox::Entry end

And they contain basics like this (simplified version):

module KalturaBox module Entry extend ActiveSupport::Concern included do before_save :create_kaltura_item, on: :create private def create_kaltura_item puts 'create callback called' self.upload end end end

Now the scope has broadened, and I was asked to also include Vimeo uploads as an option.

Is it possible to dynamically embed the concerns based on an attribute of the model, or should I consider using STI instead?

I was thinking about creating a drop-down called "video_type". How do I include the correct code based on that attribute?

最满意答案

我正在考虑创建一个名为“video_type”的下拉列表。 如何根据该属性包含正确的代码?

使用条件回调 。

before_save :create_kaltura_item, on: :create, if: :kaltura? def kaltura? video_type == 'kaltura' end

这样您就可以静态地包含这两个问题。

I was thinking about creating a drop-down called "video_type". How do I include the correct code based on that attribute?

Use conditional callbacks.

before_save :create_kaltura_item, on: :create, if: :kaltura? def kaltura? video_type == 'kaltura' end

This way you can include both concerns statically.

更多推荐

本文发布于:2023-07-26 19:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关注点   模型   动态   Rails   concerns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!