effective_orders 5.0.0 → 5.0.5

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
  SHA256:
3
- metadata.gz: 3f8f7d52a67d1ce51884188e1731619ec49e06862122a47c7ef6453a53b1a841
4
- data.tar.gz: f6326e488f80799a0b5425d319a0a576053a5f73d51967931d1b0b58281b5ce5
3
+ metadata.gz: 195cc9c586145c7c43e3765a482e6f843926b9beb4043e437960602cec6dc87d
4
+ data.tar.gz: 5feb696f017b304b3ce0f4cb63eb529cbbcb61616822af3ce97b405caa68f149
5
5
  SHA512:
6
- metadata.gz: f21b668907138f9d2257215ee2446064bceaccfe69bb4553923fac8c018f0309cf050a4aa26322d37bde38b67de363057cbdc5982bb30c085f09e4dae5b4dcca
7
- data.tar.gz: 5d78b1895cd76eafbb041324e5bc4be940a191c8d5a48ea3f5458af1d72c05b1cea9dafd0fbdab41bf56105a8f6ae0b28b5bb4ee9f47e7243c8fe76d04e25533
6
+ metadata.gz: 559e82ca80af432a84c99fadfe16ebf413f7c39725ba652568828f88cd91a4e1853adf86e327feb06df9b7e6a9e2916be3b96519847b905fef7b09162415f4cb
7
+ data.tar.gz: 4bc2f1e00f75619f168c76d6d8b4a2716baee26dc6fe734bea1b7ff35faf781a4a33f908175e472f62032196a077baf1c5c84ab2f0366dbf1ca0012466936732
data/README.md CHANGED
@@ -351,7 +351,10 @@ The permissions you actually want to define for a regular user are as follows (u
351
351
 
352
352
  ```ruby
353
353
  can [:manage], Effective::Cart, user_id: user.id
354
+
354
355
  can [:manage], Effective::Order, user_id: user.id # Orders cannot be deleted
356
+ cannot [:edit, :update], Effective::Order, state: 'purchased'
357
+
355
358
  can [:manage], Effective::Subscription, user_id: user.id
356
359
  ```
357
360
 
@@ -0,0 +1 @@
1
+ //= link_directory ../images/effective_orders
@@ -50,7 +50,7 @@ module Admin
50
50
  # The show page posts to this action
51
51
  # See Effective::OrdersController checkout
52
52
  def checkout
53
- @order = Effective::Order.find(params[:id])
53
+ @order = Effective::Order.not_purchased.find(params[:id])
54
54
 
55
55
  authorize_effective_order!
56
56
 
@@ -5,28 +5,21 @@ module Effective
5
5
 
6
6
  protected
7
7
 
8
- def order_purchased(payment:, provider:, card: 'none', email: true, skip_buyer_validations: false, purchased_url: nil, declined_url: nil)
9
- begin
10
- @order.purchase!(payment: payment, provider: provider, card: card, email: email, skip_buyer_validations: skip_buyer_validations)
11
-
12
- Effective::Cart.where(user: @order.user).destroy_all
13
-
14
- if flash[:success].blank?
15
- if EffectiveOrders.mailer[:send_order_receipt_to_buyer] && email
16
- flash[:success] = "Payment successful! A receipt has been sent to #{@order.emails_send_to}"
17
- else
18
- flash[:success] = "Payment successful! An email receipt has not been sent."
19
- end
20
- end
8
+ def order_purchased(payment:, provider:, card: 'none', email: true, skip_buyer_validations: false, purchased_url: nil)
9
+ @order.purchase!(payment: payment, provider: provider, card: card, email: email, skip_buyer_validations: skip_buyer_validations)
21
10
 
22
- purchased_url ||= effective_orders.purchased_order_path(':id')
23
- redirect_to purchased_url.gsub(':id', @order.to_param.to_s)
24
- rescue => e
25
- flash[:danger] = "An error occurred while processing your payment: #{e.message}. Please try again."
11
+ Effective::Cart.where(user: @order.user).destroy_all
26
12
 
27
- declined_url ||= effective_orders.declined_order_path(':id')
28
- redirect_to declined_url.gsub(':id', @order.to_param.to_s)
13
+ if flash[:success].blank?
14
+ if email && EffectiveOrders.mailer[:send_order_receipt_to_buyer]
15
+ flash[:success] = "Payment successful! A receipt has been sent to #{@order.emails_send_to}"
16
+ else
17
+ flash[:success] = "Payment successful! An email receipt has not been sent."
18
+ end
29
19
  end
20
+
21
+ purchased_url ||= effective_orders.purchased_order_path(':id')
22
+ redirect_to purchased_url.gsub(':id', @order.to_param.to_s)
30
23
  end
31
24
 
32
25
  def order_deferred(provider:, email: true, deferred_url: nil)
@@ -1,5 +1,6 @@
1
1
  module Effective
2
2
  class OrdersController < ApplicationController
3
+ include Effective::CrudController
3
4
  include Concerns::Purchase
4
5
 
5
6
  include Providers::Cheque
@@ -12,8 +13,6 @@ module Effective
12
13
  include Providers::Refund
13
14
  include Providers::Stripe
14
15
 
15
- include Effective::CrudController
16
-
17
16
  if (config = EffectiveOrders.layout)
18
17
  layout(config.kind_of?(Hash) ? (config[:orders] || config[:application]) : config)
19
18
  end
@@ -68,13 +67,13 @@ module Effective
68
67
 
69
68
  # Always step1
70
69
  def edit
71
- @order ||= Effective::Order.find(params[:id])
70
+ @order ||= Effective::Order.not_purchased.find(params[:id])
72
71
  EffectiveResources.authorize!(self, :edit, @order)
73
72
  end
74
73
 
75
74
  # Confirms the order from existing order
76
75
  def update
77
- @order ||= Effective::Order.find(params[:id])
76
+ @order ||= Effective::Order.not_purchased.find(params[:id])
78
77
  EffectiveResources.authorize!(self, :update, @order)
79
78
 
80
79
  @order.assign_attributes(checkout_params)
@@ -110,7 +109,7 @@ module Effective
110
109
  end
111
110
 
112
111
  def send_buyer_receipt
113
- @order = Effective::Order.find(params[:id])
112
+ @order = Effective::Order.purchased.find(params[:id])
114
113
  EffectiveResources.authorize!(self, :show, @order)
115
114
 
116
115
  if @order.send_order_receipt_to_buyer!
@@ -21,7 +21,6 @@ module Effective
21
21
  provider: 'free',
22
22
  card: 'none',
23
23
  purchased_url: free_params[:purchased_url],
24
- declined_url: free_params[:declined_url],
25
24
  email: false
26
25
  )
