Class: Resourcey::Controller

Inherits:
Object
  • Object
show all
Includes:
ControllerCollectionScope, ControllerFiltering, ControllerModel, ControllerPagination, ControllerSerialization
Defined in:
lib/resourcey/controller.rb

Instance Method Summary collapse

Methods included from ControllerSerialization

#build_serializer_from_configuration

Methods included from ControllerCollectionScope

#scoped_resources

Methods included from ControllerFiltering

#filtered_resources

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
# File 'lib/resourcey/controller.rb', line 20

def create
  @resource = resource_model.new(resource_params)

  if @resource.save
    render json: @resource, serializer: serializer, status: :ok
  else
    render json: { errors: @resource.errors }, status: :bad_request
  end
end

#destroyObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/resourcey/controller.rb', line 41

def destroy
  find_resource
  @resource.destroy

  if @resource.destroyed?
    render json: true, status: :ok
  else
    render json: { errors: @resource.errors }, status: :bad_request
  end
end

#indexObject



11
12
13
# File 'lib/resourcey/controller.rb', line 11

def index
  render json: serialized_collection(scoped_resources)
end

#showObject



15
16
17
18
# File 'lib/resourcey/controller.rb', line 15

def show
  find_resource
  render json: @resource, serializer: serializer
end

#updateObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/resourcey/controller.rb', line 30

def update
  find_resource
  @resource.assign_attributes(resource_params)

  if @resource.save
    render json: @resource, serializer: serializer, status: :ok
  else
    render json: { errors: @resource.errors }, status: :bad_request
  end
end