Class: Redde::TreeSortable::Sorter
- Inherits:
-
Object
- Object
- Redde::TreeSortable::Sorter
- Defined in:
- app/models/concerns/redde/tree_sortable.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#list ⇒ Object
Returns the value of attribute list.
Instance Method Summary collapse
-
#initialize(list, klass) ⇒ Sorter
constructor
A new instance of Sorter.
- #sort ⇒ Object
- #tree ⇒ Object
Constructor Details
#initialize(list, klass) ⇒ Sorter
Returns a new instance of Sorter.
8 9 10 11 |
# File 'app/models/concerns/redde/tree_sortable.rb', line 8 def initialize(list, klass) @list = list @klass = klass end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
7 8 9 |
# File 'app/models/concerns/redde/tree_sortable.rb', line 7 def klass @klass end |
#list ⇒ Object
Returns the value of attribute list.
7 8 9 |
# File 'app/models/concerns/redde/tree_sortable.rb', line 7 def list @list end |
Instance Method Details
#sort ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/models/concerns/redde/tree_sortable.rb', line 13 def sort tree.each do |parent, cats| i = 1 if parent == 'root' pid = nil else pid = klass.find(parent).id end cats.each do |cat| klass.find(cat).update(parent_id: pid, position: i) i += 1 end end end |
#tree ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/concerns/redde/tree_sortable.rb', line 28 def tree temp = {} list.each do |key, value| if temp[value] temp[value] << key else temp[value] = [key] end end temp end |