Class: Cartify::AddressesForm
- Inherits:
-
Object
- Object
- Cartify::AddressesForm
- Includes:
- ActiveModel::Model
- Defined in:
- app/forms/cartify/addresses_form.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#use_billing ⇒ Object
readonly
Returns the value of attribute use_billing.
Instance Method Summary collapse
- #billing ⇒ Object
- #errors ⇒ Object
-
#initialize(params = false) ⇒ AddressesForm
constructor
A new instance of AddressesForm.
- #save ⇒ Object
- #shipping ⇒ Object
Constructor Details
#initialize(params = false) ⇒ AddressesForm
Returns a new instance of AddressesForm.
8 9 10 11 12 13 14 |
# File 'app/forms/cartify/addresses_form.rb', line 8 def initialize(params = false) @save = false @params = params @target = Cartify::Order.find_by(id: order_id) || Cartify.user_class.find_by(id: user_id) || Cartify.user_class.new end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
6 7 8 |
# File 'app/forms/cartify/addresses_form.rb', line 6 def params @params end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
6 7 8 |
# File 'app/forms/cartify/addresses_form.rb', line 6 def target @target end |
#use_billing ⇒ Object (readonly)
Returns the value of attribute use_billing.
6 7 8 |
# File 'app/forms/cartify/addresses_form.rb', line 6 def use_billing @use_billing end |
Instance Method Details
#billing ⇒ Object
27 28 29 30 31 |
# File 'app/forms/cartify/addresses_form.rb', line 27 def billing fresh_bill = target.addresses.find_or_initialize_by(type: 'Cartify::Billing') fresh_bill.assign_attributes(params_for(:billing)) if save? @billing ||= fresh_bill end |
#errors ⇒ Object
23 24 25 |
# File 'app/forms/cartify/addresses_form.rb', line 23 def errors { billing: billing.errors, shipping: shipping.errors } end |
#save ⇒ Object
16 17 18 19 20 21 |
# File 'app/forms/cartify/addresses_form.rb', line 16 def save @save = true return false unless valid? persist! true end |
#shipping ⇒ Object
33 34 35 36 37 |
# File 'app/forms/cartify/addresses_form.rb', line 33 def shipping fresh_shipp = target.addresses.find_or_initialize_by(type: 'Cartify::Shipping') fresh_shipp.assign_attributes(params_for(:shipping)) if save? @shipping ||= fresh_shipp end |