Class: Comable::User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
CartOwner, Liquidable, Ransackable, RoleOwner
Defined in:
app/models/comable/user.rb

Instance Method Summary collapse

Methods included from CartOwner

#add_cart_item, #cart, #move_cart_item, #remove_cart_item, #reset_cart_item

Instance Method Details

#after_set_userObject



77
78
79
80
81
82
83
84
85
# File 'app/models/comable/user.rb', line 77

def after_set_user
  return unless current_guest_token

  guest_order = Comable::Order.incomplete.preload(:order_items).where(guest_token: current_guest_token).first
  return unless guest_order

  incomplete_order.inherit!(guest_order)
  inherit_cart_items(guest_order)
end

#as_json(options = {}) ⇒ Object



95
96
97
# File 'app/models/comable/user.rb', line 95

def as_json(options = {})
  super options.merge(include: [:bill_address, :ship_address], methods: [:text])
end

#cart_itemsObject



68
69
70
# File 'app/models/comable/user.rb', line 68

def cart_items
  incomplete_order.order_items
end

#human_idObject



87
88
89
# File 'app/models/comable/user.rb', line 87

def human_id
  "##{id}"
end

#incomplete_orderObject



72
73
74
75
# File 'app/models/comable/user.rb', line 72

def incomplete_order
  @incomplete_order = nil if @incomplete_order.try(:completed?)
  @incomplete_order ||= find_incomplete_order || initialize_incomplete_order
end

#ordersObject

Add conditions for the orders association. Override method of the orders association to support Rails 3.x.



40
41
42
# File 'app/models/comable/user.rb', line 40

def orders
  super.complete.order('completed_at DESC, id DESC')
end

#other_addressesObject



44
45
46
# File 'app/models/comable/user.rb', line 44

def other_addresses
  addresses - [bill_address] - [ship_address]
end

#reload(*_) ⇒ Object

TODO: Add a test case



61
62
63
64
65
66
# File 'app/models/comable/user.rb', line 61

def reload(*_)
  super.tap do
    @cart_items = nil
    @incomplete_order = nil
  end
end

#signed_in?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/comable/user.rb', line 56

def signed_in?
  !new_record?
end

#textObject



91
92
93
# File 'app/models/comable/user.rb', line 91

def text
  bill_address ? "#{bill_full_name} (#{email})" : email
end

#update_bill_address_by(bill_address) ⇒ Object



48
49
50
# File 'app/models/comable/user.rb', line 48

def update_bill_address_by(bill_address)
  update_attributes(bill_address: addresses.find_or_clone(bill_address))
end

#update_ship_address_by(ship_address) ⇒ Object



52
53
54
# File 'app/models/comable/user.rb', line 52

def update_ship_address_by(ship_address)
  update_attributes(ship_address: addresses.find_or_clone(ship_address))
end

#with_cookies(cookies) ⇒ Object



33
34
35
36
# File 'app/models/comable/user.rb', line 33

def with_cookies(cookies)
  @cookies = cookies
  self
end