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
34
35
36
37
38
39
# File 'app/controllers/spree/api/variants_controller.rb', line 27

def index
  @variants =
    if params[:variant_search_term]
      Spree::Config.variant_search_class.new(
        params[:variant_search_term], scope: scope
      ).results.includes(include_list)
    else
      scope.includes(include_list).ransack(params[:q]).result
    end

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

#newObject



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

def new
end

#showObject



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

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

#updateObject



50
51
52
53
54
55
56
57
# File 'app/controllers/spree/api/variants_controller.rb', line 50

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