Class: Spree::Admin::RelationsController

Inherits:
ResourceController
  • Object
show all
Defined in:
app/controllers/spree/admin/relations_controller.rb

Overview

This controller manages the creation, updating, and deletion of product relations in the Spree e-commerce platform. It handles actions such as creating new relations, updating discount amounts, updating positions, and destroying relations.

Instance Method Summary collapse

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/spree/admin/relations_controller.rb', line 14

def create
  @relation = Relation.new(relation_params)
  @relation.relatable = @product
  @relation.related_to = Spree::Variant.find(relation_params[:related_to_id]).product

  if @relation.save
    flash[:success] = message_after_create
  else
    flash[:error] = @relation.errors.full_messages.to_sentence
  end

  redirect_to(related_admin_product_url(@relation.relatable))
end

#updateObject



28
29
30
31
32
# File 'app/controllers/spree/admin/relations_controller.rb', line 28

def update
  @relation.update_attribute :discount_amount, relation_params[:discount_amount] || 0
  flash[:success] = message_after_update
  redirect_to(related_admin_product_url(@relation.relatable))
end

#update_positionsObject



34
35
36
37
# File 'app/controllers/spree/admin/relations_controller.rb', line 34

def update_positions
  @relation.insert_at(relation_params[:position].to_i)
  render json: { status: :ok }
end