【问题标题】:Create one interface for many models?为多个模型创建一个界面?
【发布时间】:2015-10-06 01:29:20
【问题描述】:

我目前正在开发一个用 Ruby on Rails 编写的自定义商店软件,其中包含三个礼品/优惠券模型。一种用于百分比折扣,一种用于特定金额,一种用于团体。现在需要对其进行大修,我正在考虑创建一个界面、与旧模型具有多态关系的模型或类似的东西,以便为所有三个模型添加特性和功能。

你会怎么处理?我想我会选择具有多态关系的模型。欢迎提出任何建议!

【问题讨论】:

    标签: ruby-on-rails ruby interface polymorphism


    【解决方案1】:

    您可以使用模块(可能会扩展关注点)。

    How to use concerns in Rails 4

    module Giftable
        extend ActiveSupport::Concern
    
        included do 
           # add relationships, validations etc.
        end
    
        def some_instance_method
           ..
        end
    
        module ClassMethods     
            def some_class_method
            ..
            end
        end 
    end
    

    礼物/代金券模型之一。

    class SimpleVoucher < ActiveRecord::Base    
        include Giftable
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      相关资源
      最近更新 更多