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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39966542011d55c929999e9579b07ae23f6026a0
4
- data.tar.gz: 6d9934377f63d12b8e08c8378d489ac4c4f8f369
3
+ metadata.gz: a055877b518c695c504b0910b9ee3b53d7dee695
4
+ data.tar.gz: b09e9b447b40d0185ef69a7f108a2807798ae29e
5
5
  SHA512:
6
- metadata.gz: 585762254b5f9536f437adb23ddc5bcbb58248e706a281ac93ffcb6aca2cdfcd45452f6964cbeaf0a638d73831f4c7391500e2cc77d33baffdc63f38322d15cf
7
- data.tar.gz: 75e659902be830d751e14fb5ce7cc75cbefdf6a03ff79ceb6e025973e3ca4315eeb970fb4fb3c7027072f6948914694cf2692e25ddb5fa7400e595cd1325d8a5
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 |product_id, quantity|
63
- @order.line_items <<
64
- LineItem.new(sellable: Product.find(product_id), quantity: quantity)
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
@@ -0,0 +1,7 @@
1
+ module OpCart
2
+ class PlansController < ApplicationController
3
+ def index
4
+ @plans = Plan.purchasable
5
+ end
6
+ end
7
+ end
@@ -13,7 +13,11 @@ module OpCart
13
13
  before_validation :snapshot_sellable
14
14
 
15
15
  def total
16
- (unit_price || sellable.price) * quantity
16
+ if sellable.is_a? Plan
17
+ 0
18
+ else
19
+ (unit_price || sellable.price) * quantity
20
+ end
17
21
  end
18
22
 
19
23
  private
@@ -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?
@@ -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
@@ -0,0 +1,7 @@
1
+ module OpCart
2
+ class PlanAddon < ActiveRecord::Base
3
+ belongs_to :plan
4
+ belongs_to :product
5
+ belongs_to :user
6
+ end
7
+ end
data/config/routes.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  OpCart::Engine.routes.draw do
2
+ resources :plans
2
3
  resources :orders
3
4
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module OpCart
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
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.3.2
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-01 00:00:00.000000000 Z
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