27
26
  end
@@ -19,8 +19,7 @@ module Effective
19
19
  card: mark_as_paid_params[:payment_card],
20
20
  email: @order.send_mark_as_paid_email_to_buyer?,
21
21
  skip_buyer_validations: true,
22
- purchased_url: effective_orders.admin_order_path(@order),
23
- declined_url: effective_orders.admin_order_path(@order)
22
+ purchased_url: effective_orders.admin_order_path(@order)
24
23
  )
25
24
  end
26
25
 
@@ -14,8 +14,7 @@ module Effective
14
14
  payment: 'for pretend',
15
15
  provider: 'pretend',
16
16
  card: 'none',
17
- purchased_url: pretend_params[:purchased_url],
18
- declined_url: pretend_params[:declined_url]
17
+ purchased_url: pretend_params[:purchased_url]
19
18
  )
20
19
  end
21
20
 
@@ -21,8 +21,7 @@ module Effective
21
21
  order_purchased(
22
22
  payment: 'refund. no payment required.',
23
23
  provider: 'refund',
24
- purchased_url: refund_params[:purchased_url],
25
- declined_url: refund_params[:declined_url]
24
+ purchased_url: refund_params[:purchased_url]
26
25
  )
27
26
  end
28
27
 
@@ -26,8 +26,7 @@ module Effective
26
26
  payment: payment,
