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 +4 -4
- data/.rake_tasks~ +7 -0
- data/README.md +12 -2
- data/lib/kart/error.rb +7 -0
- data/lib/kart/version.rb +1 -1
- data/lib/kart.rb +57 -3
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2bf83dd15cc1524891228328237e59c3b7beda4f3e0b9e9ed6a6e971be3ee4a
|
4
|
+
data.tar.gz: e991e6732480c45cc80462b384b7f70e1aef7f7ba558c5d5dd8f614580fce7c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d35dfd55dce137c04cd5c0a595c2aa3c23cc738a04e347d4141eaf1b4881e4ba4beb3ec020e3534ceca7bf24257bed0af7938018fd70dca1347664afd24f9e3
|
7
|
+
data.tar.gz: ff146457772c81e9a2c0c0c379b7745656769344656ec6b236518f425a03a99831691b637e5740389c9aa41111814188940f3869a086fbacdaefe6cc9578f595
|
data/.rake_tasks~
ADDED
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
|
-
|
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
data/lib/kart/version.rb
CHANGED
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
|
-
|
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
|
-
|
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.
|
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-
|
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
|