op_cart 0.3.1 → 0.3.2

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: e69269d8c9a1152b5369129c8c9d50713d4aabb7
4
- data.tar.gz: 9dd208f2ca331e1e8ed47abdc4fec93c02849782
3
+ metadata.gz: 39966542011d55c929999e9579b07ae23f6026a0
4
+ data.tar.gz: 6d9934377f63d12b8e08c8378d489ac4c4f8f369
5
5
  SHA512:
6
- metadata.gz: 53cd8e80758bfc5eec306927376e07f885eaec0cba12c648befeaf47bac85f6a64d251b109da038875006e66a3e9287933d50472a224581c33c1a40da0f1a45b
7
- data.tar.gz: d3bcec702719717a4fd3859543b223af70d3dbfa95db1fae5fbcb6331124342cace0a285a20324ae43e007e49fd6d1c45362589d05670fd558a45a248f7b85ac
6
+ metadata.gz: 585762254b5f9536f437adb23ddc5bcbb58248e706a281ac93ffcb6aca2cdfcd45452f6964cbeaf0a638d73831f4c7391500e2cc77d33baffdc63f38322d15cf
7
+ data.tar.gz: 75e659902be830d751e14fb5ce7cc75cbefdf6a03ff79ceb6e025973e3ca4315eeb970fb4fb3c7027072f6948914694cf2692e25ddb5fa7400e595cd1325d8a5
@@ -0,0 +1,53 @@
1
+ module OpCart
2
+ class Plan < ActiveRecord::Base
3
+ belongs_to :user
4
+
5
+ before_validation -> { self.interval_count = 1 }, unless: :interval_count?
6
+ before_create :create_plan
7
+ before_update :update_plan
8
+
9
+ validates :processor_token, uniqueness: true, presence: true
10
+ validates :name, :interval, presence: true
11
+ validates :price, :interval_count,
12
+ numericality: { only_integer: true, greater_than_or_equal_to: 0 }
13
+ validates :user, presence: true
14
+
15
+ validates :processor_token, :price, :interval_count, :interval,
16
+ unchangeable: true, on: :update
17
+
18
+ scope :purchasable, -> { where allow_purchases: true }
19
+
20
+ def processor_object
21
+ @processor_object ||= Stripe::Plan.retrieve processor_token
22
+ end
23
+
24
+ private
25
+
26
+ def create_plan
27
+ Stripe::Plan.create id: processor_token, name: name, amount: price,
28
+ currency: 'usd', interval: interval, interval_count: interval_count,
29
+ trial_period_days: trial_period_days, metadata: {
30
+ description: description, image_url: image_url, creator: user.email }
31
+ rescue Stripe::InvalidRequestError => e
32
+ if e.param
33
+ self.errors.add e.param, e.message
34
+ else
35
+ self.errors.add :base, e.message
36
+ end
37
+ false
38
+ end
39
+
40
+ def update_plan
41
+ processor_object.metadata = { description: description,
42
+ image_url: image_url, creator: user.email }
43
+ processor_object.save name: name
44
+ rescue Stripe::InvalidRequestError => e
45
+ if e.param
46
+ self.errors.add e.param, e.message
47
+ else
48
+ self.errors.add :base, e.message
49
+ end
50
+ false
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ class CreateOpCartPlans < ActiveRecord::Migration
2
+ def change
3
+ create_table :op_cart_plans do |t|
4
+ t.string :processor_token
5
+ t.string :name, null: false
6
+ t.string :description
7
+ t.string :image_url
8
+ t.integer :price
9
+ t.integer :interval_count
10
+ t.string :interval
11
+ t.integer :trial_period_days
12
+ t.boolean :allow_purchases, default: false
13
+ t.references :user, index: true
14
+
15
+ t.timestamps null: false
16
+ end
17
+ add_foreign_key :op_cart_plans, :users
18
+ end
19
+ end
@@ -1,4 +1,5 @@
1
1
  require "op_cart/engine"
2
+ require "op_cart/unchangeable_validator"
2
3
 
3
4
  module OpCart
4
5
  end
@@ -0,0 +1,9 @@
1
+ class UnchangeableValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ if !record.new_record? && value.present?
4
+ if record.changes.include?(attribute) && record.changes[attribute].any?
5
+ record.errors[attribute] << "cannot be changed once assigned"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module OpCart
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: op_cart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Boehs
@@ -82,6 +82,7 @@ files:
82
82
  - app/models/op_cart/customer.rb
83
83
  - app/models/op_cart/line_item.rb
84
84
  - app/models/op_cart/order.rb
85
+ - app/models/op_cart/plan.rb
85
86
  - app/models/op_cart/product.rb
86
87
  - app/models/op_cart/shipping_address.rb
87
88
  - config/initializers/stripe.rb
@@ -93,9 +94,11 @@ files:
93
94
  - db/migrate/20150125000000_create_op_cart_shipping_addresses.rb
94
95
  - db/migrate/20150131000000_create_op_cart_customers.rb
95
96
  - db/migrate/20150131000001_create_op_cart_cards.rb
97
+ - db/migrate/20150201033629_create_op_cart_plans.rb
96
98
  - db/seeds.rb
97
99
  - lib/op_cart.rb
98
100
  - lib/op_cart/engine.rb
101
+ - lib/op_cart/unchangeable_validator.rb
99
102
  - lib/op_cart/version.rb
100
103
  - lib/tasks/op_cart_tasks.rake
101
104
  - test/dummy/README.rdoc