Skip to content

Commit

Permalink
Make ensure methods re-usable across actions
Browse files Browse the repository at this point in the history
Previously these were only used on edit actions which was fine because
the `render_edit_with_errors` method is able to accommodate both the `update_amount` and `invalidate` actions. However, I want to re-use the `ensure_amount` method inside of the new/create flow.

Since new/create flow would need to render a different template (eg.
`render_new_with_errors`) I have altered the ensure method to accept a
block instead of being hard-coded to `render_edit_with_errors`.
  • Loading branch information
MadelineCollier committed Dec 18, 2024
1 parent 9c5283a commit e8593a8
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions admin/app/controllers/solidus_admin/store_credits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def edit_amount
end

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

if @store_credit.update_amount(permitted_store_credit_params[:amount], @store_credit_reason, spree_current_user)
respond_to do |format|
Expand Down Expand Up @@ -100,7 +100,7 @@ def edit_validity
end

def invalidate
return unless ensure_store_credit_reason
return unless ensure_store_credit_reason { render_edit_with_errors }

if @store_credit.invalidate(@store_credit_reason, spree_current_user)
flash[:notice] = t('.success')
Expand Down Expand Up @@ -171,7 +171,7 @@ def render_edit_with_errors
def ensure_amount
if permitted_store_credit_params[:amount].blank?
@store_credit.errors.add(:amount, :greater_than, count: 0, value: permitted_store_credit_params[:amount])
render_edit_with_errors
yield if block_given? # Block is for error template rendering on a per-action basis so this can be re-used.
return false
end
true
Expand All @@ -182,7 +182,7 @@ def ensure_store_credit_reason

if @store_credit_reason.blank?
@store_credit.errors.add(:store_credit_reason_id, "Store Credit reason must be provided")
render_edit_with_errors
yield if block_given? # Block is for error template rendering on a per-action basis so this can be re-used.
return false
end
true
Expand Down

0 comments on commit e8593a8

Please sign in to comment.