Skip to content
Open
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
6 changes: 4 additions & 2 deletions app/models/concerns/katalyst/tables/collection/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ module Core # :nodoc:
include Reducers

class_methods do
def config
@config ||= Config.new
def inherited(subclass)
super
subclass.config = config.dup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails takes a similar but different approach that might be clearer: rails/rails@1719d25

class_attribute :config, instance_predicate: false, default: ActiveSupport::OrderedOptions.new

class_methods do
  def inherited(klass)
      klass.config = ActiveSupport::InheritableOptions.new(config)
      super
    end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ActiveSupport::InheritableOptions looks like it's designed to work with hashes, or OrderedOptions (which extends Hash). We are using Base::Config class for config. We could stick with .dup, or update config to use OrderedOptions (but then we'd be accepting any attribute rather than just paginate and sorting)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the new approach, vs dup, and calling it before super

end

def permitted_params
Expand Down Expand Up @@ -53,6 +54,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
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