Sha256: 7e10230fa1ab608c802b011f7826291f65a78a473159f50def1588236ac299e3
Contents?: true
Size: 1.72 KB
Versions: 2
Compression:
Stored size: 1.72 KB
Contents
# Methods for finding records on the filesystem. module ActiveCopy module Finders extend ActiveSupport::Concern # Test if the query matches this particular model. def matches? query query.reduce(true) do |matches, (key, value)| matches = if key == 'tag' return false unless tags.present? tags.include? value else attributes[key] == value end end end module ClassMethods # Return the folder where all documents are stored for this model. def collection_path @collection_path ||= "#{ActiveCopy.content_path}/#{name.tableize}/content" end # Find this model by its filename. def find by_filename if File.exists? "#{Rails.root}/#{collection_path}/#{by_filename}.md" new id: by_filename else nil end end # Read all files from the +collection_path+, then instantiate them # as members of this model. Return as an +Array+. def all Dir["#{absolute_collection_path}/*.md"].reduce([]) do |articles, md_path| unless md_path == "#{Rails.root}/#{collection_path}" file_name = File.basename(md_path).gsub('.md', '') articles << self.new(id: file_name) end end end # Look for all of the matching key/value pairs in the YAML front # matter, and return an array of models that match them. def where query={} all.reject { |a| a.nil? }.reduce([]) do |results, article| results << article if article.matches? query results end end private def absolute_collection_path "#{Rails.root}/#{collection_path}" end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
active_copy-1.0.0 | lib/active_copy/finders.rb |
active_copy-1.0.0.pre | lib/active_copy/finders.rb |