op_cart 0.3.2 → 0.4.0
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 +4 -4
- data/app/controllers/op_cart/orders_controller.rb +8 -3
- data/app/controllers/op_cart/plans_controller.rb +7 -0
- data/app/models/op_cart/line_item.rb +5 -1
- data/app/models/op_cart/order.rb +14 -0
- data/app/models/op_cart/plan.rb +5 -0
- data/app/models/op_cart/plan_addon.rb +7 -0
- data/config/routes.rb +1 -0
- data/db/migrate/20150203043805_create_op_cart_plan_addons.rb +12 -0
- data/db/seeds.rb +25 -0
- data/lib/op_cart/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a055877b518c695c504b0910b9ee3b53d7dee695
|
4
|
+
data.tar.gz: b09e9b447b40d0185ef69a7f108a2807798ae29e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de05c032008bf45018a3e243bfa6dea54f4dae090fcce0213625b60c8bcffa1f661af8a1a983674cd41373c00e7ba7a43930db10dad57d5cb01b7a733164a7da
|
7
|
+
data.tar.gz: 8fe5ab19e078945cfe0b34b1ddbd062a26718a9390f3b71058ef4489fcfd4005e1e41ed7e4f2b324b4c7b5ff8469e39688c76c4d79d6f23ed27c00d6f705f486
|
@@ -6,6 +6,8 @@ module OpCart
|
|
6
6
|
def new
|
7
7
|
@products = Product.all
|
8
8
|
@order = Order.new
|
9
|
+
@order.line_items << LineItem.new
|
10
|
+
@plan = Plan.purchasable.find(params[:plan_id]) if params[:plan_id]
|
9
11
|
@user = current_user || User.new
|
10
12
|
@card = current_user.try(:customer).try :default_card
|
11
13
|
@shipping_address = @user.shipping_addresses.first || @user.shipping_addresses.new
|
@@ -59,9 +61,12 @@ module OpCart
|
|
59
61
|
@line_items = OpenStruct.new(quantities_json: line_items_params[:quantities_json] || {})
|
60
62
|
if @line_items.quantities_json.present?
|
61
63
|
li_quantities_json = JSON.parse @line_items.quantities_json
|
62
|
-
li_quantities_json.each do |
|
63
|
-
|
64
|
-
|
64
|
+
li_quantities_json.each do |plan_id, quantity|
|
65
|
+
plan = Plan.find plan_id
|
66
|
+
@order.line_items << LineItem.new(sellable: plan, quantity: quantity)
|
67
|
+
plan.plan_addons.each do |addon|
|
68
|
+
@order.line_items << LineItem.new(sellable: addon.product, quantity: quantity)
|
69
|
+
end
|
65
70
|
end
|
66
71
|
end
|
67
72
|
end
|
data/app/models/op_cart/order.rb
CHANGED
@@ -14,6 +14,7 @@ module OpCart
|
|
14
14
|
validates :line_items, presence: true
|
15
15
|
validates :shipping_address, associated: true
|
16
16
|
validates :user, presence: true
|
17
|
+
validate :validate_plan_addons_included
|
17
18
|
|
18
19
|
before_validation :set_total
|
19
20
|
before_validation -> { self.status = :pending }, unless: :status?
|
@@ -25,6 +26,19 @@ module OpCart
|
|
25
26
|
self.total = line_items.reduce(0) { |total, line_item| total += line_item.total }
|
26
27
|
end
|
27
28
|
|
29
|
+
def validate_plan_addons_included
|
30
|
+
unless line_items.flat_map do |li|
|
31
|
+
if li.sellable.is_a?(Plan) && (addons = li.sellable.plan_addons).present?
|
32
|
+
addons.map do |addon|
|
33
|
+
addon_li = line_items.select{|pali| pali.sellable == addon.product }
|
34
|
+
addon_li.count == 1 && addon_li[0].quantity == li.quantity
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end.compact.all?
|
38
|
+
self.errors.add :base, 'Plan addon(s) missing'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
28
42
|
def charge_customer
|
29
43
|
customer = Customer.find_or_create_by user: user
|
30
44
|
if processor_token.present?
|
data/app/models/op_cart/plan.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module OpCart
|
2
2
|
class Plan < ActiveRecord::Base
|
3
|
+
has_many :plan_addons
|
3
4
|
belongs_to :user
|
4
5
|
|
5
6
|
before_validation -> { self.interval_count = 1 }, unless: :interval_count?
|
@@ -21,6 +22,10 @@ module OpCart
|
|
21
22
|
@processor_object ||= Stripe::Plan.retrieve processor_token
|
22
23
|
end
|
23
24
|
|
25
|
+
def addons_price
|
26
|
+
plan_addons.reduce(0) { |total, addon| total += addon.product.price }
|
27
|
+
end
|
28
|
+
|
24
29
|
private
|
25
30
|
|
26
31
|
def create_plan
|
data/config/routes.rb
CHANGED
@@ -0,0 +1,12 @@
|
|
1
|
+
class CreateOpCartPlanAddons < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :op_cart_plan_addons do |t|
|
4
|
+
t.boolean :recurring
|
5
|
+
t.references :plan, index: true
|
6
|
+
t.references :product, index: true
|
7
|
+
t.references :user, index: true
|
8
|
+
|
9
|
+
t.timestamps null: false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/db/seeds.rb
CHANGED
@@ -21,3 +21,28 @@ product2_attrs = {
|
|
21
21
|
}
|
22
22
|
|
23
23
|
product2 = OpCart::Product.where(sku: product2_attrs[:sku]).first_or_create product2_attrs
|
24
|
+
|
25
|
+
rand_plan_no = rand 10**9
|
26
|
+
plan1_attrs = {
|
27
|
+
name: "Gold Plan #{rand_plan_no}",
|
28
|
+
processor_token: "gold-#{rand_plan_no}",
|
29
|
+
interval: 'month',
|
30
|
+
interval_count: 1,
|
31
|
+
price: '1500',
|
32
|
+
description: 'Better than Silver.',
|
33
|
+
image_url: 'http://example.com/example.png',
|
34
|
+
trial_period_days: 30,
|
35
|
+
allow_purchases: true,
|
36
|
+
user: User.first,
|
37
|
+
}
|
38
|
+
|
39
|
+
plan1 = OpCart::Plan.where(name: plan1_attrs[:name]).first_or_create plan1_attrs
|
40
|
+
|
41
|
+
plan_addon1_attrs = {
|
42
|
+
recurring: false,
|
43
|
+
product: product1,
|
44
|
+
plan: plan1,
|
45
|
+
user: User.first,
|
46
|
+
}
|
47
|
+
|
48
|
+
plan_addon1 = OpCart::PlanAddon.first_or_create plan_addon1_attrs
|
data/lib/op_cart/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: op_cart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Boehs
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -78,11 +78,13 @@ files:
|
|
78
78
|
- Rakefile
|
79
79
|
- app/controllers/op_cart/application_controller.rb
|
80
80
|
- app/controllers/op_cart/orders_controller.rb
|
81
|
+
- app/controllers/op_cart/plans_controller.rb
|
81
82
|
- app/models/op_cart/card.rb
|
82
83
|
- app/models/op_cart/customer.rb
|
83
84
|
- app/models/op_cart/line_item.rb
|
84
85
|
- app/models/op_cart/order.rb
|
85
86
|
- app/models/op_cart/plan.rb
|
87
|
+
- app/models/op_cart/plan_addon.rb
|
86
88
|
- app/models/op_cart/product.rb
|
87
89
|
- app/models/op_cart/shipping_address.rb
|
88
90
|
- config/initializers/stripe.rb
|
@@ -95,6 +97,7 @@ files:
|
|
95
97
|
- db/migrate/20150131000000_create_op_cart_customers.rb
|
96
98
|
- db/migrate/20150131000001_create_op_cart_cards.rb
|
97
99
|
- db/migrate/20150201033629_create_op_cart_plans.rb
|
100
|
+
- db/migrate/20150203043805_create_op_cart_plan_addons.rb
|
98
101
|
- db/seeds.rb
|
99
102
|
- lib/op_cart.rb
|
100
103
|
- lib/op_cart/engine.rb
|