Sha256: e2cf8087b3a574ea156acbf50fd94b8883620b77861a085038af98e5fe457414

Contents?: true

Size: 1.74 KB

Versions: 16

Compression:

Stored size: 1.74 KB

Contents

# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/metadata
# frozen_string_literal: true

class Pagy
  # Add a specialized backend method for pagination metadata
  module Backend
  private

    METADATA = %i[ scaffold_url first_url prev_url page_url next_url last_url
                   count page items vars pages last from to prev next series
               ].tap do |metadata|
                 metadata << :sequels if VARS.key?(:steps)  # :steps gets defined along with the #sequels method
               end.freeze

    VARS[:metadata] = METADATA.dup

    include Helpers

    def pagy_metadata(pagy, deprecated_url=nil, absolute: nil)
      absolute = Pagy.deprecated_arg(:url, deprecated_url, :absolute, absolute) if deprecated_url
      names   = pagy.vars[:metadata]
      unknown = names - METADATA
      raise VariableError.new(pagy), "unknown metadata #{unknown.inspect}" \
            unless unknown.empty?

      scaffold_url = pagy_url_for(pagy, PAGE_PLACEHOLDER, absolute: absolute)
      {}.tap do |metadata|
        names.each do |key|
          metadata[key] = case key
                          when :scaffold_url then scaffold_url
                          when :first_url    then scaffold_url.sub(PAGE_PLACEHOLDER, 1.to_s)
                          when :prev_url     then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.prev.to_s)
                          when :page_url     then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.page.to_s)
                          when :next_url     then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.next.to_s)
                          when :last_url     then scaffold_url.sub(PAGE_PLACEHOLDER, pagy.last.to_s)
                          else pagy.send(key)
                          end
        end
      end
    end

  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/pagy-4.11.0/lib/pagy/extras/metadata.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/pagy-4.11.0/lib/pagy/extras/metadata.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/pagy-4.11.0/lib/pagy/extras/metadata.rb
pagy-4.11.0 lib/pagy/extras/metadata.rb
pagy-4.10.2 lib/pagy/extras/metadata.rb
pagy-4.10.1 lib/pagy/extras/metadata.rb
pagy-4.10.0 lib/pagy/extras/metadata.rb
pagy-4.9.0 lib/pagy/extras/metadata.rb
pagy-4.8.1 lib/pagy/extras/metadata.rb
pagy-4.8.0 lib/pagy/extras/metadata.rb
pagy-4.7.1 lib/pagy/extras/metadata.rb
pagy-4.7.0 lib/pagy/extras/metadata.rb
pagy-4.6.0 lib/pagy/extras/metadata.rb
pagy-4.5.1 lib/pagy/extras/metadata.rb
pagy-4.5.0 lib/pagy/extras/metadata.rb
pagy-4.4.0 lib/pagy/extras/metadata.rb