kart 0.2.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: e827a6a69dbb085b00635ec694a1b1d54f5a0814d320328700611dd926fa4b62
4
- data.tar.gz: 91cab535632e944b3af02c3467959062fbf5ad6326ed83c744f03c371247e734
3
+ metadata.gz: d2bf83dd15cc1524891228328237e59c3b7beda4f3e0b9e9ed6a6e971be3ee4a
4
+ data.tar.gz: e991e6732480c45cc80462b384b7f70e1aef7f7ba558c5d5dd8f614580fce7c3
5
5
  SHA512:
6
- metadata.gz: 1c8fbeb8c41bec68f1a2c2f0ffc4af653d03b3f7a448341a1229f072ebbabed5cfc0a5640b9e2a0879a46f6f4d05eb54a5347e91b458d7621eda2ab19ffe2833
7
- data.tar.gz: dbd2f590e3731ede047fe836c08786a39fd952a62281b5d6a254855a9eeb3cdd8623e6e480ce61d4a5618dbab3d569b756ebb73bfba5c3c83a7061b3269328a5
6
+ metadata.gz: 8d35dfd55dce137c04cd5c0a595c2aa3c23cc738a04e347d4141eaf1b4881e4ba4beb3ec020e3534ceca7bf24257bed0af7938018fd70dca1347664afd24f9e3
7
+ data.tar.gz: ff146457772c81e9a2c0c0c379b7745656769344656ec6b236518f425a03a99831691b637e5740389c9aa41111814188940f3869a086fbacdaefe6cc9578f595
data/.rake_tasks~ ADDED
@@ -0,0 +1,7 @@
1
+ build
2
+ clean
3
+ clobber
4
+ install
5
+ install:local
6
+ release[remote]
7
+ spec
data/README.md CHANGED
@@ -7,9 +7,19 @@ Yet another implementation for shopping cart, really simple and easy to use.
7
7
 
8
8
  ## Usage
9
9
  **Make sure to create [User] and [Product] model beforehand**
10
- * `rails generate cart [User] [Product]`
10
+ * `rails generate cart [User] [Product]`\
11
11
  example: `rails g cart Member Book` will create a cart contains books selected by a member.
12
12
  * `rails db:migrate`
13
13
  these files created by this command are self-explanatory.
14
14
  * insert `selecting :product` to [User] and `selected_by :user` to [Product].
15
- * execute `show_cart` instance method to get current cart of user.
15
+
16
+ ## Export methods
17
+ * `selecting_products`: list of products are selected by current user
18
+ * `show_cart`: list of product id and quantity in cart of current user
19
+ * `add_to_cart (Product)`: add 1 product to cart
20
+ * `remove_from_cart (Product)`: remove 1 product from cart
21
+ * `remove_all_from_cart (Product)`: remove all kind of this product from cart (when multiple was chosen)
22
+
23
+ ## Handling exception
24
+ * **Kart::UserNotDefined**: insert `selected_by` into Product model
25
+ * **Kart::ProductNotDefined**: insert `selecting` into User model
data/lib/kart/error.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Kart
2
+ class UserNotDefined < StandardError
3
+ end
4
+
5
+ class ProductNotDefined < StandardError
6
+ end
7
+ end
data/lib/kart/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kart
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/kart.rb CHANGED
@@ -1,23 +1,77 @@
1
1
  require "kart/version"
2
2
  require "active_support/concern"
3
3
  require "kart/railtie" if defined?(Rails)
4
+ require "kart/error"
4
5
 
5
6
  module Kart
6
7
  extend ActiveSupport::Concern
7
-
8
+
8
9
  included do
10
+ @@user_cname = "user"
11
+ @@product_cname = "product"
12
+
9
13
  def self.selecting product
14
+ @@product_cname = product
15
+
10
16
  has_many :carts
11
17
  has_many :selecting_products, through: :carts, source: product
12
18
 
13
19
  define_method "show_cart" do
14
- selecting_products
20
+ Cart.where _user => self.id
21
+ end
22
+
23
+ define_method "add_to_cart" do |product|
24
+ item = Cart.find_by _user => self.id, _product => product.id
25
+
26
+ if item.nil?
27
+ Cart.new(_user => self.id, _product => product.id, :quantity => 1).save
28
+ else
29
+ item.quantity += 1
30
+ item.save
31
+ end
32
+ end
33
+
34
+ define_method "remove_from_cart" do |product|
35
+ item = Cart.find_by _user => self.id, _product => product.id
36
+
37
+ unless item.nil?
38
+ item.quantity -= 1
39
+ item.save
40
+ end
41
+ end
42
+
43
+ define_method "remove_all_from_cart" do |product|
44
+ item = Cart.find_by _user => self.id, _product => product.id
45
+
46
+ unless item.nil?
47
+ Cart.destroy item.id
48
+ end
15
49
  end
16
50
  end
17
51
 
18
52
  def self.selected_by user
53
+ @@user_cname = user
54
+
19
55
  has_many :carts
20
56
  has_many :selected_users, through: :carts, source: user
21
57
  end
22
- end
58
+
59
+ private
60
+
61
+ def _user
62
+ if @@user_cname.nil?
63
+ raise Kart::UserNotDefined, "Insert selected_by into [Product] model"
64
+ else
65
+ (@@user_cname.to_s + "_id").to_sym
66
+ end
67
+ end
68
+
69
+ def _product
70
+ if @@product_cname.nil?
71
+ raise Kart::ProductNotDefined, "Insert selecting into [User] model"
72
+ else
73
+ (@@product_cname.to_s + "_id").to_sym
74
+ end
75
+ end
76
+ end
23
77
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tran Son Tung
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-07 00:00:00.000000000 Z
11
+ date: 2019-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,6 +74,7 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
+ - ".rake_tasks~"
77
78
  - ".rspec"
78
79
  - ".travis.yml"
79
80
  - CODE_OF_CONDUCT.md
@@ -89,6 +90,7 @@ files:
89
90
  - lib/generators/templates/migration.rb
90
91
  - lib/generators/templates/model.rb
91
92
  - lib/kart.rb
93
+ - lib/kart/error.rb
92
94
  - lib/kart/railtie.rb
93
95
  - lib/kart/version.rb
94
96
  homepage: https://github.com/tungts1101/another_cart.git