Sha256: 449de1b6f016f8d49f7bd03e9dea829bd8afea549ae3b0559d0d16e44db934a3

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    module Collection
      module Core # :nodoc:
        extend ActiveSupport::Concern

        include ActiveModel::Model
        include ActiveModel::Attributes
        include ActiveModel::Dirty
        include ActiveSupport::Configurable

        include Reducers

        included do
          attr_accessor :items

          delegate :model, :each, :count, :empty?, to: :items, allow_nil: true
          delegate :model_name, to: :model, allow_nil: true
        end

        def initialize(**options)
          super

          clear_changes_information
        end

        def filter
          # no-op by default
        end

        def filtered?
          !self.class.new.filters.eql?(filters)
        end

        def filters
          attributes.except("page", "sort")
        end

        def apply(items)
          @items = items
          reducers.build do |_|
            filter
            self
          end.call(self)
          self
        end

        def with_params(params)
          self.attributes = params.permit(self.class.attribute_types.keys)

          self
        end

        # Returns a hash of the current attributes that have changed from defaults.
        def to_params
          attributes.slice(*changed)
        end

        def inspect
          "#<#{self.class.name} @attributes=#{attributes.inspect} @model=\"#{model}\" @count=#{items&.count}>"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
katalyst-tables-2.2.4 app/models/concerns/katalyst/tables/collection/core.rb
katalyst-tables-2.2.3 app/models/concerns/katalyst/tables/collection/core.rb
katalyst-tables-2.2.2 app/models/concerns/katalyst/tables/collection/core.rb
katalyst-tables-2.2.1 app/models/concerns/katalyst/tables/collection/core.rb