comable_core 0.2.0 → 0.2.1

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: a04e83098af7da04af0085988beee96d9dfaf150
4
- data.tar.gz: 30d646a65f0e590746934f8f1f34707777d54a4f
3
+ metadata.gz: a0847e05a0d1404a70910a95138f0666315b06db
4
+ data.tar.gz: 213b8f3f6e5d9f11779633b3ce76eb97a366075a
5
5
  SHA512:
6
- metadata.gz: 0b51d3919bf4bd8ffc65b79d3c0142214e56ce4e1b55ed5b137b57e07e542a7231bc9ce32b1e71c24c6c301997f55e50e7dea2306ec990be9d9359391d8a593f
7
- data.tar.gz: fe8b54aec6b72c0cd10a76fb86abe592de36b273879d3ffc1642ef9fda419dc25e9aa62c92fef070eb0e1174ef318d3b5edf19df4adfeedfe29dd709c158cead
6
+ metadata.gz: 9fcebb0bbdd4673c92acacaea3fe91ebed3591b10dbc050c86311a0026acfe431460e0a46c9fabec158fc00ea7ef59561d14ee566800d0e8e8d10aea26915483
7
+ data.tar.gz: d2bba80062e20dd7320057eb35e70c4c2e4f609ae522288b5fc43691a621977f2a06039d3d4caaa390f482be08dc530ce947d69fe04dd36ced4d138e8794a6f4
@@ -1,5 +1,6 @@
1
1
  module Comable
2
2
  class OrderMailer < ActionMailer::Base
3
+ include Comable::ApplicationHelper
3
4
  helper Comable::ApplicationHelper
4
5
  helper_method :subject_for
5
6
 
@@ -10,10 +11,6 @@ module Comable
10
11
 
11
12
  private
12
13
 
13
- def current_store
14
- Comable::Store.instance
15
- end
16
-
17
14
  def subject_for(order)
18
15
  [
19
16
  current_store.name,
@@ -90,7 +90,7 @@ module Comable
90
90
  order_deliveries.map(&:order_details).flatten.each do |order_detail|
91
91
  return errors.add :base, "「#{order_detail.stock.name_with_sku}」の注文数が不正です。" if order_detail.quantity <= 0
92
92
  quantity = order_detail.stock.quantity - order_detail.quantity
93
- return errors.add :base, "「#{order_detail.stock.name_with_sku}」の在庫が不足しています。" if quantity < 0
93
+ return errors.add :base, I18n.t('comable.errors.messages.product_soldout', name: order_detail.stock.name_with_sku) if quantity < 0
94
94
  end
95
95
  end
96
96
 
@@ -37,12 +37,13 @@ module Comable
37
37
  # @example
38
38
  # stock.unsold? #=> true
39
39
  #
40
+ # @param quantity [Fixnum] 減算する在庫数を指定する
40
41
  # @return [Boolean] 在庫があれば true を返す
41
42
  # @see #soldout?
42
- def unsold?
43
+ def unsold?(quantity: 1)
43
44
  return false if product_id_num.nil?
44
- return false if quantity.nil?
45
- quantity > 0
45
+ return false if self.quantity.nil?
46
+ (self.quantity - quantity) >= 0
46
47
  end
47
48
 
48
49
  # 在庫の有無を取得する
@@ -50,10 +51,11 @@ module Comable
50
51
  # @example
51
52
  # stock.soldout? #=> false
52
53
  #
54
+ # @param quantity [Fixnum] 減算する在庫数を指定する
53
55
  # @return [Boolean] {#unsold?} の逆。在庫がなければ true を返す
54
56
  # @see #unsold?
55
- def soldout?
56
- !unsold?
57
+ def soldout?(quantity: 1)
58
+ !unsold?(quantity: quantity)
57
59
  end
58
60
 
59
61
  # 在庫減算を行う
@@ -2,7 +2,7 @@ module Comable
2
2
  class Store < ActiveRecord::Base
3
3
  class << self
4
4
  def instance
5
- Comable::Store.first || Comable::Store.new(name: default_store_name)
5
+ first || new(name: default_store_name)
6
6
  end
7
7
 
8
8
  def default_store_name
@@ -3,10 +3,15 @@ ja:
3
3
  honorific: &honorific
4
4
  '%{name} 様'
5
5
 
6
+ errors:
7
+ messages:
8
+ product_soldout: '%{name}は在庫が不足しています。'
9
+ products_soldout: 'ご指定の商品は在庫が不足しています。'
10
+ product_not_found: '%{name}は存在しないか削除された可能性があります。'
11
+ products_not_found: 'ご指定の商品は存在しないか削除された可能性があります。'
12
+
6
13
  carts:
7
- add_product: '1つの商品がカートに入りしました'
8
- product_not_found: 'ご指定の商品は見つかりませんでした'
9
- product_not_stocked: 'ご指定の商品は在庫がありません'
14
+ add_product: '1つの商品がカートに入りました'
10
15
  empty: 'カートに商品が入っていません'
11
16
  update: 'カートの内容が変更されました'
12
17
  orders:
@@ -46,25 +46,25 @@ module Comable
46
46
  end
47
47
 
48
48
  def add_stock_to_cart(stock, quantity)
49
- fail I18n.t('comable.carts.product_not_stocked') if stock.soldout?
50
-
51
49
  cart_items = find_cart_items_by(stock)
52
50
  if cart_items.any?
53
51
  cart_item = cart_items.first
52
+ fail Comable::NoStock if stock.soldout?(quantity: quantity + cart_item.quantity)
54
53
  cart_item.quantity += quantity
55
54
  (cart_item.quantity > 0) ? cart_item.save : cart_item.destroy
56
55
  else
56
+ fail Comable::NoStock if stock.soldout?(quantity: quantity)
57
57
  cart_items.create(quantity: quantity)
58
58
  end
59
59
  end
60
60
 
61
61
  def reset_stock_from_cart(stock, quantity)
62
+ fail Comable::NoStock if stock.soldout?(quantity: quantity)
63
+
62
64
  cart_items = find_cart_items_by(stock)
63
65
  if quantity > 0
64
66
  return add_stock_to_cart(stock, quantity) if cart_items.empty?
65
- cart_item = cart_items.first
66
- cart_item.quantity = quantity
67
- cart_item.save
67
+ cart_items.first.update_attributes(quantity: quantity)
68
68
  else
69
69
  return false if cart_items.empty?
70
70
  cart_items.first.destroy
@@ -72,7 +72,8 @@ module Comable
72
72
  end
73
73
 
74
74
  def find_cart_items_by(stock)
75
- fail I18n.t('comable.carts.product_not_found') unless stock.is_a?(Comable::Stock)
75
+ # TODO: Refactoring
76
+ fail unless stock.is_a?(Comable::Stock)
76
77
  cart_items.where(Comable::Stock.table_name.singularize.foreign_key => stock.id)
77
78
  end
78
79
  end
@@ -1,4 +1,7 @@
1
1
  module Comable
2
2
  class InvalidOrder < StandardError
3
3
  end
4
+
5
+ class NoStock < StandardError
6
+ end
4
7
  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.0
4
+ version: 0.2.1
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-09-28 00:00:00.000000000 Z
11
+ date: 2014-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails