Class: Wco::PricesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/wco/prices_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#home, #tinymce

Methods included from ApplicationHelper

#my_truthy?, #obfuscate, #pp_amount, #pp_currency, #pp_date, #pp_datetime, #pp_money, #pp_percent, #pp_time, #pretty_date

Instance Method Details

#createObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/wco/prices_controller.rb', line 4

def create
  @price   = Wco::Price.new params[:price].permit!
  authorize! :create, @price

  @price.interval = nil if !params[:price][:interval].present?
  @product        = params[:price][:product_type].constantize.find @price.product_id
  @price.product  = @product

  stripe_product  = Stripe::Product.retrieve( @product.product_id )
  price_hash = {
    product:     stripe_product.id,
    unit_amount: @price.amount_cents,
    currency:    'usd',
  }
  if @price.interval.present?
    price_hash[:recurring] = { interval: @price.interval }
  end
  stripe_price = Stripe::Price.create( price_hash )
  flash_notice stripe_price
  @price.price_id = stripe_price[:id]

  if @price.save
    flash_notice @price
  else
    flash_alert @price
  end
  case @product.class.name
  when 'WcoHosting::ApplianceTmpl'
    redirect_to request.referrer || root_path
  when 'Wco::Product'
    redirect_to controller: :products, action: :index
  end
end

#destroyObject



38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/wco/prices_controller.rb', line 38

def destroy
  @price = Wco::Price.find params[:id]
  authorize! :destroy, @price
  flag = @price.delete
  if flag
    flash_notice 'ok'
  else
    flash_alert @price
  end
  redirect_to request.referrer || root_path
end

#newObject



50
51
52
53
54
55
# File 'app/controllers/wco/prices_controller.rb', line 50

def new
  @price = Wco::Price.new
  authorize! :new, @price

  @products_list = Wco::Product.list + WcoHosting::ApplianceTmpl.list
end