Class: Spree::Api::ResourceController

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

Direct Known Subclasses

UsersController

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/spree/api/resource_controller.rb', line 20

def create
  authorize! :create, model_class

  @object = model_class.new(permitted_resource_params)
  instance_variable_set("@#{object_name}", @object)

  if @object.save
    respond_with(@object, status: 201, default_template: :show)
  else
    invalid_resource!(@object)
  end
end

#destroyObject



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

def destroy
  authorize! :destroy, @object

  if @object.destroy
    respond_with(@object, status: 204)
  else
    invalid_resource!(@object)
  end
end

#indexObject



4
5
6
7
8
9
# File 'app/controllers/spree/api/resource_controller.rb', line 4

def index
  @collection = model_class.accessible_by(current_ability, :read).ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  instance_variable_set("@#{controller_name}", @collection)

  respond_with(@collection)
end

#newObject



15
16
17
18
# File 'app/controllers/spree/api/resource_controller.rb', line 15

def new
  authorize! :new, model_class
  respond_with(model_class.new)
end

#showObject



11
12
13
# File 'app/controllers/spree/api/resource_controller.rb', line 11

def show
  respond_with(@object)
end

#updateObject



33
34
35
36
37
38
39
40
41
# File 'app/controllers/spree/api/resource_controller.rb', line 33

def update
  authorize! :update, @object

  if @object.update_attributes(permitted_resource_params)
    respond_with(@object, status: 200, default_template: :show)
  else
    invalid_resource!(@object)
  end
end