Class: SolidusAdmin::StoreCreditsController

Inherits:
ResourcesController show all
Defined in:
app/controllers/solidus_admin/store_credits_controller.rb

Instance Method Summary collapse

Methods inherited from ResourcesController

#destroy, #edit, #update

Methods included from ComponentsHelper

#component

Instance Method Details

#createObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 39

def create
  @store_credit = @user.store_credits.build(
    permitted_resource_params.merge({
      created_by: spree_current_user,
      action_originator: spree_current_user
    })
  )

  return unless ensure_amount { render_new_with_errors }
  return unless ensure_store_credit_category { render_new_with_errors }

  if @store_credit.save
    flash[:notice] = t('.success')
    redirect_to after_create_path, status: :see_other
  else
    render_new_with_errors
  end
end

#edit_amountObject



58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 58

def edit_amount
  respond_to do |format|
    format.html {
      render component("users/store_credits/edit_amount").new(
        user: @user,
        store_credit: @store_credit,
        reasons: @store_credit_reasons
      )
    }
  end
end

#edit_memoObject



82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 82

def edit_memo
  respond_to do |format|
    format.html {
      render component("users/store_credits/edit_memo").new(
        user: @user,
        store_credit: @store_credit,
      )
    }
  end
end

#edit_validityObject



104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 104

def edit_validity
  respond_to do |format|
    format.html {
      render component("users/store_credits/edit_validity").new(
        user: @user,
        store_credit: @store_credit,
        reasons: @store_credit_reasons
      )
    }
  end
end

#indexObject



11
12
13
14
15
16
17
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 11

def index
  @store_credits = Spree::StoreCredit.where(user_id: @user.id).order(id: :desc)

  respond_to do |format|
    format.html { render component("users/store_credits/index").new(user: @user, store_credits: @store_credits) }
  end
end

#invalidateObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 116

def invalidate
  return unless ensure_store_credit_reason { render_edit_with_errors }

  if @store_credit.invalidate(@store_credit_reason, spree_current_user)
    flash[:notice] = t('.success')
  else
    # Ensure store_credit_reason handles invalid param/form submissions and modal re-rendering.
    # This is just a fallback error state in case anything goes wrong with StoreCredit#invalidate.
    flash[:error] = t('.failure')
  end

  redirect_to after_update_path, status: :see_other
end

#newObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 25

def new
  @store_credit ||= Spree::StoreCredit.new

  respond_to do |format|
    format.html {
      render component("users/store_credits/new").new(
        user: @user,
        store_credit: @store_credit,
        categories: @store_credit_categories
      )
    }
  end
end

#showObject



19
20
21
22
23
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 19

def show
  respond_to do |format|
    format.html { render component("users/store_credits/show").new(user: @user, store_credit: @store_credit, events: @store_credit_events) }
  end
end

#update_amountObject



70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 70

def update_amount
  return unless ensure_amount { render_edit_with_errors }
  return unless ensure_store_credit_reason { render_edit_with_errors }

  if @store_credit.update_amount(permitted_resource_params[:amount], @store_credit_reason, spree_current_user)
    flash[:notice] = t('.success')
    redirect_to after_update_path, status: :see_other
  else
    render_edit_with_errors
  end
end

#update_memoObject



93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/solidus_admin/store_credits_controller.rb', line 93

def update_memo
  if @store_credit.update(memo: permitted_resource_params[:memo])
    flash[:notice] = t('.success')
  else
    # Memo update failures are nearly impossible to trigger due to lack of validation.
    flash[:error] = t('.failure')
  end

  redirect_to after_update_path, status: :see_other
end