Class: Lexorank::Ranking

Inherits:
Object
  • Object
show all
Includes:
Lexorank
Defined in:
lib/lexorank/ranking.rb

Constant Summary

Constants included from Lexorank

MAX_CHAR, MIN_CHAR, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Lexorank

#get_char, #mid, #value_between

Constructor Details

#initialize(record_class:, field:, group_by:, advisory_lock:) ⇒ Ranking

Returns a new instance of Ranking.



8
9
10
11
12
13
# File 'lib/lexorank/ranking.rb', line 8

def initialize(record_class:, field:, group_by:, advisory_lock:)
  @record_class = record_class
  @field = field
  @group_by = process_group_by_column_name(group_by)
  @advisory_lock_config = { enabled: record_class.respond_to?(:with_advisory_lock) }.merge(advisory_lock)
end

Instance Attribute Details

#advisory_lock_configObject (readonly)

Returns the value of attribute advisory_lock_config.



6
7
8
# File 'lib/lexorank/ranking.rb', line 6

def advisory_lock_config
  @advisory_lock_config
end

#fieldObject (readonly)

Returns the value of attribute field.



6
7
8
# File 'lib/lexorank/ranking.rb', line 6

def field
  @field
end

#group_byObject (readonly)

Returns the value of attribute group_by.



6
7
8
# File 'lib/lexorank/ranking.rb', line 6

def group_by
  @group_by
end

#record_classObject (readonly)

Returns the value of attribute record_class.



6
7
8
# File 'lib/lexorank/ranking.rb', line 6

def record_class
  @record_class
end

Instance Method Details

#advisory_lock_name(instance) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lexorank/ranking.rb', line 91

def advisory_lock_name(instance)
  if advisory_lock_config[:lock_name].present?
    advisory_lock_config[:lock_name].(instance)
  else
    "#{record_class.table_name}_update_#{field}".tap do |name|
      if group_by.present?
        name << "_group_#{instance.send(group_by)}"
      end
    end
  end
end

#advisory_locks_enabled?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/lexorank/ranking.rb', line 103

def advisory_locks_enabled?
  record_class.respond_to?(:with_advisory_lock) && advisory_lock_config[:enabled]
end

#move_to(instance, position, **options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lexorank/ranking.rb', line 32

def move_to(instance, position, **options)
  if block_given? && advisory_locks_enabled?
    return with_lock_if_enabled(instance, **options.fetch(:advisory_lock, {})) do
      move_to(instance, position, **options)
      yield
    end
  end

  collection = record_class.ranked
  if group_by.present?
    collection = collection.where("#{group_by}": instance.send(group_by))
  end

  # exceptions:
  #   move to the beginning (aka move to position 0)
  #   move to end (aka position = collection.size - 1)
  # when moving to the end of the collection the offset and limit statement automatically handles
  # that 'after' is nil which is the same like [collection.last, nil]
  before, after =
    if position == :last
      [collection.last, nil]
    elsif position.zero?
      [nil, collection.first]
    else
      collection.where.not(id: instance.id).offset(position - 1).limit(2)
    end

  # If position >= collection.size both `before` and `after` will be nil. In this case
  # we set before to the last element of the collection
  if before.nil? && after.nil?
    before = collection.last
  end

  rank =
    if (self == after && send(field).present?) || (before == self && after.nil?)
      send(field)
    else
      value_between(before&.send(field), after&.send(field))
    end

  instance.send(:"#{field}=", rank)

  if block_given?
    yield
  else
    rank
  end
end

#validate!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lexorank/ranking.rb', line 15

def validate!
  if advisory_lock_config[:enabled] && !record_class.respond_to?(:with_advisory_lock)
    raise(
      Lexorank::InvalidConfigError,
      "Cannot enable advisory lock if #{record_class.name} does not respond to #with_advisory_lock. " \
      'Consider installing the with_advisory_lock gem (https://rubygems.org/gems/with_advisory_lock).'
    )
  end

  unless field
    raise(
      Lexorank::InvalidConfigError,
      'The supplied ":field" option cannot be "nil"!'
    )
  end
end

#with_lock_if_enabled(instance, **options) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/lexorank/ranking.rb', line 81

def with_lock_if_enabled(instance, **options, &)
  if advisory_locks_enabled?
    advisory_lock_options = advisory_lock_config.except(:enabled, :lock_name).merge(options)

    record_class.with_advisory_lock(advisory_lock_name(instance), **advisory_lock_options, &)
  else
    yield
  end
end