lib/trestle/navigation.rb in trestle-0.8.6 vs lib/trestle/navigation.rb in trestle-0.8.7
- old
+ new
@@ -5,16 +5,14 @@
autoload :Block
autoload :Item
autoload :Group
autoload :NullGroup, "trestle/navigation/group"
- def initialize(blocks)
- @blocks = blocks
- end
+ attr_reader :items
- def items
- @blocks.map(&:items).flatten
+ def initialize(items)
+ @items = items
end
def by_group
Hash[stable_sort(items.group_by(&:group)).map { |group, items| [group, stable_sort(items)] }]
end
@@ -24,9 +22,17 @@
end
def first
sorted = by_group.values
sorted.first.first if sorted.any?
+ end
+
+ def visible(context)
+ self.class.new(items.select { |item| item.visible?(context) })
+ end
+
+ def self.build(blocks)
+ new(blocks.map(&:items).flatten)
end
private
def stable_sort(items)
items.sort_by.with_index { |item, i| [item, i] }