27
27
  provider: 'stripe',
28
28
  card: payment[:card],
29
- purchased_url: stripe_params[:purchased_url],
30
- declined_url: stripe_params[:declined_url]
29
+ purchased_url: stripe_params[:purchased_url]
31
30
  )
32
31
  end
33
32
 
@@ -14,12 +14,12 @@ class Admin::EffectiveOrdersDatatable < Effective::Datatable
14
14
  end
15
15
 
16
16
  filters do
17
- if attributes[:user_id].blank? && attributes[:parent_id].blank?
18
- scope :purchased, default: true
17
+ unless attributes[:skip_filters]
18
+ scope :all
19
+ scope :purchased
19
20
  scope :deferred
20
21
  scope :refunds
21
22
  scope :not_purchased
22
- scope :all
23
23
  end
24
24
  end
25
25
 
@@ -84,13 +84,14 @@ class Admin::EffectiveOrdersDatatable < Effective::Datatable
84
84
  end
85
85
 
86
86
  collection do
87
- scope = Effective::Order.all.includes(:addresses, :order_items, :user)
87
+ scope = Effective::Order.all.deep
88
88
 
89
89
  if EffectiveOrders.orders_collection_scope.respond_to?(:call)
90
90
  scope = EffectiveOrders.orders_collection_scope.call(scope)
91
91
  end
92
92
 
93
93
  if attributes[:user_id].present?
94
+ user = current_user.class.find(attributes[:user_id])
94
95
  scope = scope.where(user: user)
95
96
  end
96
97
 
@@ -101,8 +102,4 @@ class Admin::EffectiveOrdersDatatable < Effective::Datatable
101
102
  scope
102
103
  end
103
104
 
104
- def user
105
- @user ||= current_user.class.find(attributes[:user_id])
106
- end
107
-
108
105
  end
@@ -6,8 +6,6 @@ class EffectiveOrdersDatatable < Effective::Datatable
6
6
  scope :purchased, default: true
7
7
  scope :deferred
8
8
  scope :refunds
9
- scope :not_purchased
10
- scope :all
11
9
  end
12
10
  end
13
11
 
@@ -61,7 +59,9 @@ class EffectiveOrdersDatatable < Effective::Datatable
61
59
  end
62
60
 
63
61
  collection do
64
- scope = Effective::Order.all.where(user: user).includes(:addresses, :order_items, :user)
62
+ user = current_user.class.find(attributes[:user_id])
63
+
64
+ scope = Effective::Order.all.deep.where(user: user)
65
65
 
66
66
  if EffectiveOrders.orders_collection_scope.respond_to?(:call)
67
67
  scope = EffectiveOrders.orders_collection_scope.call(scope)
@@ -78,8 +78,4 @@ class EffectiveOrdersDatatable < Effective::Datatable
78
78
  scope
79
79
  end
80
80
 
81
- def user
82
- @user ||= current_user.class.find(attributes[:user_id])
83
- end
84
-
85
81
  end
@@ -107,13 +107,7 @@ module EffectiveOrdersHelper
107
107
  end
108
108
  end
109
109
 
110
- def render_orders(obj, opts = {})
111
- orders = Array(obj.kind_of?(User) ? Effective::Order.purchased_by(obj) : obj)
112
-
113
- if orders.any? { |order| order.kind_of?(Effective::Order) == false }
114
- raise 'expected a User or Effective::Order'
115
- end
116
-
110
+ def render_orders(orders, opts = {})
117
111
  render(partial: 'effective/orders/orders_table', locals: { orders: orders }.merge(opts))
118
112
  end
119
113
 
@@ -34,10 +34,6 @@ module Effective
34
34
  belongs_to :user, polymorphic: true, validate: false # This is the buyer/user of the order. We validate it below.
35
35
  has_many :order_items, -> { order(:id) }, inverse_of: :order, dependent: :delete_all
36
36
 
