Module: Archangel::Controllers::ResourcefulConcern

Extended by:
ActiveSupport::Concern
Included in:
Backend::AssetsController, Backend::CollectionsController, Backend::DesignsController, Backend::EntriesController, Backend::PagesController, Backend::UsersController, Backend::WidgetsController
Defined in:
app/controllers/concerns/archangel/controllers/resourceful_concern.rb

Overview

Resourceful concern

Usage

module Archangel
  module Backend
    class ExamplesController < BackendController
      include Archangel::Controllers::ResourcefulConcern

      protected

      def permitted_attributes
        %w[bar bat baz]
      end

      def resources_content
        @examples = current_site.examples.all

        authorize @examples
      end

      def resource_content
        resource_id = params.fetch(:id)

        @example = current_site.examples.find_by!(id: resource_id)

        authorize @example
      end

      def resource_new_content
        @example = current_site.examples.new(resource_new_params)

        authorize @example
      end
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#createObject

Create resource

Formats

HTML, JSON

Request

POST /resources
POST /resources.json

Parameters

{
  "resource": {
    "id": 123,
    ...
  }
}


145
146
147
148
149
150
151
# File 'app/controllers/concerns/archangel/controllers/resourceful_concern.rb', line 145

def create
  resource = resource_new_content || {}

  resource.save if resource.present?

  respond_with resource, location: -> { location_after_create }
end

#destroyObject

Destroy resource

Formats

HTML, JSON

Params

[Integer] id - the resource ID

Request

DELETE /resources/:id
DELETE /resources/:id.json


224
225
226
227
228
229
230
# File 'app/controllers/concerns/archangel/controllers/resourceful_concern.rb', line 224

def destroy
  resource = resource_content || nil

  resource.destroy if resource.present?

  respond_with resource, location: -> { location_after_destroy }
end

#editObject

Edit resource

Formats

HTML, JSON

Params

[Integer] id - the resource ID

Request

GET /resources/:id/edit
GET /resources/:id/edit.json

Response

{
  "id": 123,
  ...
  "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
  "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ"
}


174
175
176
177
178
# File 'app/controllers/concerns/archangel/controllers/resourceful_concern.rb', line 174

def edit
  resource = resource_content || {}

  respond_with resource
end

#indexObject

Resources

Formats

HTML, JSON

Request

GET /resources
GET /resources.json

Response

[
  {
    "id": 123,
    ...
    "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
    "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ"
  },
  ...
]


70
71
72
73
74
# File 'app/controllers/concerns/archangel/controllers/resourceful_concern.rb', line 70

def index
  resources = resources_content || []

  respond_with resources
end

#newObject

New resource

Formats

HTML, JSON

Request

GET /resources/new
GET /resources/new.json

Response

{
  "id": null,
  ...
  "created_at": null,
  "updated_at": null
}


121
122
123
124
125
# File 'app/controllers/concerns/archangel/controllers/resourceful_concern.rb', line 121

def new
  resource = resource_new_content || nil

  respond_with resource
end

#showObject

Resource

Formats

HTML, JSON

Params

[Integer] id - the resource ID

Request

GET /resources/:id
GET /resources/:id.json

Response

{
  "id": 123,
  ...
  "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
  "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ"
}


97
98
99
100
101
# File 'app/controllers/concerns/archangel/controllers/resourceful_concern.rb', line 97

def show
  resource = resource_content || {}

  respond_with resource
end

#updateObject

Update resource

Formats

HTML, JSON

Params

[Integer] id - the resource ID

Request

PATCH /resources/:id
PATCH /resources/:id.json
PUT   /resources/:id
PUT   /resources/:id.json

Parameters

{
  "resource": {
    "id": 123,
    ...
  }
}


203
204
205
206
207
208
209
# File 'app/controllers/concerns/archangel/controllers/resourceful_concern.rb', line 203

def update
  resource = resource_content || {}

  resource.update(resource_params) if resource.present?

  respond_with resource, location: -> { location_after_update }
end