Module: ActiveCopy::Finders::ClassMethods
- Defined in:
- lib/active_copy/finders.rb
Instance Method Summary collapse
-
#all ⇒ Object
Read all files from the
collection_path
, then instantiate them as members of this model. -
#collection_path ⇒ Object
Return the folder where all documents are stored for this model.
-
#find(by_filename) ⇒ Object
Find this model by its filename.
-
#where(query = {}) ⇒ Object
Look for all of the matching key/value pairs in the YAML front matter, and return an array of models that match them.
Instance Method Details
#all ⇒ Object
Read all files from the collection_path
, then instantiate them as members of this model. Return as an Array
.
37 38 39 40 41 42 43 44 |
# File 'lib/active_copy/finders.rb', line 37 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 |
#collection_path ⇒ Object
Return the folder where all documents are stored for this model.
22 23 24 |
# File 'lib/active_copy/finders.rb', line 22 def collection_path @collection_path ||= "#{ActiveCopy.content_path}/#{name.tableize}/content" end |
#find(by_filename) ⇒ Object
Find this model by its filename.
27 28 29 30 31 32 33 |
# File 'lib/active_copy/finders.rb', line 27 def find by_filename if File.exists? "#{Rails.root}/#{collection_path}/#{by_filename}.md" new id: by_filename else nil end end |
#where(query = {}) ⇒ Object
Look for all of the matching key/value pairs in the YAML front matter, and return an array of models that match them.
48 49 50 51 52 53 |
# File 'lib/active_copy/finders.rb', line 48 def where query={} all.reject { |a| a.nil? }.reduce([]) do |results, article| results << article if article.matches? query results end end |