改革gem:一机多表

Reform gem: use one model for multiple forms

我正在使用 Reform gem 制作一个用于在我的网上商店结帐的表单对象。我有 Checkout 表单,其中包含 Order 模型的属性,该模型与 Address 模型有关联。

问题是订单模型与同一地址模型有两个关联:

class Order < ActiveRecord::Base
  #...
  belongs_to :billing_address, :class_name => 'Address'
  belongs_to :shipping_address, :class_name => 'Address'
  #...
end

所以我需要设置我的 Checkout 表单以使用地址模型两次。我过去使用的临时方法很简单。但是它工作正常:

class Checkout < Reform::Form
  extend ::ActiveModel::Callbacks
  #...
  property :billing_address, populate_if_empty: Address do
    property :firstname
    property :lastname
    property :address1
    property :address2
    property :phone
    property :city
    property :zipcode
    property :country_id
    property :billing_address_for_id
    property :shipping_address_for_id

    validates :firstname,
              :lastname,
              :address1,
              :phone,
              :city,
              :zipcode,
              :country_id,
              presence: true

    # provided by phony_rails gem
    # validates phone number to be correct and plausible 
    # without country accordance
    validates :phone, phony_plausible: { ignore_record_country_code: true }

    # provided by validates_zipcode gem
    # validates zipcode to be correct due to country alpha2 code
    validates :zipcode, zipcode: { country_code: :country_code }
  end

  property :shipping_address, populate_if_empty: Address do
    property :firstname
    property :lastname
    property :address1
    property :address2
    property :phone
    property :city
    property :zipcode
    property :country_id
    property :billing_address_for_id
    property :shipping_address_for_id

    validates :firstname,
              :lastname,
              :address1,
              :phone,
              :city,
              :zipcode,
              :country_id,
              presence: true

    # provided by phony_rails gem
    # validates phone number to be correct and plausible 
    # without country accordance
    validates :phone, phony_plausible: { ignore_record_country_code: true }

    # provided by validates_zipcode gem
    # validates zipcode to be correct due to country alpha2 code
    validates :zipcode, zipcode: { country_code: :country_code }
  #...
  end

但很明显,必须重构重复的代码。在那里我发现我无法想出任何可行的解决方案。我最后一次尝试如下:

class Checkout < Reform::Form
  extend ::ActiveModel::Callbacks
  #...
  property :billing_address, populate_if_empty: Address, form: BillingAddress 
  property :shipping_address, populate_if_empty: Address, form: ShippingAddress 
  #...
end

class BillingAddress < Reform::Form
  extend ::ActiveModel::Callbacks
  include Address
end

class ShippingAddress < Reform::Form
  extend ::ActiveModel::Callbacks
  include Address
end

module Address
  include Reform::Form::Module

  property :firstname
  property :lastname
  property :address1
  property :address2
  property :phone
  property :city
  property :zipcode
  property :country_id
  property :billing_address_for_id
  property :shipping_address_for_id

  validates :firstname,
            :lastname,
            :address1,
            :phone,
            :city,
            :zipcode,
            :country_id,
            presence: true

  # provided by phony_rails gem
  # validates phone number to be correct and plausible 
  # without country accordance
  validates :phone, phony_plausible: { ignore_record_country_code: true }

  # provided by validates_zipcode gem
  # validates zipcode to be correct due to country alpha2 code
  validates :zipcode, zipcode: { country_code: :country_code }
end

并使用该设置

@checkout = Checkout.new(@order)

给我错误

undefined method `active_record' for #Declarative::Heritage:0x007ff6ea6fb038

那么 Reform 中是否有任何标准方法可以使上面的事情起作用?或者任何人都可以告诉我做错了什么?谢谢!

无意中找到了答案。实际问题是我的模块名称是 Address,这正是我的一个模型的名称。这显然引起了一些冲突。所以我将 Address 更改为 AddressModule。

我的工作设置是:

class Checkout < Reform::Form
  extend ::ActiveModel::Callbacks
  #...
  property :billing_address, populate_if_empty: Address, form: BillingAddress 
  property :shipping_address, populate_if_empty: Address, form: ShippingAddress 
  #...
end

class BillingAddress < Reform::Form
  extend ::ActiveModel::Callbacks
  include AddressModule
end

class ShippingAddress < Reform::Form
  extend ::ActiveModel::Callbacks
  include AddressModule
end

module AddressModule
  include Reform::Form::Module

  property :firstname
  property :lastname
  property :address1
  property :address2
  property :phone
  property :city
  property :zipcode
  property :country_id
  property :billing_address_for_id
  property :shipping_address_for_id

  validates :firstname,
            :lastname,
            :address1,
            :phone,
            :city,
            :zipcode,
            :country_id,
            presence: true

  # provided by phony_rails gem
  # validates phone number to be correct and plausible 
  # without country accordance
  validates :phone, phony_plausible: { ignore_record_country_code: true }

  # provided by validates_zipcode gem
  # validates zipcode to be correct due to country alpha2 code
  validates :zipcode, zipcode: { country_code: :country_code }
end