Skip to content

Instantly share code, notes, and snippets.

@mkarklins
Last active August 31, 2017 09:51
Show Gist options
  • Save mkarklins/7842bec597c72ac24ca15bf581a10694 to your computer and use it in GitHub Desktop.
Save mkarklins/7842bec597c72ac24ca15bf581a10694 to your computer and use it in GitHub Desktop.
Non-deterministic navigation rendering
Rails = nil
require 'simple-navigation'
SimpleNavigation.config_file_paths << File.expand_path('../', __FILE__)
include SimpleNavigation::Helpers
threads = []
20.times do
threads << Thread.new do
puts render_navigation
end
end
threads.map(&:join)
require './navigation_renderer'
SimpleNavigation::Configuration.run do |navigation|
navigation.renderer = NavigationRenderer
navigation.items do |primary|
primary.item :books, 'Books', "#"
primary.item :music, 'Music', "#"
sleep 0.1 # This could be an ActiveRecord query retrieving renderable records.
primary.item :records, 'records', "#" do |secondary|
secondary.item :record_1, 'record_1', "#"
secondary.item :record_2, 'record_2', "#"
end
end
end
require 'simple-navigation'
class NavigationRenderer < SimpleNavigation::Renderer::Base
def render(item_container)
item_container.items.inject([]) do |list, item|
list << item.name
if include_sub_navigation?(item) || consider_sub_navigation?(item)
list << render_sub_navigation_for(item)
end
list
end.join(", ")
end
def tag_for(item)
item.name
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment