comable_core 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b77ac8195add8320091e5c17e8b8eb99d3a0ef5f
4
- data.tar.gz: 9b4e674341684b9d4d6807aa9283a4eee1c40594
3
+ metadata.gz: da4f11f29114534f3c55966b7021770844817bc2
4
+ data.tar.gz: 21a2c7ab417d1401a7b79b6ed456e22f40ce0395
5
5
  SHA512:
6
- metadata.gz: ea16779b2ba3b37d19946de36f67bdbf846914750d4e006ebf86e7483a4c415c7163854e66aa4b1d8f1b7e056d285648bd529d13d0d1000dfb545490b5f19a1d
7
- data.tar.gz: 42fba0d835446368be5aec1f6deaf0a316c763751c43998035648fc64ad15e565ac27fa6d9f6f403e04243788cdef7ed43c8c3a91aa406bb791606d9746c21f7
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
- enumerize :assign_key, in: {
8
- nothing: nil,
9
- bill: 1,
10
- ship: 2
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
- has_many :comable_addresses, class_name: Comable::Address.name, foreign_key: table_name.singularize.foreign_key
8
- alias_method :addresses, :comable_addresses
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
- return if signed_in?
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(family_name: family_name, first_name: first_name, email: email, order_deliveries_attributes: [{ family_name: family_name, first_name: first_name }])
77
- @cookies.permanent.signed[:guest_token] = order.guest_token if not_signed_in?
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
- Comable::Customer.table_name.singularize.foreign_key => self,
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
- # Rails 3.x だと has_many 先のインスタンスが追加されても
93
- # 親インスタンスが感知できないので、いちいちリロードするように変更
94
- if Rails::VERSION::MAJOR == 3
95
- def add_stock_to_cart_with_reload(*args)
96
- add_stock_to_cart_without_reload(*args).tap { @incomplete_order = nil }
97
- end
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
- alias_method_chain :reset_stock_from_cart, :reload
132
+ # TODO: Remove?
133
+ cart_items.reload
104
134
  end
105
135
  end
106
136
  end
@@ -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 if: :incomplete? do |incomplete_order|
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
- before_create :generate_guest_token
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 send(Comable::Customer.table_name.singularize.foreign_key)
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
@@ -31,6 +31,10 @@ class CreateComableCustomers < ActiveRecord::Migration
31
31
  # t.datetime :locked_at
32
32
 
33
33
  ## Others
34
+ t.references :bill_address
35
+ t.references :ship_address
36
+
37
+ # TODO: Remove
34
38
  t.string :family_name
35
39
  t.string :first_name
36
40
  end
@@ -12,6 +12,8 @@ class CreateComableOrders < ActiveRecord::Migration
12
12
  t.string :shipment_tracking_number
13
13
  t.integer :shipment_method_id
14
14
  t.integer :total_price
15
+ t.references :bill_address
16
+ t.references :ship_address
15
17
  t.datetime :completed_at
16
18
  end
17
19
 
@@ -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, null: false
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
@@ -16,6 +16,10 @@ module Comable
16
16
  end
17
17
  end
18
18
 
19
+ def move_cart_item(cart_item)
20
+ add_cart_item(cart_item.stock, quantity: cart_item.quantity) && cart_item.destroy
21
+ end
22
+
19
23
  def cart_items
20
24
  fail 'You should implement cart_items method.'
21
25
  end
@@ -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} ||= devise_current_#{mapping} || Comable::Customer.new(cookies)
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.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-04 00:00:00.000000000 Z
11
+ date: 2014-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails