comable 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cdfd44cf66aa3b1b326e80bef01bdde6f1c0447e
4
- data.tar.gz: ef5123467b8427a8ca186b342ae58449fe0b62e0
3
+ metadata.gz: 56d44aba496e45370483a0b7a625fcf96f4dabf2
4
+ data.tar.gz: 03b299ed0c7bcfb019964d1da1758175951052d0
5
5
  SHA512:
6
- metadata.gz: 91ecb5a8436788eab87d6443b4238cd5473ef327c1a77eb8bc40990b589f0c3c3ff4bca20b96a6895d092ffb6c9ebc2e60c45a3d7681a222e4ea99e0686282d2
7
- data.tar.gz: 1b987222eab78a0c8f796b71fdc44cd6327b3fde26f7dec98e249e5162afa53b2e1f33e80a0c1bd6d561b24105d713bcdcea8dd2362b3d7e0e6323af377700c9
6
+ metadata.gz: d97538ce5e3a49d8c47b6f98da650be04606d4e51b535fc610ef9b9c334740bd17d9c6c66e4e2931c007851a27ce20eaa0b5bf2d0b718dd649e7bdfb6d326826
7
+ data.tar.gz: fd264578ddb1caffef0f74ece4c72d08cd73092250b833101f9708ab34687e489d487c8cf8e5757f0f91183d1316aef9cd9c4d2bd787e6b0a498ef1b0d69e74c
@@ -13,12 +13,23 @@ module Comable
13
13
  end
14
14
 
15
15
  # TODO: 在庫確認
16
- current_customer.add_cart_item(stock || product)
16
+ current_customer.add_cart_item(stock || product, quantity: params[:quantity].to_i)
17
17
 
18
18
  flash[:notice] = I18n.t('comable.carts.add_product')
19
19
  redirect_to cart_path
20
20
  end
21
21
 
22
+ def update
23
+ stock = Comable::Stock.where(id: params[:stock_id]).first
24
+ return redirect_by_product_not_found unless stock
25
+
26
+ # TODO: 在庫確認
27
+ current_customer.reset_cart_item(stock, quantity: params[:quantity].to_i)
28
+
29
+ flash[:notice] = I18n.t('comable.carts.update')
30
+ redirect_to cart_path
31
+ end
32
+
22
33
  private
23
34
 
24
35
  def redirect_by_product_not_found
@@ -1,7 +1,5 @@
1
1
  module Comable
2
2
  class CartItem < ActiveRecord::Base
3
- utusemi!
4
-
5
3
  belongs_to :customer, class_name: Comable::Customer.name, foreign_key: Comable::Customer.table_name.singularize.foreign_key
6
4
  belongs_to :stock, class_name: Comable::Stock.name, foreign_key: Comable::Stock.table_name.singularize.foreign_key
7
5
 
@@ -24,25 +24,21 @@ module Comable
24
24
  !logged_in?
25
25
  end
26
26
 
27
- def add_cart_item(obj)
28
- case obj
29
- when Comable::Product
30
- add_stock_to_cart(obj.stocks.first)
31
- when Comable::Stock
32
- add_stock_to_cart(obj)
33
- when Array
34
- obj.map { |item| add_cart_item(item) }
35
- else
36
- fail
27
+ def add_cart_item(obj, quantity: 1)
28
+ process_cart_item(obj) do |stock|
29
+ add_stock_to_cart(stock, quantity)
37
30
  end
38
31
  end
39
32
 
40
- def remove_cart_item(obj)
41
- case obj
42
- when Comable::Stock
43
- remove_stock_from_cart(obj)
44
- else
45
- fail
33
+ def remove_cart_item(obj, quantity: -1)
34
+ process_cart_item(obj) do |stock|
35
+ add_stock_to_cart(stock, quantity)
36
+ end
37
+ end
38
+
39
+ def reset_cart_item(obj, quantity: 0)
40
+ process_cart_item(obj) do |stock|
41
+ reset_stock_from_cart(stock, quantity)
46
42
  end
47
43
  end
48
44
 
@@ -83,26 +79,42 @@ module Comable
83
79
  @cookies.signed[:guest_token]
84
80
  end
85
81
 
86
- def add_stock_to_cart(stock)
82
+ def process_cart_item(obj)
83
+ case obj
84
+ when Comable::Product
85
+ yield obj.stocks.first
86
+ when Comable::Stock
87
+ yield obj
88
+ when Array
89
+ obj.map { |item| yield item }
90
+ else
91
+ fail
92
+ end
93
+ end
94
+
95
+ def add_stock_to_cart(stock, quantity)
87
96
  fail I18n.t('comable.carts.product_not_stocked') if stock.soldout?
