Sha256: 35ae01f3d3954e2bc65b81d28c19a0bc4ec1a329cccaaed18e91881843095f27

Contents?: true

Size: 1.57 KB

Versions: 35

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module Alchemy
  class Api::NodesController < Api::BaseController
    before_action :load_node, except: :index
    before_action :authorize_access, only: [:move, :toggle_folded]

    def index
      @nodes = Node.all
      @nodes = @nodes.includes(:parent)
      @nodes = @nodes.where(language_id: params[:language_id]) if params[:language_id]
      @nodes = @nodes.ransack(params[:filter]).result

      if params[:page]
        @nodes = @nodes.page(params[:page]).per(params[:per_page])
      end

      render json: @nodes, adapter: :json, root: "data", meta: meta_data, include: params[:include]
    end

    def move
      target_parent_node = Node.find(params[:target_parent_id])
      @node.move_to_child_with_index(target_parent_node, params[:new_position])
      render json: @node, serializer: NodeSerializer
    end

    def toggle_folded
      @node.update(folded: !@node.folded)
      render json: @node, serializer: NodeSerializer
    end

    private

    def load_node
      @node = Node.find(params[:id])
    end

    def authorize_access
      authorize! :update, @node
    end

    def meta_data
      {
        total_count: total_count_value,
        per_page: per_page_value,
        page: page_value
      }
    end

    def total_count_value
      params[:page] ? @nodes.total_count : @nodes.size
    end

    def per_page_value
      if params[:page]
        (params[:per_page] || Kaminari.config.default_per_page).to_i
      else
        @nodes.size
      end
    end

    def page_value
      params[:page] ? params[:page].to_i : 1
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
alchemy_cms-7.1.12 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.0.15 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.11 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.10 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.9 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.0.14 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.0.13 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.8 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.7 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.0.12 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.0.11 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.6 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.5 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.0.10 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.4 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.0.9 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.3 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.2 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.1 app/controllers/alchemy/api/nodes_controller.rb
alchemy_cms-7.1.0 app/controllers/alchemy/api/nodes_controller.rb