comable_core 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/helpers/comable/application_helper.rb +17 -10
- data/app/models/comable/ability.rb +1 -1
- data/app/models/comable/address.rb +2 -2
- data/app/models/comable/order/associations.rb +22 -0
- data/app/models/comable/order/callbacks.rb +43 -0
- data/app/models/comable/order/scopes.rb +17 -0
- data/app/models/comable/order/validations.rb +39 -0
- data/app/models/comable/order.rb +63 -72
- data/app/models/comable/order_item/csvable.rb +32 -0
- data/app/models/comable/{order_detail.rb → order_item.rb} +19 -10
- data/app/models/comable/payment.rb +82 -0
- data/app/models/comable/payment_method.rb +1 -0
- data/app/models/comable/product/csvable.rb +20 -0
- data/app/models/comable/product.rb +2 -7
- data/app/models/comable/shipment.rb +79 -0
- data/app/models/comable/stock/csvable.rb +26 -0
- data/app/models/comable/stock.rb +2 -17
- data/app/models/comable/tracker.rb +17 -0
- data/app/models/comable/{customer.rb → user.rb} +11 -23
- data/app/models/concerns/comable/checkout.rb +35 -36
- data/app/models/concerns/comable/importable.rb +67 -0
- data/app/views/comable/order_mailer/complete.text.erb +9 -8
- data/config/initializers/comma.rb +8 -0
- data/config/locales/en.yml +105 -14
- data/config/locales/ja.yml +92 -19
- data/db/migrate/{20140120032559_create_comable_customers.rb → 20140120032559_create_comable_users.rb} +6 -6
- data/db/migrate/20140723175431_create_comable_orders.rb +2 -4
- data/db/migrate/{20140723175810_create_comable_order_details.rb → 20140723175810_create_comable_order_items.rb} +3 -3
- data/db/migrate/20140817194104_create_comable_payment_methods.rb +1 -0
- data/db/migrate/20141024025526_create_comable_addresses.rb +1 -1
- data/db/migrate/20150423095210_create_comable_shipments.rb +12 -0
- data/db/migrate/20150511171940_create_comable_payments.rb +11 -0
- data/db/migrate/20150513185230_create_comable_trackers.rb +12 -0
- data/db/seeds/comable/{customers.rb → users.rb} +3 -3
- data/lib/comable/core/configuration.rb +7 -1
- data/lib/comable/core/engine.rb +0 -25
- data/lib/comable/payment_provider/base.rb +16 -0
- data/lib/comable_core.rb +6 -1
- data/lib/comma_extractor_extentions.rb +31 -0
- data/lib/generators/comable/install/templates/config/initializers/comable.rb +16 -1
- metadata +79 -8
- data/lib/comable/errors.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3586df9a1e1a2267feb7f84f1658f21f92909b2
|
4
|
+
data.tar.gz: 94e817e742c94849a8eb08ebf7f8de9c983c3a33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b03e1b7ff2c55f92048da4ab2fa08fb7c503ea2e6d70da22eebf8b1cc8f98c6c2bfb11241811c76fce533cad8cdc1dc190bf158290f58cbda72555902498132
|
7
|
+
data.tar.gz: 7c24b354ae4934ba2670851b2390e18f1c292f9675cde73ac251bf0799e1d49b81f1d551926db6afed7dd6cc5074dae95c9542b76b0fc245b4a1e989cb36f75d
|
@@ -4,15 +4,17 @@ module Comable
|
|
4
4
|
@current_store ||= Comable::Store.instance
|
5
5
|
end
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# ...
|
12
|
-
# end
|
7
|
+
def current_comable_user
|
8
|
+
resource = current_admin_user || current_user || Comable::User.new
|
9
|
+
resource.with_cookies(cookies)
|
10
|
+
end
|
13
11
|
|
14
12
|
def current_order
|
15
|
-
|
13
|
+
current_comable_user.incomplete_order
|
14
|
+
end
|
15
|
+
|
16
|
+
def current_trackers
|
17
|
+
@curent_trackers ||= (controller_name == 'orders' && action_name == 'create') ? Comable::Tracker.activated : Comable::Tracker.activated.with_place(:everywhere)
|
16
18
|
end
|
17
19
|
|
18
20
|
def next_order_path
|
@@ -25,7 +27,7 @@ module Comable
|
|
25
27
|
end
|
26
28
|
|
27
29
|
def store_location
|
28
|
-
session[:
|
30
|
+
session[:user_return_to] = request.fullpath.gsub('//', '/')
|
29
31
|
end
|
30
32
|
|
31
33
|
def name_with_honorific(name)
|
@@ -41,14 +43,19 @@ module Comable
|
|
41
43
|
].join(' ')
|
42
44
|
end
|
43
45
|
|
46
|
+
def liquidize(content, arguments)
|
47
|
+
string = Liquid::Template.parse(content).render(arguments.stringify_keys)
|
48
|
+
string.respond_to?(:html_safe) ? string.html_safe : string
|
49
|
+
end
|
50
|
+
|
44
51
|
private
|
45
52
|
|
46
53
|
def after_sign_in_path_for(_resource)
|
47
|
-
session.delete(:
|
54
|
+
session.delete(:user_return_to) || comable.root_path
|
48
55
|
end
|
49
56
|
|
50
57
|
def after_sign_out_path_for(_resource)
|
51
|
-
session.delete(:
|
58
|
+
session.delete(:user_return_to) || comable.root_path
|
52
59
|
end
|
53
60
|
|
54
61
|
def after_sign_up_path_for(resource)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Comable
|
2
2
|
class Address < ActiveRecord::Base
|
3
|
-
belongs_to :
|
3
|
+
belongs_to :user, class_name: Comable::User.name, autosave: false
|
4
4
|
|
5
5
|
validates :family_name, presence: true, length: { maximum: 255 }
|
6
6
|
validates :first_name, presence: true, length: { maximum: 255 }
|
@@ -25,7 +25,7 @@ module Comable
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def attributes_without_id
|
28
|
-
attributes.except('id', '
|
28
|
+
attributes.except('id', 'user_id')
|
29
29
|
end
|
30
30
|
|
31
31
|
def full_name
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Comable
|
2
|
+
class Order < ActiveRecord::Base
|
3
|
+
module Associations
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
belongs_to :user, class_name: Comable::User.name, autosave: false
|
8
|
+
belongs_to :bill_address, class_name: Comable::Address.name, autosave: true, dependent: :destroy
|
9
|
+
belongs_to :ship_address, class_name: Comable::Address.name, autosave: true, dependent: :destroy
|
10
|
+
has_many :order_items, dependent: :destroy, class_name: Comable::OrderItem.name, inverse_of: :order
|
11
|
+
has_one :payment, dependent: :destroy, class_name: Comable::Payment.name, inverse_of: :order
|
12
|
+
has_one :shipment, dependent: :destroy, class_name: Comable::Shipment.name, inverse_of: :order
|
13
|
+
|
14
|
+
accepts_nested_attributes_for :bill_address
|
15
|
+
accepts_nested_attributes_for :ship_address
|
16
|
+
accepts_nested_attributes_for :order_items
|
17
|
+
accepts_nested_attributes_for :payment
|
18
|
+
accepts_nested_attributes_for :shipment
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Comable
|
2
|
+
class Order < ActiveRecord::Base
|
3
|
+
module Callbacks
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
define_model_callbacks :complete
|
8
|
+
|
9
|
+
before_validation :generate_guest_token, on: :create
|
10
|
+
before_validation :clone_addresses_from_user, on: :create
|
11
|
+
before_complete :generate_code
|
12
|
+
after_complete :clone_addresses_to_user
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate_code
|
16
|
+
self.code = loop do
|
17
|
+
random_token = "C#{Array.new(11) { rand(9) }.join}"
|
18
|
+
break random_token unless self.class.exists?(code: random_token)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate_guest_token
|
23
|
+
return if user
|
24
|
+
self.guest_token ||= loop do
|
25
|
+
random_token = SecureRandom.urlsafe_base64(nil, false)
|
26
|
+
break random_token unless self.class.exists?(guest_token: random_token)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def clone_addresses_from_user
|
31
|
+
return unless user
|
32
|
+
self.bill_address ||= user.bill_address.try(:clone)
|
33
|
+
self.ship_address ||= user.ship_address.try(:clone)
|
34
|
+
end
|
35
|
+
|
36
|
+
def clone_addresses_to_user
|
37
|
+
return unless user
|
38
|
+
user.update_bill_address_by bill_address
|
39
|
+
user.update_ship_address_by ship_address
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Comable
|
2
|
+
class Order < ActiveRecord::Base
|
3
|
+
module Scopes
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
scope :complete, -> { where.not(completed_at: nil) }
|
8
|
+
scope :incomplete, -> { where(completed_at: nil) }
|
9
|
+
scope :by_user, -> (user) { where(user_id: user) }
|
10
|
+
scope :this_month, -> { where(completed_at: Time.now.beginning_of_month..Time.now.end_of_month) }
|
11
|
+
scope :this_week, -> { where(completed_at: Time.now.beginning_of_week..Time.now.end_of_week) }
|
12
|
+
scope :last_week, -> { where(completed_at: 1.week.ago.beginning_of_week..1.week.ago.end_of_week) }
|
13
|
+
scope :recent, -> { order('completed_at DESC, id DESC') }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Comable
|
2
|
+
class Order < ActiveRecord::Base
|
3
|
+
module Validations
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
validates :user_id, uniqueness: { scope: :completed_at }, if: :user
|
8
|
+
validates :guest_token, presence: true, uniqueness: { scope: :completed_at }, unless: :user
|
9
|
+
|
10
|
+
with_options if: -> { stated?(:cart) } do |context|
|
11
|
+
context.validates :email, presence: true, length: { maximum: 255 }
|
12
|
+
end
|
13
|
+
|
14
|
+
with_options if: -> { stated?(:orderer) } do |context|
|
15
|
+
context.validates :bill_address, presence: true
|
16
|
+
end
|
17
|
+
|
18
|
+
with_options if: -> { stated?(:delivery) } do |context|
|
19
|
+
context.validates :ship_address, presence: true
|
20
|
+
end
|
21
|
+
|
22
|
+
with_options if: -> { stated?(:shipment) && shipment_required? } do |context|
|
23
|
+
context.validates :shipment, presence: true
|
24
|
+
end
|
25
|
+
|
26
|
+
with_options if: -> { stated?(:payment) && payment_required? } do |context|
|
27
|
+
context.validates :payment, presence: true
|
28
|
+
end
|
29
|
+
|
30
|
+
with_options if: -> { stated?(:confirm) } do |context|
|
31
|
+
context.validates :code, presence: true
|
32
|
+
context.validates :payment_fee, presence: true
|
33
|
+
context.validates :shipment_fee, presence: true
|
34
|
+
context.validates :total_price, presence: true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/app/models/comable/order.rb
CHANGED
@@ -1,125 +1,116 @@
|
|
1
|
+
require_dependency 'comable/order/associations'
|
2
|
+
require_dependency 'comable/order/callbacks'
|
3
|
+
require_dependency 'comable/order/scopes'
|
4
|
+
require_dependency 'comable/order/validations'
|
5
|
+
require_dependency 'comable/order/morrisable'
|
6
|
+
|
1
7
|
module Comable
|
2
8
|
class Order < ActiveRecord::Base
|
3
9
|
include Comable::Checkout
|
4
10
|
include Comable::Ransackable
|
11
|
+
include Comable::Order::Associations
|
12
|
+
include Comable::Order::Callbacks
|
13
|
+
include Comable::Order::Scopes
|
14
|
+
include Comable::Order::Validations
|
5
15
|
include Comable::Order::Morrisable
|
6
16
|
|
7
|
-
|
8
|
-
belongs_to :payment_method, class_name: Comable::PaymentMethod.name, autosave: false
|
9
|
-
belongs_to :shipment_method, class_name: Comable::ShipmentMethod.name, autosave: false
|
10
|
-
belongs_to :bill_address, class_name: Comable::Address.name, autosave: true, dependent: :destroy
|
11
|
-
belongs_to :ship_address, class_name: Comable::Address.name, autosave: true, dependent: :destroy
|
12
|
-
has_many :order_details, dependent: :destroy, class_name: Comable::OrderDetail.name, inverse_of: :order
|
13
|
-
|
14
|
-
accepts_nested_attributes_for :bill_address
|
15
|
-
accepts_nested_attributes_for :ship_address
|
16
|
-
accepts_nested_attributes_for :order_details
|
17
|
-
|
18
|
-
define_model_callbacks :complete
|
19
|
-
before_validation :generate_guest_token, on: :create
|
20
|
-
before_validation :clone_addresses_from_customer, on: :create
|
21
|
-
after_complete :clone_addresses_to_customer
|
17
|
+
ransack_options attribute_select: { associations: [:payment, :shipment] }, ransackable_attributes: { except: [:bill_address_id, :ship_address_id] }
|
22
18
|
|
23
|
-
|
24
|
-
scope :incomplete, -> { where(completed_at: nil) }
|
25
|
-
scope :by_customer, -> (customer) { where(customer_id: customer) }
|
26
|
-
scope :this_month, -> { where(completed_at: Time.now.beginning_of_month..Time.now.end_of_month) }
|
27
|
-
scope :this_week, -> { where(completed_at: Time.now.beginning_of_week..Time.now.end_of_week) }
|
28
|
-
scope :last_week, -> { where(completed_at: 1.week.ago.beginning_of_week..1.week.ago.end_of_week) }
|
29
|
-
|
30
|
-
ransack_options ransackable_attributes: { except: [:shipment_method_id, :payment_method_id, :bill_address_id, :ship_address_id] }
|
19
|
+
liquid_methods :code, :payment_fee, :shipment_fee, :item_total_price, :total_price
|
31
20
|
|
32
21
|
delegate :full_name, to: :bill_address, allow_nil: true, prefix: :bill
|
33
22
|
delegate :full_name, to: :ship_address, allow_nil: true, prefix: :ship
|
23
|
+
delegate :state, :human_state_name, to: :payment, allow_nil: true, prefix: true
|
24
|
+
delegate :state, :human_state_name, to: :shipment, allow_nil: true, prefix: true
|
34
25
|
|
35
|
-
def complete
|
26
|
+
def complete!
|
36
27
|
ActiveRecord::Base.transaction do
|
37
28
|
run_callbacks :complete do
|
38
|
-
|
29
|
+
self.attributes = current_attributes
|
30
|
+
|
31
|
+
order_items.each(&:complete)
|
32
|
+
save!
|
33
|
+
|
34
|
+
payment.next_state! if payment
|
35
|
+
shipment.next_state! if shipment
|
36
|
+
|
37
|
+
touch(:completed_at)
|
39
38
|
end
|
40
39
|
end
|
41
40
|
end
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
fail Comable::InvalidOrder unless complete
|
46
|
-
self
|
47
|
-
end
|
42
|
+
alias_method :complete, :complete!
|
43
|
+
deprecate :complete, deprecator: Comable::Deprecator.instance
|
48
44
|
|
49
|
-
def
|
50
|
-
|
45
|
+
def restock!
|
46
|
+
order_items.each(&:restock)
|
47
|
+
save!
|
51
48
|
end
|
52
49
|
|
53
|
-
|
54
|
-
|
55
|
-
|
50
|
+
def unstock!
|
51
|
+
order_items.each(&:unstock)
|
52
|
+
save!
|
56
53
|
end
|
57
54
|
|
58
55
|
def stocked_items
|
59
|
-
|
56
|
+
order_items.to_a.select(&:unstocked?)
|
60
57
|
end
|
61
58
|
|
62
|
-
alias_method :soldout_stocks, :stocked_items
|
63
|
-
deprecate :soldout_stocks, deprecator: Comable::Deprecator.instance
|
64
|
-
|
65
59
|
# 時価商品合計を取得
|
66
60
|
def current_item_total_price
|
67
|
-
|
61
|
+
order_items.to_a.sum(&:current_subtotal_price)
|
68
62
|
end
|
69
63
|
|
70
64
|
# 売価商品合計を取得
|
71
65
|
def item_total_price
|
72
|
-
|
66
|
+
order_items.to_a.sum(&:subtotal_price)
|
73
67
|
end
|
74
68
|
|
75
69
|
# 時価送料を取得
|
76
70
|
def current_shipment_fee
|
77
|
-
|
71
|
+
shipment.try(:fee) || 0
|
72
|
+
end
|
73
|
+
|
74
|
+
# Get the current payment fee
|
75
|
+
def current_payment_fee
|
76
|
+
payment.try(:fee) || 0
|
78
77
|
end
|
79
78
|
|
80
79
|
# 時価合計を取得
|
81
80
|
def current_total_price
|
82
|
-
current_item_total_price + current_shipment_fee
|
81
|
+
current_item_total_price + current_payment_fee + current_shipment_fee
|
83
82
|
end
|
84
83
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
self.
|
89
|
-
self.
|
90
|
-
self.
|
91
|
-
generate_code
|
84
|
+
# Inherit from other Order
|
85
|
+
def inherit!(order)
|
86
|
+
self.bill_address ||= order.bill_address
|
87
|
+
self.ship_address ||= order.ship_address
|
88
|
+
self.payment ||= order.payment
|
89
|
+
self.shipment ||= order.shipment
|
92
90
|
|
93
|
-
|
94
|
-
|
95
|
-
save
|
91
|
+
stated?(order.state) ? save! : next_state!
|
96
92
|
end
|
97
93
|
|
98
|
-
def
|
99
|
-
|
100
|
-
random_token = "C#{Array.new(11) { rand(9) }.join}"
|
101
|
-
break random_token unless self.class.exists?(code: random_token)
|
102
|
-
end
|
94
|
+
def paid?
|
95
|
+
payment ? payment.completed? : true
|
103
96
|
end
|
104
97
|
|
105
|
-
def
|
106
|
-
|
107
|
-
self.guest_token ||= loop do
|
108
|
-
random_token = SecureRandom.urlsafe_base64(nil, false)
|
109
|
-
break random_token unless self.class.exists?(guest_token: random_token)
|
110
|
-
end
|
98
|
+
def shipped?
|
99
|
+
shipment ? shipment.completed? : true
|
111
100
|
end
|
112
101
|
|
113
|
-
def
|
114
|
-
|
115
|
-
self.bill_address ||= customer.bill_address.try(:clone)
|
116
|
-
self.ship_address ||= customer.ship_address.try(:clone)
|
102
|
+
def can_ship?
|
103
|
+
shipment && shipment.ready? && paid? && completed?
|
117
104
|
end
|
118
105
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
106
|
+
private
|
107
|
+
|
108
|
+
def current_attributes
|
109
|
+
{
|
110
|
+
payment_fee: current_payment_fee,
|
111
|
+
shipment_fee: current_shipment_fee,
|
112
|
+
total_price: current_total_price
|
113
|
+
}
|
123
114
|
end
|
124
115
|
end
|
125
116
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Comable
|
2
|
+
class OrderItem < ActiveRecord::Base
|
3
|
+
module Csvable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
comma do
|
8
|
+
__association__ order: :code
|
9
|
+
__association__ order: :email
|
10
|
+
__association__ order: :payment_fee
|
11
|
+
__association__ order: :shipment_fee
|
12
|
+
__association__ order: :total_price
|
13
|
+
__association__ order: { bill_address: :family_name }
|
14
|
+
__association__ order: { bill_address: :first_name }
|
15
|
+
__association__ order: { bill_address: :zip_code }
|
16
|
+
__association__ order: { bill_address: :state_name }
|
17
|
+
__association__ order: { bill_address: :city }
|
18
|
+
__association__ order: { bill_address: :detail }
|
19
|
+
__association__ order: { bill_address: :phone_number }
|
20
|
+
name
|
21
|
+
code
|
22
|
+
price
|
23
|
+
sku_h_item_name
|
24
|
+
sku_v_item_name
|
25
|
+
sku_h_choice_name
|
26
|
+
sku_v_choice_name
|
27
|
+
quantity
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,29 +1,36 @@
|
|
1
1
|
module Comable
|
2
|
-
class
|
2
|
+
class OrderItem < ActiveRecord::Base
|
3
3
|
include Comable::SkuItem
|
4
4
|
include Comable::SkuChoice
|
5
|
+
include Comable::OrderItem::Csvable
|
5
6
|
|
6
7
|
belongs_to :stock, class_name: Comable::Stock.name, autosave: true
|
7
|
-
belongs_to :order, class_name: Comable::Order.name, inverse_of: :
|
8
|
+
belongs_to :order, class_name: Comable::Order.name, inverse_of: :order_items
|
8
9
|
|
9
10
|
validates :quantity, numericality: { greater_than: 0 }
|
10
11
|
validate :valid_stock_quantity
|
11
12
|
|
13
|
+
liquid_methods :name, :name_with_sku, :code, :quantity, :price, :subtotal_price
|
14
|
+
|
12
15
|
delegate :product, to: :stock
|
13
16
|
delegate :image_url, to: :product
|
14
17
|
delegate :guest_token, to: :order
|
15
18
|
delegate :completed?, to: :order, allow_nil: true
|
16
|
-
delegate :completing?, to: :order, allow_nil: true
|
17
19
|
|
18
|
-
|
19
|
-
incomplete.before_validation :copy_attributes
|
20
|
-
end
|
20
|
+
before_validation :copy_attributes, unless: :completed?
|
21
21
|
|
22
22
|
def complete
|
23
|
-
|
23
|
+
unstock
|
24
|
+
end
|
25
|
+
|
26
|
+
def unstock
|
24
27
|
decrement_stock
|
25
28
|
end
|
26
29
|
|
30
|
+
def restock
|
31
|
+
increment_stock
|
32
|
+
end
|
33
|
+
|
27
34
|
# TODO: カート投入時との差額表示
|
28
35
|
def copy_attributes
|
29
36
|
self.attributes = current_attributes
|
@@ -50,9 +57,6 @@ module Comable
|
|
50
57
|
end
|
51
58
|
end
|
52
59
|
|
53
|
-
alias_method :soldout_stock?, :unstocked?
|
54
|
-
deprecate :soldout_stock?, deprecator: Comable::Deprecator.instance
|
55
|
-
|
56
60
|
private
|
57
61
|
|
58
62
|
def valid_stock_quantity
|
@@ -73,6 +77,11 @@ module Comable
|
|
73
77
|
stock.quantity -= quantity
|
74
78
|
end
|
75
79
|
|
80
|
+
def increment_stock
|
81
|
+
stock.lock!
|
82
|
+
stock.quantity += quantity
|
83
|
+
end
|
84
|
+
|
76
85
|
def current_attributes
|
77
86
|
{
|
78
87
|
name: product.name,
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Comable
|
2
|
+
class Payment < ActiveRecord::Base
|
3
|
+
include Comable::Ransackable
|
4
|
+
|
5
|
+
belongs_to :order, class_name: Comable::Order.name, inverse_of: :payment
|
6
|
+
belongs_to :payment_method, class_name: Comable::PaymentMethod.name
|
7
|
+
|
8
|
+
before_validation :copy_attributes_from_payment_method, unless: :order_completed?
|
9
|
+
|
10
|
+
validates :order, presence: true
|
11
|
+
validates :payment_method, presence: true
|
12
|
+
validates :fee, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
13
|
+
|
14
|
+
delegate :name, to: :payment_method
|
15
|
+
delegate :payment_provider, to: :payment_method
|
16
|
+
delegate :authorize!, :complete!, :cancel!, :resume!, to: :payment_provider, prefix: :provider
|
17
|
+
|
18
|
+
ransack_options ransackable_attributes: { except: [:order_id, :payment_method_id] }
|
19
|
+
|
20
|
+
# The #state attribute assigns the following values:
|
21
|
+
#
|
22
|
+
# pending when Order is not able to pay (default)
|
23
|
+
# ready when Order is able to pay
|
24
|
+
# completed when Order is already paid
|
25
|
+
# canceled when Order is canceled
|
26
|
+
# resumed when Order is resumed from the "canceled" state
|
27
|
+
state_machine initial: :pending do
|
28
|
+
state :pending
|
29
|
+
state :ready
|
30
|
+
state :completed
|
31
|
+
state :canceled
|
32
|
+
state :resumed
|
33
|
+
|
34
|
+
event :next_state do
|
35
|
+
transition :pending => :ready
|
36
|
+
transition :ready => :completed
|
37
|
+
end
|
38
|
+
|
39
|
+
event :cancel do
|
40
|
+
transition [:completed, :resumed] => :canceled
|
41
|
+
end
|
42
|
+
|
43
|
+
event :resume do
|
44
|
+
transition :canceled => :resumed
|
45
|
+
end
|
46
|
+
|
47
|
+
after_transition to: :ready, do: :next_state!
|
48
|
+
before_transition to: :ready, do: -> (s) { s.provider_authorize! }
|
49
|
+
before_transition to: :completed, do: -> (s) { s.provider_complete! }
|
50
|
+
before_transition to: :canceled, do: -> (s) { s.provider_cancel! }
|
51
|
+
before_transition to: :canceled, do: -> (s) { s.provider_resume! }
|
52
|
+
end
|
53
|
+
|
54
|
+
class << self
|
55
|
+
def state_names
|
56
|
+
state_machine.states.keys
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def stated?(target_state)
|
61
|
+
target_state_index = self.class.state_names.index(target_state.to_sym)
|
62
|
+
current_state_index = self.class.state_names.index(state_name)
|
63
|
+
target_state_index < current_state_index
|
64
|
+
end
|
65
|
+
|
66
|
+
def completed?
|
67
|
+
state?(:completed) || state?(:resumed)
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def order_completed?
|
73
|
+
order.completed?
|
74
|
+
end
|
75
|
+
|
76
|
+
def copy_attributes_from_payment_method
|
77
|
+
self.attributes = {
|
78
|
+
fee: payment_method.fee
|
79
|
+
}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -3,6 +3,7 @@ module Comable
|
|
3
3
|
validates :name, presence: true, length: { maximum: 255 }
|
4
4
|
validates :payment_provider_type, presence: true, length: { maximum: 255 }
|
5
5
|
validates :payment_provider_kind, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
6
|
+
validates :fee, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
6
7
|
validates :enable_price_from, numericality: { greater_than_or_equal_to: 0, allow_blank: true }
|
7
8
|
validates :enable_price_to, numericality: { greater_than_or_equal_to: 0, allow_blank: true }
|
8
9
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Comable
|
2
|
+
class Product < ActiveRecord::Base
|
3
|
+
module Csvable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
include Comable::Importable
|
7
|
+
|
8
|
+
included do
|
9
|
+
comma do
|
10
|
+
name
|
11
|
+
code
|
12
|
+
price
|
13
|
+
caption
|
14
|
+
sku_h_item_name
|
15
|
+
sku_v_item_name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module Comable
|
2
2
|
class Product < ActiveRecord::Base
|
3
3
|
include Comable::SkuItem
|
4
|
-
include Comable::Product::Search
|
5
4
|
include Comable::Ransackable
|
5
|
+
include Comable::Product::Search
|
6
|
+
include Comable::Product::Csvable
|
6
7
|
|
7
8
|
has_many :stocks, class_name: Comable::Stock.name, dependent: :destroy
|
8
9
|
has_many :images, class_name: Comable::Image.name, dependent: :destroy
|
@@ -34,16 +35,10 @@ module Comable
|
|
34
35
|
stocks.stocked.exists?
|
35
36
|
end
|
36
37
|
|
37
|
-
alias_method :unsold?, :stocked?
|
38
|
-
deprecate :unsold?, deprecator: Comable::Deprecator.instance
|
39
|
-
|
40
38
|
def unstocked?
|
41
39
|
!stocked?
|
42
40
|
end
|
43
41
|
|
44
|
-
alias_method :soldout?, :unstocked?
|
45
|
-
deprecate :soldout?, deprecator: Comable::Deprecator.instance
|
46
|
-
|
47
42
|
def category_path_names=(category_path_names, delimiter: Comable::Category::DEFAULT_PATH_NAME_DELIMITER)
|
48
43
|
self.categories = Comable::Category.find_by_path_names(category_path_names, delimiter: delimiter)
|
49
44
|
end
|