88
97
 
89
98
  cart_items = find_cart_items_by(stock)
90
99
  if cart_items.any?
91
100
  cart_item = cart_items.first
92
- cart_item.increment!(:quantity)
101
+ cart_item.quantity += quantity
102
+ (cart_item.quantity > 0) ? cart_item.save : cart_item.destroy
93
103
  else
94
- cart_item = cart_items.create
104
+ cart_item = cart_items.create(quantity: quantity)
95
105
  @cookies.permanent.signed[:guest_token] = cart_item.guest_token if not_logged_in?
96
106
  end
97
107
  end
98
108
 
99
- def remove_stock_from_cart(stock)
100
- cart_item = find_cart_items_by(stock).first
101
- return false unless cart_item
102
-
103
- if cart_item.quantity.pred.nonzero?
104
- cart_item.decrement!(:quantity)
109
+ def reset_stock_from_cart(stock, quantity)
110
+ cart_items = find_cart_items_by(stock)
111
+ if quantity > 0
112
+ return add_stock_to_cart(stock, quantity) if cart_items.empty?
113
+ cart_item = cart_items.first
114
+ cart_item.quantity = quantity
115
+ cart_item.save
105
116
  else
117
+ return false if cart_items.empty?
106
118
  cart_item.destroy
107
119
  end
108
120
  end
@@ -2,22 +2,26 @@ h1 カート
2
2
 
3
3
  ul.cart
4
4
  - current_customer.cart.each do |cart_item|
5
- - stock = cart_item.stock
6
- - product = stock.product
7
- li.product
8
- h2.name
9
- - if stock.sku?
10
- - sku_name = stock.sku_h_choice_name
11
- - sku_name += '/' + stock.sku_v_choice_name if stock.sku_v_choice_name.present?
12
- = link_to product.name + "(#{sku_name})", comable.product_path(product)
13
- - else
14
- = link_to product.name, comable.product_path(product)
15
- .caption
16
- = product.caption
17
- .price
18
- = number_to_currency product.price
19
- .quantity
20
- = number_with_delimiter cart_item.quantity
5
+ = form_tag comable.cart_path, method: :put do
6
+ - stock = cart_item.stock
7
+ - product = stock.product
8
+ li.product
9
+ h2.name
10
+ - if stock.sku?
11
+ - sku_name = stock.sku_h_choice_name
12
+ - sku_name += '/' + stock.sku_v_choice_name if stock.sku_v_choice_name.present?
13
+ = link_to product.name + "(#{sku_name})", comable.product_path(product)
14
+ - else
15
+ = link_to product.name, comable.product_path(product)
16
+ .caption
17
+ = product.caption
18
+ .price
19
+ = number_to_currency product.price
20
+ .quantity
21
+ - selected = cart_item.quantity
22
+ = select_tag :quantity, options_for_select(1.upto([10, selected].max).to_a, selected)
23
+ = hidden_field_tag :stock_id, stock.id
24
+ = submit_tag '変更'
21
25
 
22
26
  .order
23
27
  = link_to '注文', comable.new_order_path
@@ -14,6 +14,7 @@
14
14
  - if @product.unsold?
15
15
  .add_cart
16
16
  = hidden_field_tag :product_id, @product.id
17
+ = select_tag :quantity, options_for_select(1.upto(10).to_a)
17
18
  = submit_tag 'カートに入れる'
18
19
  - else
19
20
  .sold_out
@@ -5,6 +5,7 @@ ja:
5
5
  product_not_found: 'ご指定の商品は見つかりませんでした'
6
6
  product_not_stocked: 'ご指定の商品は在庫がありません'
7
7
  empty: 'カートに商品が入っていません'
8
+ update: 'カートの内容が変更されました'
8
9
  orders:
9
10
  success: '注文が完了しました'
10
11
  failure: '注文に失敗しました。入力項目を見直してください。'
@@ -1,5 +1,4 @@
1
1
  require 'slim'
2
- require 'utusemi'
3
2
 
4
3
  module Comable
5
4
  class Engine < ::Rails::Engine
@@ -1,3 +1,3 @@
1
1
  module Comable
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-08-04 00:00:00.000000000 Z
11
+ date: 2014-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails