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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for the plugin. i am trying it out, but this is throwing an error when put into the plugins directory:
i am new with Ruby, but putting an
end
before theRuhoh::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