Reform gem: use one model for multiple forms

VAD

I am using Reform gem to make a form object for checkout in my web store. I have the Checkout form which contains properties for Order model, which has associations with Address model.

Problem is the Order model has two association with the same Address model:

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

So I need to set up my Checkout form to use Address model twice. The temporary approach I used to apply was trivial. However it was working fine:

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

But it's obvious that duplicated code must be refactored. And there I found that I can't come up with any working solution. My last attempt was like following:

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

And with that setup

@checkout = Checkout.new(@order)

was giving me the error

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

So is there any standard approach in Reform to make things above work? Or can anybody tell what I do wrong? Thanks!

VAD

Accidentally I've found an answer. The actual problem was my module name was Address what is an exactly name of one of my models. And it made some conflict obviously. So I've changed Address to AddressModule.

And my working setup will be:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple update forms for one model

From Dev

Rails one model multiple forms one page

From Dev

AngularJS - multiple forms - one ng-model?

From Dev

Form Objects: Use Reform Gem with User / Pet Example

From Dev

How to use reform gem with two models that share identical column names

From Dev

rails 4 active admin multiple forms for one model

From Dev

MVC EditorFor model binding for multiple edit forms on one page

From Dev

use geocoder gem to store address of one model search with address of the other model

From Dev

How to create multiple forms for Single Model and all forms must be submitted by one submit button?

From Dev

Keystone.js: use one model in multiple sites

From Dev

How to use multiple ng-model in one function?

From Dev

How to use one model for multiple tables in laravel 5.6

From Dev

Spring MVC model for multiple forms

From Dev

Use multiple model in one modelform and save one user input field to two model

From Dev

Uninitialized constant ArticleFormTest::Article in Reform gem tutorial

From Dev

Rails 4 with Reform GEM and simple_form

From Dev

Multiple Login Forms On One Page

From Dev

Clear multiple forms on one page

From Dev

Requiring multiple modules in one project/gem

From Dev

Django: One model multi-steps forms

From Dev

How to reuse a model for multiple forms in Spring MVC?

From Dev

MVC Multiple tables one model

From Dev

Multiple model or DB for one request?

From Dev

Laravel One Model Multiple Controllers

From Dev

MVC Multiple tables one model

From Dev

Multiple tables in one model - Laravel

From Dev

Django, multiple ModelAdmin for one Model

From Dev

Rails Paperclip Gem Saving Multiple Attachments per Model instance

From Dev

Send multiple forms data on one page with ajax

Related Related

  1. 1

    Multiple update forms for one model

  2. 2

    Rails one model multiple forms one page

  3. 3

    AngularJS - multiple forms - one ng-model?

  4. 4

    Form Objects: Use Reform Gem with User / Pet Example

  5. 5

    How to use reform gem with two models that share identical column names

  6. 6

    rails 4 active admin multiple forms for one model

  7. 7

    MVC EditorFor model binding for multiple edit forms on one page

  8. 8

    use geocoder gem to store address of one model search with address of the other model

  9. 9

    How to create multiple forms for Single Model and all forms must be submitted by one submit button?

  10. 10

    Keystone.js: use one model in multiple sites

  11. 11

    How to use multiple ng-model in one function?

  12. 12

    How to use one model for multiple tables in laravel 5.6

  13. 13

    Spring MVC model for multiple forms

  14. 14

    Use multiple model in one modelform and save one user input field to two model

  15. 15

    Uninitialized constant ArticleFormTest::Article in Reform gem tutorial

  16. 16

    Rails 4 with Reform GEM and simple_form

  17. 17

    Multiple Login Forms On One Page

  18. 18

    Clear multiple forms on one page

  19. 19

    Requiring multiple modules in one project/gem

  20. 20

    Django: One model multi-steps forms

  21. 21

    How to reuse a model for multiple forms in Spring MVC?

  22. 22

    MVC Multiple tables one model

  23. 23

    Multiple model or DB for one request?

  24. 24

    Laravel One Model Multiple Controllers

  25. 25

    MVC Multiple tables one model

  26. 26

    Multiple tables in one model - Laravel

  27. 27

    Django, multiple ModelAdmin for one Model

  28. 28

    Rails Paperclip Gem Saving Multiple Attachments per Model instance

  29. 29

    Send multiple forms data on one page with ajax

HotTag

Archive