Class: PaperTrailDiff::VersionScopeFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/paper_trail_diff/version_scope_filter.rb,
sig/generated/paper_trail_diff/version_scope_filter.rbs

Overview

Applies a caller's version filter to a range, naming which of its versions count as selected mutations. The unfiltered versions stay available to the caller, because the one that reveals the last selected mutation is drawn from them.

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ VersionScopeFilter

: (untyped) -> void

Parameters:

  • (Object)


11
12
13
# File 'lib/paper_trail_diff/version_scope_filter.rb', line 11

def initialize(scope)
  @scope = validated(scope)
end

Instance Method Details

#call(relation, in_range) ⇒ Array[untyped]

: (untyped, Array) -> Array

Parameters:

  • (Object)
  • (Array[untyped])

Returns:

  • (Array[untyped])


16
17
18
19
20
21
22
23
24
# File 'lib/paper_trail_diff/version_scope_filter.rb', line 16

def call(relation, in_range)
  scope = @scope
  return in_range unless scope

  # A narrowed relation is the expected return. Active Support also gives
  # `pluck` to plain enumerables, so an array of versions works too.
  chosen = Set.new(scope.call(relation).pluck(:id))
  in_range.select { |version| chosen.include?(version.id) }
end

#validated(scope) ⇒ Object

: (untyped) -> untyped

Parameters:

  • (Object)

Returns:

  • (Object)


31
32
33
34
35
# File 'lib/paper_trail_diff/version_scope_filter.rb', line 31

def validated(scope)
  return scope if scope.nil? || scope.respond_to?(:call)

  raise ConfigurationError, 'version_scope: must respond to call'
end