Class: Spree::Api::VariantsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/api/variants_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
# File 'app/controllers/spree/api/variants_controller.rb', line 8

def create
  authorize! :create, Variant
  @variant = scope.new(variant_params)
  if @variant.save
    respond_with(@variant, status: 201, default_template: :show)
  else
    invalid_resource!(@variant)
  end
end

#destroyObject



18
19
20
21
22
# File 'app/controllers/spree/api/variants_controller.rb', line 18

def destroy
  @variant = scope.accessible_by(current_ability, :destroy).find(params[:id])
  @variant.discard
  respond_with(@variant, status: 204)
end

#indexObject

The lazyloaded associations here are pretty much attached to which nodes we render on the view so we better update it any time a node is included or removed from the views.



27
28
29
30
31
32
33
# File 'app/controllers/spree/api/variants_controller.rb', line 27

def index
  @variants = scope.includes(include_list)
    .ransack(params[:q]).result

  @variants = paginate(@variants)
  respond_with(@variants)
end

#newObject



35
36
# File 'app/controllers/spree/api/variants_controller.rb', line 35

def new
end

#showObject



38
39
40
41
42
# File 'app/controllers/spree/api/variants_controller.rb', line 38

def show
  @variant = scope.includes(include_list)
    .find(params[:id])
  respond_with(@variant)
end

#updateObject



44
45
46
47
48
49
50
51
# File 'app/controllers/spree/api/variants_controller.rb', line 44

def update
  @variant = scope.accessible_by(current_ability, :update).find(params[:id])
  if @variant.update(variant_params)
    respond_with(@variant, status: 200, default_template: :show)
  else
    invalid_resource!(@product)
  end
end