Sha256: 7594ba7ab4211c2b5789ba81d140ddaf7ac18243a66e5199dc53a201abbacf87

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require "active_support"
require "active_support/concern"

require_relative "backend/sort_form"

module Katalyst
  module Tables
    # Utilities for controllers that are generating collections for visualisation
    # in a table view using Katalyst::Tables::Frontend.
    #
    # Provides `table_sort` for sorting based on column interactions (sort param).
    module Backend
      extend ActiveSupport::Concern

      # Sort the given collection by params[:sort], which is set when a user
      # interacts with a column header in a frontend table view.
      #
      # @return [[SortForm, ActiveRecord::Relation]]
      def table_sort(collection)
        column, direction = params[:sort]&.split(" ")
        direction         = "asc" unless SortForm::DIRECTIONS.include?(direction)

        SortForm.new(column: column,
                     direction: direction)
                .apply(collection)
      end

      included do
        class_attribute :_default_table_component, instance_accessor: false
      end

      class_methods do
        # Set the table component to be used as the default for all tables
        # in the views rendered by this controller and its subclasses.
        #
        # ==== Parameters
        # * <tt>component</tt> - Default table component, an instance of +Katalyst::TableComponent+
        def default_table_component(component)
          self._default_table_component = component
        end
      end

      # Default table component for this controller
      def default_table_component
        self.class._default_table_component
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
katalyst-tables-2.1.1 lib/katalyst/tables/backend.rb
katalyst-tables-2.1.0 lib/katalyst/tables/backend.rb
katalyst-tables-2.0.0 lib/katalyst/tables/backend.rb