lib/trello_helper.rb
def bounding_card_ids_by_id(cards)
    lis = longest_increasing_sequence(cards.map { |c| c.card.pos })
    run_start = -1
    run_end = -1
    cards_between_bounding = {}
    # Iterate over the cards to find contiguous runs of cards which need
    # their position attribute updated
    cards.each_with_index do |card, index|
      if !lis.include? card.card.pos
        if run_start == -1
          run_start = index
        end
        run_end = index
      end
      # Check if we're in a run of cards that need updating
      if run_start != -1
        # Check if we've reached the end of the run
        if (lis.include? card.card.pos) || (index == (cards.length - 1))
          # Find the position of the in-order card preceding the run,
          # use that to determine the lower bound for the run of cards
          # needing updates.
          #
          # Determine before and after card ids. After_index will
          # always be the same - we want the card to be between the
          # previous card and the end of the run, so it's always
          # properly ordered regardless how trello renumbers the list.
          after_index = (run_end == (cards.length - 1)) ? nil : cards[run_end + 1].card.id
          (run_start..run_end).each do |run_index|
            bounding_card_ids = {}
            bounding_card_ids[:before] = (run_index == 0) ? nil : cards[run_index - 1].card.id
            bounding_card_ids[:after] = after_index
            cards_between_bounding[cards[run_index].card.id] = bounding_card_ids
          end
          run_start = -1
          run_end = -1
        end
      end
    end
    cards_between_bounding
  end
# For each card which needs to be updated, find the ids of cards
# before and after it which should be used in calculating its new
# position. Handle edge cases where the card is at going to be at
# the start or end of the list
#
# The end of the run is always selected as the "after" card, since
# in the caller, we calculate the new position as halfway between
# the "before" card position and the "after" card position.
# Selecting the end of the run makes sure that the calculated
# position is strictly increasing, even if Trello renumbers the
# cards
For each card which needs to be updated, find the ids of cards
before and after it which should be used in calculating its new
position. Handle edge cases where the card is at going to be at
the start or end of the list


For each card which needs to be updated find the ids of cards before and after it which should be used in calculating its new position . Handle edge cases where the card is at going to be at the start or end of the list
def bounding_card_ids_by_id(cards)
    lis = longest_increasing_sequence(cards.map { |c| c.card.pos })
    run_start = -1
    run_end = -1
    cards_between_bounding = {}
    cards.each_with_index do |card, index|
      if !lis.include? card.card.pos
        if run_start == -1
          run_start = index
        end
        run_end = index
      end
      if run_start != -1
        if (lis.include? card.card.pos) || (index == (cards.length - 1))
           Determine before and after card ids. After_index will
           always be the same - we want the card to be between the
           previous card and the end of the run, so it's always
           properly ordered regardless how trello renumbers the list.
          after_index = (run_end == (cards.length - 1)) ? nil : cards[run_end + 1].card.id
          (run_start..run_end).each do |run_index|
            bounding_card_ids = {}
            bounding_card_ids[:before] = (run_index == 0) ? nil : cards[run_index - 1].card.id
            bounding_card_ids[:after] = after_index
            cards_between_bounding[cards[run_index].card.id] = bounding_card_ids
          end
          run_start = -1
          run_end = -1
        end
      end
    end
    cards_between_bounding
  end
def bounding_card_ids_by_id ( cards ) lis = longest_increasing_sequence ( cards . map { | c | c . card . pos } ) run_start = - 1 run_end = - 1 cards_between_bounding = { } cards . each_with_index do | card , index | if ! lis . include? card . card . pos if run_start == - 1 run_start = index end run_end = index end if run_start != - 1 if ( lis . include? card . card . pos ) || ( index == ( cards . length - 1 ) ) after_index = ( run_end == ( cards . length - 1 ) ) ? nil : cards [ run_end + 1 ] . card . id ( run_start .. run_end ) . each do | run_index | bounding_card_ids = { } bounding_card_ids [ :before ] = ( run_index == 0 ) ? nil : cards [ run_index - 1 ] . card . id bounding_card_ids [ :after ] = after_index cards_between_bounding [ cards [ run_index ] . card . id ] = bounding_card_ids end run_start = - 1 run_end = - 1 end end end cards_between_bounding end
For each card which needs to be updated, find the ids of cards
before and after it which should be used in calculating its new
position.
For each card which needs to be updated find the ids of cards before and after it which should be used in calculating its new position .
# Iterate over the cards to find contiguous runs of cards which need # their position attribute updated # Check if we're in a run of cards that need updating # Check if we've reached the end of the run # Find the position of the in-order card preceding the run, # use that to determine the lower bound for the run of cards # needing updates. # # Determine before and after card ids. After_index will # always be the same - we want the card to be between the # previous card and the end of the run, so it's always # properly ordered regardless how trello renumbers the list.
cards