Class: Cartify::AddressesForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/forms/cartify/addresses_form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'app/forms/cartify/addresses_form.rb', line 6

def params
  @params
end

#targetObject (readonly)

Returns the value of attribute target.



6
7
8
# File 'app/forms/cartify/addresses_form.rb', line 6

def target
  @target
end

#use_billingObject (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

#billingObject



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

#errorsObject



23
24
25
# File 'app/forms/cartify/addresses_form.rb', line 23

def errors
  { billing: billing.errors, shipping: shipping.errors }
end

#saveObject



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

#shippingObject



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