37
- if defined?(EffectiveQbSync)
38
- has_one :qb_order_item
39
- end
40
-
41
37
  accepts_nested_attributes_for :order_items, allow_destroy: false, reject_if: :all_blank
42
38
  accepts_nested_attributes_for :user, allow_destroy: false, update_only: true
43
39
 
@@ -78,6 +74,10 @@ module Effective
78
74
  self.state = EffectiveOrders::CONFIRMED if pending?
79
75
  end
80
76
 
77
+ before_save(if: -> { state_was == EffectiveOrders::PURCHASED }) do
78
+ raise EffectiveOrders::AlreadyPurchasedException.new('cannot unpurchase an order') unless purchased?
79
+ end
80
+
81
81
  # Order validations
82
82
  validates :user_id, presence: true
83
83
  validates :email, presence: true, email: true # email and cc validators are from effective_resources
@@ -137,7 +137,7 @@ module Effective
137
137
  validates :payment_provider, presence: true, inclusion: { in: EffectiveOrders.deferred_providers }
138
138
  end
139
139
 
140
- scope :deep, -> { includes(:user, order_items: :purchasable) }
140
+ scope :deep, -> { includes(:addresses, :user, order_items: :purchasable) }
141
141
  scope :sorted, -> { order(:id) }
142
142
 
143
143
  scope :purchased, -> { where(state: EffectiveOrders::PURCHASED) }
@@ -261,12 +261,6 @@ module Effective
261
261
  end
262
262
  end
263
263
 
264
- # first or build
265
- def qb_item_name
266
- raise('expected EffectiveQbSync gem') unless defined?(EffectiveQbSync)
267
- (qb_order_item || build_qb_order_item(name: purchasable.qb_item_name)).name
268
- end
269
-
270
264
  def pending?
271
265
  state == EffectiveOrders::PENDING
272
266
  end
@@ -338,6 +332,8 @@ module Effective
338
332
  # It skips any address or bad user validations
339
333
  # It's basically the same as save! on a new order, except it might send the payment request to buyer
340
334
  def pending!
335
+ return false if purchased?
336
+
341
337
  self.state = EffectiveOrders::PENDING
342
338
  self.addresses.clear if addresses.any? { |address| address.valid? == false }
343
339
  save!
@@ -348,6 +344,7 @@ module Effective
348
344
 
349
345
  # Used by admin checkout only
350
346
  def confirm!
347
+ return false if purchased?
351
348
  update!(state: EffectiveOrders::CONFIRMED)
352
349
  end
353
350
 
@@ -365,38 +362,31 @@ module Effective
365
362
 
366
363
  # Effective::Order.new(items: Product.first, user: User.first).purchase!(email: false)
367
364
  def purchase!(payment: 'none', provider: 'none', card: 'none', email: true, skip_buyer_validations: false)
368
- return false if purchased?
369
- error = nil
370
-
371
- assign_attributes(
372
- state: EffectiveOrders::PURCHASED,
373
- payment: payment_to_h(payment),
374
- payment_provider: provider,
375
- payment_card: (card.presence || 'none'),
376
- skip_buyer_validations: skip_buyer_validations
377
- )
365
+ # Assign attributes
366
+ self.state = EffectiveOrders::PURCHASED
367
+ self.skip_buyer_validations = skip_buyer_validations
378
368
 
369
+ self.payment_provider ||= provider
370
+ self.payment_card ||= (card.presence || 'none')
379
371
  self.purchased_at ||= Time.zone.now
372
+ self.payment = payment_to_h(payment) if self.payment.blank?
380
373
 
381
- Effective::Order.transaction do
382
- begin
374
+ begin
375
+ Effective::Order.transaction do
383
376
  run_purchasable_callbacks(:before_purchase)
384
377
  save!
385
378
  update_purchasables_purchased_order!
386
- rescue => e
387
- self.state = state_was
388
- self.purchased_at = nil
389
-
390
- error = e.message
391
- raise ::ActiveRecord::Rollback
392
379
  end
393
- end
380
+ rescue => e
381
+ Effective::Order.transaction do
382
+ save!(validate: false)
383
+ update_purchasables_purchased_order!
384
+ end
394
385
 
