comable_core 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/comable/address.rb +25 -7
- data/app/models/comable/customer.rb +51 -21
- data/app/models/comable/order.rb +24 -3
- data/db/migrate/20140120032559_create_comable_customers.rb +4 -0
- data/db/migrate/20140723175431_create_comable_orders.rb +2 -0
- data/db/migrate/20141024025526_create_comable_addresses.rb +1 -4
- data/lib/comable/cart_owner.rb +4 -0
- data/lib/comable/core/engine.rb +3 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da4f11f29114534f3c55966b7021770844817bc2
|
4
|
+
data.tar.gz: 21a2c7ab417d1401a7b79b6ed456e22f40ce0395
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fa65b0505fd616f97943e355e63806c077046c614516adb4f8b5dd10d122bc7fd79221006b8705b782166b469cdce63a1dba780f1ef00f0c4115da7690a9366
|
7
|
+
data.tar.gz: 6f03560e17a436549e97d6993895e87e8c04a8eed78dd2e9fc50ef16a1181241f3553bdb091e0001b9a1ac412f66ba041df9fd5b24c99073909f0be41defa51f
|
@@ -1,13 +1,31 @@
|
|
1
1
|
module Comable
|
2
2
|
class Address < ActiveRecord::Base
|
3
|
-
extend Enumerize
|
4
|
-
|
5
3
|
belongs_to :customer, class_name: Comable::Customer.name, foreign_key: Comable::Customer.table_name.singularize.foreign_key, autosave: false
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
}
|
5
|
+
validates :family_name, presence: true, length: { maximum: 255 }
|
6
|
+
validates :first_name, presence: true, length: { maximum: 255 }
|
7
|
+
validates :zip_code, presence: true, length: { maximum: 8 }
|
8
|
+
validates :state_name, presence: true, length: { maximum: 255 }
|
9
|
+
validates :city, presence: true, length: { maximum: 255 }
|
10
|
+
validates :detail, length: { maximum: 255 }
|
11
|
+
validates :phone_number, length: { maximum: 18 }
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def find_or_clone(address)
|
15
|
+
find { |obj| obj.same_as? address } || address.clone
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def same_as?(address)
|
20
|
+
attributes_without_id == address.attributes_without_id
|
21
|
+
end
|
22
|
+
|
23
|
+
def clone
|
24
|
+
self.class.new(attributes_without_id)
|
25
|
+
end
|
26
|
+
|
27
|
+
def attributes_without_id
|
28
|
+
attributes.except('id', Comable::Customer.table_name.singularize.foreign_key.to_s)
|
29
|
+
end
|
12
30
|
end
|
13
31
|
end
|
@@ -3,16 +3,23 @@ module Comable
|
|
3
3
|
include CartOwner
|
4
4
|
|
5
5
|
has_many :orders, class_name: Comable::Order.name, foreign_key: table_name.singularize.foreign_key
|
6
|
+
has_many :addresses, class_name: Comable::Address.name, foreign_key: table_name.singularize.foreign_key, dependent: :destroy
|
7
|
+
belongs_to :bill_address, class_name: Comable::Address.name, dependent: :destroy
|
8
|
+
belongs_to :ship_address, class_name: Comable::Address.name, dependent: :destroy
|
6
9
|
|
7
|
-
|
8
|
-
|
10
|
+
accepts_nested_attributes_for :addresses
|
11
|
+
accepts_nested_attributes_for :bill_address
|
12
|
+
accepts_nested_attributes_for :ship_address
|
9
13
|
|
10
14
|
devise(*Comable::Config.devise_strategies[:customer])
|
11
15
|
|
16
|
+
before_save :inherit_cart_items, if: :current_sign_in_at_changed?
|
17
|
+
|
12
18
|
def initialize(*args)
|
13
19
|
obj = args.first
|
14
20
|
case obj.class.name
|
15
21
|
when /Cookies/
|
22
|
+
Rails.logger.debug '[DEPRECATED] Comable::Customer#new(cookies) is deprecated. Please use Comable::Customer#with_cookies(cookies) method.'
|
16
23
|
@cookies = obj
|
17
24
|
super()
|
18
25
|
else
|
@@ -20,12 +27,29 @@ module Comable
|
|
20
27
|
end
|
21
28
|
end
|
22
29
|
|
30
|
+
def with_cookies(cookies)
|
31
|
+
@cookies = cookies
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
23
35
|
# Add conditions for the orders association.
|
24
36
|
# Override method of the orders association to support Rails 3.x.
|
25
37
|
def orders
|
26
38
|
super.complete
|
27
39
|
end
|
28
40
|
|
41
|
+
def other_addresses
|
42
|
+
addresses - [bill_address] - [ship_address]
|
43
|
+
end
|
44
|
+
|
45
|
+
def update_bill_address_by(bill_address)
|
46
|
+
update_attributes(bill_address: addresses.find_or_clone(bill_address))
|
47
|
+
end
|
48
|
+
|
49
|
+
def update_ship_address_by(ship_address)
|
50
|
+
update_attributes(ship_address: addresses.find_or_clone(ship_address))
|
51
|
+
end
|
52
|
+
|
29
53
|
def signed_in?
|
30
54
|
!new_record?
|
31
55
|
end
|
@@ -66,41 +90,47 @@ module Comable
|
|
66
90
|
private
|
67
91
|
|
68
92
|
def current_guest_token
|
69
|
-
|
70
|
-
@cookies.signed[:guest_token]
|
93
|
+
@cookies.signed[:guest_token] if @cookies
|
71
94
|
end
|
72
95
|
|
73
96
|
def initialize_incomplete_order
|
74
97
|
orders = find_incomplete_orders
|
75
98
|
return orders.first if orders.any?
|
76
|
-
order = orders.create(
|
77
|
-
@cookies.permanent.signed[:guest_token] = order.guest_token if
|
99
|
+
order = orders.create(incomplete_order_attributes)
|
100
|
+
@cookies.permanent.signed[:guest_token] = order.guest_token if @cookies
|
78
101
|
order
|
79
102
|
end
|
80
103
|
|
104
|
+
def incomplete_order_attributes
|
105
|
+
{
|
106
|
+
self.class.table_name.singularize.foreign_key => id,
|
107
|
+
email: email,
|
108
|
+
# TODO: Remove
|
109
|
+
family_name: family_name,
|
110
|
+
first_name: first_name,
|
111
|
+
order_deliveries_attributes: [{ family_name: family_name, first_name: first_name }]
|
112
|
+
}
|
113
|
+
end
|
114
|
+
|
81
115
|
def find_incomplete_orders
|
116
|
+
guest_token = current_guest_token unless signed_in?
|
82
117
|
Comable::Order
|
83
118
|
.incomplete
|
84
119
|
.includes(order_deliveries: :order_details)
|
85
|
-
.where(
|
86
|
-
|
87
|
-
:guest_token => current_guest_token
|
88
|
-
)
|
120
|
+
.where(guest_token: guest_token)
|
121
|
+
.by_customer(self)
|
89
122
|
.limit(1)
|
90
123
|
end
|
91
124
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
alias_method_chain :add_stock_to_cart, :reload
|
99
|
-
|
100
|
-
def reset_stock_from_cart_with_reload(*args)
|
101
|
-
reset_stock_from_cart_without_reload(*args).tap { @incomplete_order = nil }
|
125
|
+
def inherit_cart_items
|
126
|
+
return unless current_guest_token
|
127
|
+
guest_order = Comable::Order.incomplete.includes(order_deliveries: :order_details).where(guest_token: current_guest_token).first
|
128
|
+
return unless guest_order
|
129
|
+
guest_order.order_deliveries.map(&:order_details).flatten.each do |order_detail|
|
130
|
+
move_cart_item(order_detail)
|
102
131
|
end
|
103
|
-
|
132
|
+
# TODO: Remove?
|
133
|
+
cart_items.reload
|
104
134
|
end
|
105
135
|
end
|
106
136
|
end
|
data/app/models/comable/order.rb
CHANGED
@@ -3,8 +3,13 @@ module Comable
|
|
3
3
|
belongs_to :customer, class_name: Comable::Customer.name, foreign_key: Comable::Customer.table_name.singularize.foreign_key, autosave: false
|
4
4
|
belongs_to :payment, class_name: Comable::Payment.name, foreign_key: Comable::Payment.table_name.singularize.foreign_key, autosave: false
|
5
5
|
belongs_to :shipment_method, class_name: Comable::ShipmentMethod.name, autosave: false
|
6
|
+
belongs_to :bill_address, class_name: Comable::Address.name, autosave: true, dependent: :destroy
|
7
|
+
belongs_to :ship_address, class_name: Comable::Address.name, autosave: true, dependent: :destroy
|
6
8
|
has_many :order_deliveries, dependent: :destroy, class_name: Comable::OrderDelivery.name, foreign_key: table_name.singularize.foreign_key, inverse_of: :order
|
7
9
|
|
10
|
+
accepts_nested_attributes_for :bill_address
|
11
|
+
accepts_nested_attributes_for :ship_address
|
12
|
+
# TODO: Remove
|
8
13
|
accepts_nested_attributes_for :order_deliveries
|
9
14
|
|
10
15
|
with_options if: :complete? do |complete_order|
|
@@ -16,17 +21,20 @@ module Comable
|
|
16
21
|
complete_order.validates :total_price, presence: true
|
17
22
|
end
|
18
23
|
|
19
|
-
with_options
|
24
|
+
with_options unless: :complete? do |incomplete_order|
|
20
25
|
incomplete_order.validates Comable::Customer.table_name.singularize.foreign_key, uniqueness: { scope: [Comable::Customer.table_name.singularize.foreign_key, :completed_at] }, if: :customer
|
21
26
|
incomplete_order.validates :guest_token, uniqueness: { scope: [:guest_token, :completed_at] }, if: :guest_token
|
22
27
|
end
|
23
28
|
|
24
29
|
define_model_callbacks :complete
|
25
30
|
before_complete :precomplete
|
26
|
-
|
31
|
+
before_validation :generate_guest_token, on: :create
|
32
|
+
before_validation :clone_addresses_from_customer, on: :create
|
33
|
+
after_complete :clone_addresses_to_customer
|
27
34
|
|
28
35
|
scope :complete, -> { where.not(completed_at: nil) }
|
29
36
|
scope :incomplete, -> { where(completed_at: nil) }
|
37
|
+
scope :by_customer, -> (customer) { where(Comable::Customer.table_name.singularize.foreign_key => customer) }
|
30
38
|
|
31
39
|
def precomplete
|
32
40
|
valid_order_quantity?
|
@@ -105,11 +113,24 @@ module Comable
|
|
105
113
|
end
|
106
114
|
|
107
115
|
def generate_guest_token
|
108
|
-
return if
|
116
|
+
return if customer
|
109
117
|
self.guest_token ||= loop do
|
110
118
|
random_token = SecureRandom.urlsafe_base64(nil, false)
|
111
119
|
break random_token unless self.class.exists?(guest_token: random_token)
|
112
120
|
end
|
113
121
|
end
|
122
|
+
|
123
|
+
def clone_addresses_from_customer
|
124
|
+
return unless customer
|
125
|
+
self.bill_address ||= customer.bill_address.try(:clone)
|
126
|
+
self.ship_address ||= customer.ship_address.try(:clone)
|
127
|
+
end
|
128
|
+
|
129
|
+
def clone_addresses_to_customer
|
130
|
+
return unless customer
|
131
|
+
# TODO: Remove conditions for compatibility.
|
132
|
+
customer.update_bill_address_by bill_address if bill_address
|
133
|
+
customer.update_ship_address_by ship_address if ship_address
|
134
|
+
end
|
114
135
|
end
|
115
136
|
end
|
@@ -1,8 +1,7 @@
|
|
1
1
|
class CreateComableAddresses < ActiveRecord::Migration
|
2
2
|
def change
|
3
3
|
create_table :comable_addresses do |t|
|
4
|
-
t.integer :comable_customer_id
|
5
|
-
t.integer :assign_key
|
4
|
+
t.integer :comable_customer_id
|
6
5
|
t.string :family_name, null: false
|
7
6
|
t.string :first_name, null: false
|
8
7
|
t.string :zip_code, null: false, limit: 8
|
@@ -13,7 +12,5 @@ class CreateComableAddresses < ActiveRecord::Migration
|
|
13
12
|
t.string :phone_number, null: false, limit: 18
|
14
13
|
t.datetime :last_used_at
|
15
14
|
end
|
16
|
-
|
17
|
-
add_index :comable_addresses, [:comable_customer_id, :assign_key], unique: true, name: :comable_addresses_idx_01
|
18
15
|
end
|
19
16
|
end
|
data/lib/comable/cart_owner.rb
CHANGED
data/lib/comable/core/engine.rb
CHANGED
@@ -47,7 +47,9 @@ module Comable
|
|
47
47
|
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
48
48
|
alias_method :devise_current_#{mapping}, :current_#{mapping}
|
49
49
|
def current_#{mapping}
|
50
|
-
@current_#{mapping} ||=
|
50
|
+
@current_#{mapping}_guest ||= Comable::Customer.new
|
51
|
+
#{mapping} = devise_current_#{mapping} || @current_#{mapping}_guest
|
52
|
+
#{mapping}.with_cookies(cookies)
|
51
53
|
end
|
52
54
|
METHODS
|
53
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: comable_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- YOSHIDA Hiroki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|