Class: Api::ElementsController

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

Instance Method Summary collapse

Instance Method Details

#indexObject

Returns all elements as json object

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

If you want to only load a specific type of element pass ?named=an_element_name



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/alchemy/api/elements_controller.rb', line 11

def index
  # Fix for cancancan not able to merge multiple AR scopes for logged in users
  @elements = if cannot? :manage, Alchemy::Element
    Alchemy::Element.accessible_by(current_ability, :index)
  else
    Alchemy::Element.all
  end

  @elements = @elements.not_nested.joins(:page_version).merge(PageVersion.published)

  @elements = if params[:page_id].present?
    @elements.includes(:page).where(alchemy_pages: {id: params[:page_id]})
  else
    @elements.includes(*element_includes)
  end

  if params[:named].present?
    @elements = @elements.named(params[:named])
  end
  @elements = @elements.order(:position)

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

#showObject

Returns a json object for element



37
38
39
40
41
# File 'app/controllers/alchemy/api/elements_controller.rb', line 37

def show
  @element = Element.where(id: params[:id]).includes(*element_includes).first
  authorize! :show, @element
  respond_with @element
end