395
- raise "Failed to purchase order: #{error || errors.full_messages.to_sentence}" unless error.nil?
386
+ raise(e)
387
+ end
396
388
 
397
389
  run_purchasable_callbacks(:after_purchase)
398
-
399
- send_refund_notification! if email && refund?
400
390
  send_order_receipts! if email
401
391
 
402
392
  true
@@ -459,6 +449,7 @@ module Effective
459
449
  def send_order_receipts!
460
450
  send_order_receipt_to_admin! if EffectiveOrders.mailer[:send_order_receipt_to_admin]
461
451
  send_order_receipt_to_buyer! if EffectiveOrders.mailer[:send_order_receipt_to_buyer]
452
+ send_refund_notification! if refund?
462
453
  end
463
454
 
464
455
  def send_order_receipt_to_admin!
@@ -5,6 +5,10 @@ module Effective
5
5
  belongs_to :order
6
6
  belongs_to :purchasable, polymorphic: true
7
7
 
8
+ if defined?(EffectiveQbSync)
9
+ has_one :qb_order_item
10
+ end
11
+
8
12
  effective_resource do
9
13
  name :string
10
14
  quantity :integer
@@ -61,5 +65,11 @@ module Effective
61
65
  end
62
66
  end
63
67
 
68
+ # first or build
69
+ def qb_item_name
70
+ raise('expected EffectiveQbSync gem') unless defined?(EffectiveQbSync)
71
+ (qb_order_item || build_qb_order_item(name: purchasable.qb_item_name)).name
72
+ end
73
+
64
74
  end
65
75
  end
@@ -4,6 +4,8 @@ module Effective
4
4
 
5
5
  acts_as_purchasable
6
6
 
7
+ # belongs_to :purchased_order_id
8
+
7
9
  effective_resource do
8
10
  name :string
9
11
  qb_item_name :string
@@ -3,7 +3,7 @@
3
3
  = link_to 'Print', '#', class: 'btn btn-primary print-button', data: { role: 'print-button' }, onClick: 'window.print(); false;'
4
4
 
5
5
  - if order.purchased?
6
- = link_to 'Email receipt to buyer', effective_orders.send_buyer_receipt_order_path(order),
6
+ = link_to 'E-mail Receipt', effective_orders.send_buyer_receipt_order_path(order),
7
7
  class: 'btn btn-secondary',
8
8
  data: { confirm: "Send receipt to #{order.emails_send_to}?" }
9
9
 
@@ -4,3 +4,7 @@
4
4
  = render_checkout(@order)
5
5
  - else
6
6
  = render @order
7
+
8
+ %hr
9
+
10
+ = link_to 'Continue', effective_orders.orders_path, class: 'btn btn-primary'
@@ -16,8 +16,8 @@ module EffectiveOrders
16
16
  eval File.read("#{config.root}/config/effective_orders.rb")
17
17
  end
18
18
 
19
- initializer "effective_orders.append_precompiled_assets" do |app|
20
- Rails.application.config.assets.precompile += ['effective_orders/*']
19
+ initializer 'effective_orders.assets' do |app|
20
+ app.config.assets.precompile += ['effective_orders_manifest.js', 'effective_orders/*']
21
21
  end
22
22
 
23
23
  initializer 'effective_orders.refund', after: :load_config_initializers do
@@ -1,3 +1,3 @@
1
1
  module EffectiveOrders
2
- VERSION = '5.0.0'.freeze
2
+ VERSION = '5.0.5'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_orders
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-24 00:00:00.000000000 Z
11
+ date: 2021-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -118,6 +118,7 @@ extra_rdoc_files: []
118
118
  files:
119
119
  - MIT-LICENSE
120
120
  - README.md
121
+ - app/assets/config/effective_orders_manifest.js
121
122
  - app/assets/images/effective_orders/stripe.png
122
123
  - app/assets/javascripts/effective_orders.js
123
124
  - app/assets/javascripts/effective_orders/customers.js.coffee