简体   繁体   English

Rails模型的动态关注点

[英]Dynamic concerns for a Rails model

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. 最初的范围仅是上传到Kaltura视频服务器。 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: 这些回调方法(特别是针对Kaltura)的加载方式如下:

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. 现在范围扩大了,我被要求也包括Vimeo上传。

Is it possible to dynamically embed the concerns based on an attribute of the model, or should I consider using STI instead? 是否可以根据模型的属性动态嵌入关注点,还是应该考虑使用STI?

I was thinking about creating a drop-down called "video_type". 我正在考虑创建一个名为“ video_type”的下拉菜单。 How do I include the correct code based on that attribute? 如何基于该属性包括正确的代码?

I was thinking about creating a drop-down called "video_type". 我正在考虑创建一个名为“ 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. 这样,您可以静态地包括这两个关注点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM