DRY rails 模型中的多个相同关联

DRY Multiple same associations in rails Model

在我的模型中,我有多个 has_one 关联,例如

has_one  :t1_for_self_order, -> { t1_for_self_order }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :shipping_charges_for_t1, -> { shipping_charges_for_t1 }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :t2_for_self_order, -> { t2_for_self_order }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :shipping_charges_for_t2, -> { shipping_charges_for_t2 }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :minimum_value_for_gifting, -> { minimum_value_for_gifting }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :international_fulfillment_fees, -> { international_fulfillment_fees }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'

你注意到在所有的关联中只有同一个scope name的关联名称不同,其他的内容都是一样的

所以我想编写一个函数来删除所有这些重复项。 我认为使用 Meta 编程是可能的,但我不确定该怎么做

此外,我还有一组嵌套属性供我们使用。

    THRESHOLD_INTENT = ["t1_for_self_order", "shipping_charges_for_t1", "t2_for_self_order", "shipping_charges_for_t2",
                    "minimum_value_for_gifting", "international_fulfillment_fees", "menu_customization_fee",
                    "total_menu_customization_fee_cap", "video_message_fee", "total_video_message_fee_cap",
                    "swag_price", "box_types_with_price", "customization_box_types_with_price", "custom_note_fee",
                    "non_us_fees"]

我喜欢这样

THRESHOLD_INTENT.each do |t_intent|
  has_one  t_intent.to_sym, as: :source, dependent: :destroy, inverse_of: :source, class_name: 'Spree::PriceMapping'
end

但是我怎样才能通过这样的范围

-> { t1_for_self_order }

尝试提取公共属性,例如

relationship_attributes = { as: :source, dependent: :destroy, inverse_of: :source, class_name: 'Spree::PriceMapping' }

has_one :t1_for_self_order, -> { t1_for_self_order }, relationship_attributes
has_one :shipping_charges_for_t1, -> { shipping_charges_for_t1 }, relationship_attributes
has_one :t2_for_self_order, -> { t2_for_self_order }, relationship_attributes
has_one :shipping_charges_for_t2, -> { shipping_charges_for_t2 }, relationship_attributes
has_one :minimum_value_for_gifting, -> { minimum_value_for_gifting }, relationship_attributes
has_one  :international_fulfillment_fees, -> { international_fulfillment_fees }, relationship_attributes

我不会使用元编程,它会使阅读变得困难

您可以遍历关联名称列表并使用每个项目的所有参数调用 has_onesend(association) 需要将关联作用域作为方法调用。

ASSOCIATIONS = [:t1_for_self_order, :shipping_charges_for_t1, :t2_for_self_order, :shipping_charges_for_t2, :minimum_value_for_gifting, :international_fulfillment_fees]

ASSOCIATIONS.each do |association|
  has_one association, -> { send(association) }, as: :source, dependent: :destroy, inverse_of: :source, class_name: 'Spree::PriceMapping'
end