Class: Plutonium::Positioning::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/plutonium/positioning/config.rb

Overview

Strategy configuration object created by the position_on DSL.

Three modes:

Mode A (:delegate) — delegate reposition! to Plutonium::Positioning::Model
Mode B (:block)    — call a user-supplied block with a Move
Mode C (:disabled) — no ordering; relation returned unchanged

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, attribute, block) ⇒ Config

Returns a new instance of Config.



41
42
43
44
45
# File 'lib/plutonium/positioning/config.rb', line 41

def initialize(mode, attribute, block)
  @mode = mode
  @attribute = attribute
  @block = block
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



39
40
41
# File 'lib/plutonium/positioning/config.rb', line 39

def attribute
  @attribute
end

Class Method Details

.attribute(attr) ⇒ Object

Mode A, custom attribute



25
26
27
# File 'lib/plutonium/positioning/config.rb', line 25

def self.attribute(attr)
  new(:delegate, attr.to_sym, nil)
end

.defaultObject

Mode A, default attribute :position



20
21
22
# File 'lib/plutonium/positioning/config.rb', line 20

def self.default
  new(:delegate, :position, nil)
end

.disabledObject

Mode C — disabled



35
36
37
# File 'lib/plutonium/positioning/config.rb', line 35

def self.disabled
  new(:disabled, nil, nil)
end

.with_block(attr, block) ⇒ Object

Mode B — orders by attr, write delegated to block



30
31
32
# File 'lib/plutonium/positioning/config.rb', line 30

def self.with_block(attr, block)
  new(:block, attr.to_sym, block)
end

Instance Method Details

#delegate?Boolean

Mode A — the framework owns the write, via Positioning::Model. Callers ask because Mode A is the only mode whose storage they may reason about: a fractional position between two neighbours, in a known scope group. A Mode B block owns both the storage and its own notion of neighbours, so nothing outside it may second-guess the drop it is handed.

Returns:

  • (Boolean)


56
57
58
# File 'lib/plutonium/positioning/config.rb', line 56

def delegate?
  @mode == :delegate
end

#disabled?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/plutonium/positioning/config.rb', line 47

def disabled?
  @mode == :disabled
end

#order(relation) ⇒ Object

Apply positional ordering to a relation. Mode A/B: relation.reorder(attribute) Mode C: return relation unchanged



63
64
65
66
# File 'lib/plutonium/positioning/config.rb', line 63

def order(relation)
  return relation if disabled?
  relation.reorder(@attribute)
end

#reposition!(record:, prev_record:, next_record:, index:, column: nil) ⇒ Object

Persist the new position for a dropped record. Returns true when the caller must reconcile its view of the list because positions other than this record's may have changed.

Mode A: delegate to record.reposition!(prev_record:, next_record:), and report whether it had to rebalance the scope group. Mode B: call the user block with a Move; always true — the block is an opaque write, and gems in this space routinely renumber the whole group (acts_as_list does). Mode C: no-op; always false — nothing was written.

column is the kanban column key on a board, and nil on every other surface (index tables, nested tables, grids) — those have no columns.



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/plutonium/positioning/config.rb', line 81

def reposition!(record:, prev_record:, next_record:, index:, column: nil)
  case @mode
  when :delegate
    record.reposition!(prev_record:, next_record:).rebalanced?
  when :block
    @block.call(Move.new(record:, column:, prev: prev_record, next: next_record, index:))
    true
  when :disabled
    false
  end
end