改造宝石:将一种模型用于多种形式

时间:2016-04-26 10:02:23

标签: ruby-on-rails ruby forms reform

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

问题是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

那么改革中是否有任何标准方法可以使事情发挥作用?或者任何人都能说出我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:2)

无意中我找到了答案。实际问题是我的模块名称是Address什么是我的一个模型的确切名称。它显然发生了一些冲突。所以我已经将Address更改为AddressModule。

我的工作设置将是:

bower.json