diff --git a/app/models/concerns/katalyst/tables/collection/core.rb b/app/models/concerns/katalyst/tables/collection/core.rb index 26a068b..29b3ea6 100644 --- a/app/models/concerns/katalyst/tables/collection/core.rb +++ b/app/models/concerns/katalyst/tables/collection/core.rb @@ -5,6 +5,11 @@ module Tables module Collection class Config attr_accessor :paginate, :sorting + + def initialize(parent = nil) + @paginate = parent&.paginate&.dup + @sorting = parent&.sorting&.dup + end end module Core # :nodoc: @@ -18,8 +23,9 @@ module Core # :nodoc: include Reducers class_methods do - def config - @config ||= Config.new + def inherited(subclass) + subclass.config = Config.new(config) + super end def permitted_params @@ -53,6 +59,7 @@ def resolve_type_name(name, **) end included do + class_attribute :config, instance_accessor: false, default: Config.new attr_accessor :items, :unscoped_items delegate :each, :count, :empty?, to: :items, allow_nil: true diff --git a/lib/katalyst/tables/config.rb b/lib/katalyst/tables/config.rb index 442b017..c28fda5 100644 --- a/lib/katalyst/tables/config.rb +++ b/lib/katalyst/tables/config.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "active_support/configurable" - module Katalyst module Tables class Config diff --git a/spec/models/katalyst/tables/collection/base_spec.rb b/spec/models/katalyst/tables/collection/base_spec.rb index d686c11..4f64237 100644 --- a/spec/models/katalyst/tables/collection/base_spec.rb +++ b/spec/models/katalyst/tables/collection/base_spec.rb @@ -156,4 +156,23 @@ .to eq(Person.table_search("person").reorder(name: :desc).limit(20).offset(20).to_sql) end end + + context "with a subclass that overrides config" do + let(:superclass) do + Class.new(described_class) do + config.sorting = %i[col1 col2] + end + end + let!(:subclass) do + Class.new(superclass) do + config.sorting.push(:col3) + end + end + + it { expect(subclass.config.sorting).to eq(%i[col1 col2 col3]) } + + it "does not modify superclass config" do + expect(superclass.config.sorting).to eq(%i[col1 col2]) + end + end end diff --git a/spec/models/katalyst/tables/collection/pagination_spec.rb b/spec/models/katalyst/tables/collection/pagination_spec.rb index 3f056bb..5ab1d4b 100644 --- a/spec/models/katalyst/tables/collection/pagination_spec.rb +++ b/spec/models/katalyst/tables/collection/pagination_spec.rb @@ -11,6 +11,12 @@ it { is_expected.not_to be_filtered } it { is_expected.to have_attributes(to_params: {}) } + it { expect(Examples::PaginatedCollection.new).to be_paginate } + it { expect(Examples::PaginatedCollection.config).to have_attributes(paginate: { limit: 10 }) } + it { expect(Examples::InheritedPaginatedCollection.new).to be_paginate } + it { expect(Examples::InheritedPaginatedCollection.config).to have_attributes(paginate: { limit: 10 }) } + it { expect(Examples::OverriddenPaginatedCollection.new).to be_paginate } + it { expect(Examples::OverriddenPaginatedCollection.config).to have_attributes(paginate: { limit: 20 }) } it "does not paginate by default" do expect(collection.apply(items)).to have_attributes(count: 0, pagination: nil) diff --git a/spec/support/collection_examples.rb b/spec/support/collection_examples.rb index c01ac9f..0d7ec6b 100644 --- a/spec/support/collection_examples.rb +++ b/spec/support/collection_examples.rb @@ -83,4 +83,14 @@ def filter self.items = items.search(search) if search.present? end end + + class PaginatedCollection < Katalyst::Tables::Collection::Base + config.paginate = { limit: 10 } + end + + class InheritedPaginatedCollection < PaginatedCollection; end + + class OverriddenPaginatedCollection < PaginatedCollection + config.paginate = { limit: 20 } + end end