method.rb
1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class DeliveryPlugin::Method < ActiveRecord::Base
extend CurrencyHelper::ClassMethods
Types = ['pickup', 'deliver']
# see also: Profile::LOCATION_FIELDS
AddressFields = %w[
address address_line2 address_reference district city state country_name zip_code
].map(&:to_sym)
attr_accessible :profile, :delivery_type, :name, :description,
:fixed_cost, :free_over_price
belongs_to :profile
has_many :delivery_options, class_name: 'DeliveryPlugin::Option', foreign_key: :delivery_method_id, dependent: :destroy
validates_presence_of :profile
validates_presence_of :name
validates_inclusion_of :delivery_type, in: Types
scope :pickup, conditions: {delivery_type: 'pickup'}
scope :delivery, conditions: {delivery_type: 'deliver'}
def pickup?
self.delivery_type == 'pickup'
end
def deliver?
self.delivery_type == 'deliver'
end
def has_cost? order_price=nil
if order_price.present? and order_price.nonzero? and self.free_over_price.present? and self.free_over_price.nonzero?
order_price <= self.free_over_price
else
self.fixed_cost.present? and self.fixed_cost.nonzero?
end
end
def free? order_price=nil
!self.has_cost?
end
def cost order_price=nil
if self.has_cost?(order_price) then self.fixed_cost.to_f else 0 end
end
has_currency :fixed_cost
has_currency :free_over_price
has_currency :cost
protected
end