Class: Spree::Billing::StoreCreditsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/billing/store_credits_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#current_vendor, #default_url_options, #edit_object_url, #handle_unauthorized_vendor, #page, #per_page, #required_vendor_user!, #set_locale, #switch_vendor, #vendors

Methods included from SpreeCmCommissioner::Billing::RoleAuthorization

#auth_action, #auth_entry, #auth_user, #authorize!, #authorize?, #authorize_admin, #authorize_role!, #redirect_unauthorized_access

Instance Method Details

#createObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/spree/billing/store_credits_controller.rb', line 16

def create
  @store_credit = @user.store_credits.build(
    permitted_store_credit_params.merge(
      created_by: try_spree_current_user,
      action_originator: try_spree_current_user,
      store: current_store,
      currency: current_store.default_currency,
      category: Spree::StoreCreditCategory.where(name: 'Default').first
    )
  )

  @duration = params[:store_credit][:duration].to_i
  @store_credit.memo = params[:store_credit][:description]
  @store_credit.amount = total_amount
  if @store_credit.save
    SpreeCmCommissioner::SubscriptionsPrepaidOrderCreator.call(customer: @customer, store_credit: @store_credit, duration: @duration)
    flash[:success] = flash_message_for(@store_credit, :successfully_created)
    redirect_to spree.billing_customer_store_credits_path(@customer)
  else
    load_categories
    flash[:error] = Spree.t('store_credit.errors.unable_to_create')
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/spree/billing/store_credits_controller.rb', line 55

def destroy
  @store_credit = @user.store_credits.for_store(current_store).find(params[:id])
  ensure_unused_store_credit

  if @store_credit.destroy
    flash[:success] = flash_message_for(@store_credit, :successfully_removed)
    respond_with(@store_credit) do |format|
      format.html { redirect_to spree.billing_customer_store_credits_path(@customer) }
      format.js { render_js_for_destroy }
    end
  else
    render plain: Spree.t('store_credit.errors.unable_to_delete'), status: :unprocessable_entity
  end
end

#indexObject



12
13
14
# File 'app/controllers/spree/billing/store_credits_controller.rb', line 12

def index
  @store_credits = @user.store_credits.for_store(current_store).includes(:category).reverse_order
end

#updateObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/spree/billing/store_credits_controller.rb', line 41

def update
  @store_credit.assign_attributes(permitted_store_credit_params)
  @store_credit.created_by = try_spree_current_user

  if @store_credit.save
    flash[:success] = flash_message_for(@store_credit, :successfully_updated)
    redirect_to spree.billing_customer_store_credits_path(@customer)
  else
    load_categories
    flash[:error] = Spree.t('store_credit.errors.unable_to_update')
    render :edit, status: :unprocessable_entity
  end
end