Skip to content

Instantly share code, notes, and snippets.

@plusjade
Created November 30, 2013 11:12
Ruhoh plugin to pragmatically filter pages in a pages collection by an arbitrary prefix. Useful for filtering into sub-directories of the pages collection.
# ./my-blog/plugins/pages_filter.rb
module PagesFilter
# Filter pages by custom prefix.
# The prefix string is compared against the id which is the relative filepath.
# This enables sub-directory filtering.
# Example:
# <ul>
# {{# docs.filter.2/widgets/ }}
# <li>{{ title }}</li>
# {{/ docs.filter.2/widgets/ }}
# </ul>
#
# The filter returns pages within the directory "2/widgets/" (of the docs collection).
def filter
@filter ||= Ruhoh::Views::Helpers::SimpleProxy.new({
matcher: /^[\w\-\/]+$/,
function: -> prefix {
ruhoh.cache.get("#{ resource_name }-#{ prefix }-all") ||
ruhoh.cache.set("#{ resource_name }-#{ prefix }-all", dictionary.each_value.find_all { |model|
model.id.start_with?(prefix) && File.basename(File.dirname(model.id)) != "drafts"
}.sort
)
}
})
end
Ruhoh::Resources::Pages::CollectionView.send(:include, PagesFilter)
@waynedpj
Copy link

thanks for the plugin. i am trying it out, but this is throwing an error when put into the plugins directory:

/home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/plugins/local_plugins_plugin.rb:7:in `require': /home/waynedpj/progetti/in-giro.org/sito/ruhoh/sorgente/plugins/pages_filter/pages_filter.rb:28: syntax error, unexpected $end, expecting keyword_end (SyntaxError)
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/plugins/local_plugins_plugin.rb:7:in `block (2 levels) in <class:LocalPluginsPlugin>'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/plugins/local_plugins_plugin.rb:7:in `each'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/plugins/local_plugins_plugin.rb:7:in `block in <class:LocalPluginsPlugin>'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/plugins/initializer.rb:12:in `instance_exec'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/plugins/initializer.rb:12:in `run'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/plugins/plugin.rb:11:in `block in run_all'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/plugins/plugin.rb:10:in `each'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/plugins/plugin.rb:10:in `run_all'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh.rb:87:in `setup_plugins'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/lib/ruhoh/client.rb:43:in `initialize'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/bin/ruhoh:41:in `new'
    from /home/waynedpj/progetti/ruhoh/sorgente/ruhoh.rb/bin/ruhoh:41:in `<top (required)>'
    from /home/waynedpj/.gem/ruby/1.9.1/bin/ruhoh:23:in `load'
    from /home/waynedpj/.gem/ruby/1.9.1/bin/ruhoh:23:in `<main>'

i am new with Ruby, but putting an end before the Ruhoh::Resources::Pages::CollectionView.send(:include, PagesFilter) line seems to fix the problem. however, i am unsure how to make a pull request for a gist, or if it is even possible?

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment