Sha256: 0624bc61b369566199076d2fd816003da717527a26ca0ac7923130c51a1ad68a

Contents?: true

Size: 1.64 KB

Versions: 26

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Alchemy
  class Api::ElementsController < Api::BaseController
    # 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
    #
    def index
      # Fix for cancancan not able to merge multiple AR scopes for logged in users
      if cannot? :manage, Alchemy::Element
        @elements = Alchemy::Element.accessible_by(current_ability, :index)
      else
        @elements = Alchemy::Element.all
      end

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

      if params[:page_id].present?
        @elements = @elements.includes(:page).where(alchemy_pages: { id: params[:page_id] })
      else
        @elements = @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

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

    private

    def element_includes
      [
        {
          nested_elements: [
            {
              contents: :essence,
              ingredients: :related_object,
            },
            :tags,
          ],
        },
        {
          contents: :essence,
          ingredients: :related_object,
        },
        :tags,
      ]
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
alchemy_cms-6.1.10 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.9 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.8 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.7 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.6 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.5 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.0.14 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.4 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.0.13 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.3 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.2 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.1 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.1.0 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.0.12 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.0.11 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.0.10 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.0.9 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.0.8 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.0.7 app/controllers/alchemy/api/elements_controller.rb
alchemy_cms-6.0.6 app/controllers/alchemy/api/elements_controller.rb