lib/trestle/navigation.rb in trestle-0.8.10 vs lib/trestle/navigation.rb in trestle-0.8.11
- old
+ new
@@ -12,11 +12,14 @@
def initialize(items)
@items = items
end
def by_group
- Hash[stable_sort(items.group_by(&:group)).map { |group, items| [group, stable_sort(items)] }]
+ sorted_groups = stable_sort(items.group_by { |item| groups[item.group.id] })
+ sorted_items = sorted_groups.map { |group, items| [group, stable_sort(items)] }
+
+ Hash[sorted_items]
end
def each(&block)
by_group.each(&block)
end
@@ -35,8 +38,17 @@
end
private
def stable_sort(items)
items.sort_by.with_index { |item, i| [item, i] }
+ end
+
+ def groups
+ @groups ||= items.inject({}) { |groups, item|
+ group = groups[item.group.id]
+
+ groups[item.group.id] = group ? group.merge(item.group) : item.group
+ groups
+ }
end
end
end