Class: SortableElementForNestedSet::TreeMoveCalculator
- Inherits:
-
Object
- Object
- SortableElementForNestedSet::TreeMoveCalculator
- Defined in:
- lib/tree_calc.rb
Overview
Calculate the new position of a moved tree within a nested set. The new placement and sort order is given as a hash, as created by draggable_element Javascript helper.
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Class Method Summary collapse
-
.create_tree_nodes(tree) ⇒ Object
Create Array of TreeNodes from the parameter generated by draggable_element JS helper.
-
.to_hash(array) ⇒ Object
Convert an array of ids to the required hash format.
Instance Method Summary collapse
-
#initialize(sort_order) ⇒ TreeMoveCalculator
constructor
Calculate the left, right and parent values from the parameter generated by draggable_element JS helper.
- #left_of(target_id) ⇒ Object
- #parent_of(val_to_find) ⇒ Object
- #placement_of(target_id) ⇒ Object
- #right_of(target_id) ⇒ Object
Constructor Details
#initialize(sort_order) ⇒ TreeMoveCalculator
Calculate the left, right and parent values from the parameter generated by draggable_element JS helper
10 11 12 |
# File 'lib/tree_calc.rb', line 10 def initialize(sort_order) @nodes = TreeMoveCalculator.create_tree_nodes(sort_order) end |
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
6 7 8 |
# File 'lib/tree_calc.rb', line 6 def nodes @nodes end |
Class Method Details
.create_tree_nodes(tree) ⇒ Object
Create Array of TreeNodes from the parameter generated by draggable_element JS helper.
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tree_calc.rb', line 54 def self.create_tree_nodes(tree) tree = to_hash(tree) if tree.is_a?(Array) result = [] tree.keys.sort.each do |key| node = TreeNode.new(tree[key]["id"].to_i) node.children = create_tree_nodes( tree[key].reject {|k, v| k == "id"} ) if tree[key].many? result << node end return result end |
.to_hash(array) ⇒ Object
Convert an array of ids to the required hash format.
69 70 71 72 73 74 75 |
# File 'lib/tree_calc.rb', line 69 def self.to_hash(array) result = {} array.each_with_index {|id, i| result[i.to_s] = {"id" => id.to_s } } return result end |
Instance Method Details
#left_of(target_id) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/tree_calc.rb', line 32 def left_of(target_id) array_with_target_node = find_array_with_target_node(target_id) if (array_with_target_node.first.id == target_id) return nil else return array_with_target_node[array_with_target_node.collect(&:id).index(target_id) - 1].id end end |
#parent_of(val_to_find) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tree_calc.rb', line 42 def parent_of(val_to_find) return nil if @nodes.collect(&:id).include?(val_to_find) parent = nil @nodes.each do |node| parent = find_parent_of(val_to_find, node) return parent if parent end end |
#placement_of(target_id) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/tree_calc.rb', line 24 def placement_of(target_id) # Note that this swaps left and right as the consumer views it differently. {:parent => parent_of(target_id), :move_to_right_of => left_of(target_id), :move_to_left_of => right_of(target_id) } end |
#right_of(target_id) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/tree_calc.rb', line 14 def right_of(target_id) array_with_target_node = find_array_with_target_node(target_id) if (array_with_target_node.last.id == target_id) return nil else return array_with_target_node[array_with_target_node.collect(&:id).index(target_id) + 1].id end end |