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
17
18
19
20
21
22
# File 'app/controllers/spree/api/variants_controller.rb', line 8

def create
  Spree::Deprecation.warn "    This route is deprecated. Use the route nested within the product resource:\n\n      POST api/products/{product_id}/variants\n  MSG\n\n  authorize! :create, Variant\n  @variant = scope.new(variant_params)\n  if @variant.save\n    respond_with(@variant, status: 201, default_template: :show)\n  else\n    invalid_resource!(@variant)\n  end\nend\n" unless request.path.include?('/products/')

#destroyObject



24
25
26
27
28
29
30
# File 'app/controllers/spree/api/variants_controller.rb', line 24

def destroy
  warn_if_nested_member_route

  @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.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/spree/api/variants_controller.rb', line 35

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



49
50
# File 'app/controllers/spree/api/variants_controller.rb', line 49

def new
end

#showObject



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

def show
  warn_if_nested_member_route

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

#updateObject



60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/spree/api/variants_controller.rb', line 60

def update
  warn_if_nested_member_route

  @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