Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app/models/concerns/katalyst/tables/collection/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/katalyst/tables/config.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "active_support/configurable"

module Katalyst
module Tables
class Config
Expand Down
19 changes: 19 additions & 0 deletions spec/models/katalyst/tables/collection/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions spec/models/katalyst/tables/collection/pagination_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions spec/support/collection_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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