Class: ShopOrder

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/shop_order.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_session(session) ⇒ Object

A hookable method used by the site controller



134
135
136
# File 'app/models/shop_order.rb', line 134

def find_by_session(session)
  find(session)
end

.paramsObject



150
151
152
# File 'app/models/shop_order.rb', line 150

def params
  [ :id, :notes, :status ]
end

.scope_by_status(status) ⇒ Object

Will scope the contained find calls to a specific status



139
140
141
142
143
144
145
146
147
148
# File 'app/models/shop_order.rb', line 139

def scope_by_status(status)
  case status
  when 'new', 'shipped', 'paid'
    with_scope(:find => { :conditions => {:status => status}}) do
      yield
    end
  else
    yield
  end
end

Instance Method Details

#add(*attrs) ⇒ Object



72
73
74
75
76
77
# File 'app/models/shop_order.rb', line 72

def add(*attrs)
  add!(*attrs)
  true
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
  false
end

#add!(id, quantity = nil, type = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/shop_order.rb', line 47

def add!(id, quantity = nil, type = nil)
  result = true
  
  begin
    line_item = line_items.find(id)
    quantity = line_item.quantity + quantity.to_i
    
    modify!(id,quantity)
  rescue
    quantity  ||= 1
    type      ||= 'ShopProduct'
    
    if line_items.exists?({:item_id => id, :item_type => type})
      line_item = line_items.first(:conditions => {:item_id => id, :item_type => type})
      quantity  = line_item.quantity + quantity.to_i
    
      modify!(line_item.id, quantity)
    else
      line_items.create!({:item_id => id, :item_type => type, :quantity => quantity})
    end
  end
  
  result
end

#clear!Object



110
111
112
# File 'app/models/shop_order.rb', line 110

def clear!
  line_items.destroy_all
end

#modify(*attrs) ⇒ Object



89
90
91
92
93
94
# File 'app/models/shop_order.rb', line 89

def modify(*attrs)
  modify!(*attrs)
  true
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
  false
end

#modify!(id, quantity = 1) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'app/models/shop_order.rb', line 79

def modify!(id, quantity = 1)
  quantity = quantity.to_i
  if quantity <= 0
    remove!(id)
  else
    line_item = line_items.find(id)
    line_item.update_attributes! :quantity => quantity
  end
end

#new?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/models/shop_order.rb', line 118

def new?
  self.status === 'new'
end

#paid?Boolean

Returns:

  • (Boolean)


122
123
124
125
# File 'app/models/shop_order.rb', line 122

def paid?
  return false unless self.payment.present?
  self.payment.amount === self.price and self.status === 'paid'
end

#priceObject



19
20
21
22
23
24
25
# File 'app/models/shop_order.rb', line 19

def price
  price = 0; line_items.map { |l| price += l.price }
  
  price += tax if Radiant::Config['shop.tax_strategy'] === 'exclusive'
  
  price
end

#quantityObject



114
115
116
# File 'app/models/shop_order.rb', line 114

def quantity
  self.line_items.sum(:quantity)
end

#remove(*attrs) ⇒ Object



103
104
105
106
107
108
# File 'app/models/shop_order.rb', line 103

def remove(*attrs)
  remove!(*attrs)
  true
rescue ActiveRecord::RecordNotFound
  false
end

#remove!(id) ⇒ Object



96
97
98
99
100
101
# File 'app/models/shop_order.rb', line 96

def remove!(id)
  line_item = line_items.find(id)
  line_item.destroy
  
  true
end

#shipped?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/models/shop_order.rb', line 127

def shipped?
  self.status === 'shipped'
end

#taxObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/shop_order.rb', line 32

def tax
  price      = 0; line_items.map { |l| price += l.price; }
  tax        = 0
  percentage = Radiant::Config['shop.tax_percentage'].to_f * 0.01
  
  case Radiant::Config['shop.tax_strategy']
  when 'inclusive'
    tax = price - (price / (1 + percentage))
  when 'exclusive'
    tax = price * percentage
  end
  
  BigDecimal.new(tax.to_s)
end

#weightObject



27
28
29
30
# File 'app/models/shop_order.rb', line 27

def weight
  weight = 0; line_items.map { |l| weight += l.weight }
  weight
end