Class: Api::ContentsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/alchemy/api/contents_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject

Returns all contents as json object

You can either load all or only these for :element_id param



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/alchemy/api/contents_controller.rb', line 9

def index
  # Fix for cancancan not able to merge multiple AR scopes for logged in users
  if can? :manage, Alchemy::Content
    @contents = Content.all
  else
    @contents = Content.accessible_by(current_ability, :index)
  end
  if params[:element_id].present?
    @contents = @contents.where(element_id: params[:element_id])
  end
  @contents = @contents.includes(*content_includes)

  render json: @contents, adapter: :json, root: "contents"
end

#showObject

Returns a json object for content

You can either load it from :id param or even more useful via passing the element id and the name of the content

$ bin/rake routes

for more infos on how the url looks like.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/alchemy/api/contents_controller.rb', line 33

def show
  if params[:id]
    @content = Content.where(id: params[:id]).includes(:essence).first
  elsif params[:element_id] && params[:name]
    @content = Content.where(
      element_id: params[:element_id],
      name: params[:name],
    ).includes(*content_includes).first || raise(ActiveRecord::RecordNotFound)
  end
  authorize! :show, @content
  respond_with @content
end