Module: Pagy::Keyset::Adapters::ActiveRecord

Defined in:
lib/pagy/classes/keyset/adapters/active_record.rb

Overview

Keyset adapter for ActiveRecord

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(including) ⇒ Object



44
45
46
47
48
# File 'lib/pagy/classes/keyset/adapters/active_record.rb', line 44

def self.included(including)
  instance_methods(false).each do |method_name|
    including.send(:protected, method_name)
  end
end

Instance Method Details

#apply_where(predicate, arguments) ⇒ Object

Apply the where predicate to the set



40
41
42
# File 'lib/pagy/classes/keyset/adapters/active_record.rb', line 40

def apply_where(predicate, arguments)
  @set = @set.where(predicate, **arguments)
end

#ensure_selectObject

Append the missing keyset keys, if the set is restricted by select



33
34
35
36
37
# File 'lib/pagy/classes/keyset/adapters/active_record.rb', line 33

def ensure_select
  return if @set.select_values.empty?

  @set = @set.select(*@keyset.keys)
end

#extract_keysetObject

Extract the keyset from the set



9
10
11
12
13
# File 'lib/pagy/classes/keyset/adapters/active_record.rb', line 9

def extract_keyset
  @set.order_values.each_with_object({}) do |node, keyset|
    keyset[node.value.name.to_sym] = node.direction
  end
end

#keyset_attributes_from(record) ⇒ Object

Get the keyset attributes from a record



16
17
18
# File 'lib/pagy/classes/keyset/adapters/active_record.rb', line 16

def keyset_attributes_from(record)
  record.slice(*@keyset.keys)
end

#quoted_identifiers(table) ⇒ Object

Get the hash of quoted keyset identifiers



21
22
23
24
# File 'lib/pagy/classes/keyset/adapters/active_record.rb', line 21

def quoted_identifiers(table)
  connection = @set.connection
  @keyset.to_h { |column| [column, "#{connection.quote_table_name(table)}.#{connection.quote_column_name(column)}"] }
end

#typecast(attributes) ⇒ Object

Typecast the attributes



27
28
29
30
# File 'lib/pagy/classes/keyset/adapters/active_record.rb', line 27

def typecast(attributes)
  @set.model.new(attributes).slice(attributes.keys)
      .to_hash.transform_keys(&:to_sym)
end