Class: Article

Inherits:
FileModel show all
Defined in:
app/models/article.rb

Constant Summary collapse

@@tag_index =

Store list of articles for each tag in a hash Fast enough for the small number of articles we’re dealing with

{}

Instance Attribute Summary

Attributes inherited from FileModel

#body, #file, #key, #meta

Class Method Summary collapse

Methods inherited from FileModel

all, content_path, count, files, find, from_file, #index, #initialize, #method_missing, #next, #previous, random, relative_path

Constructor Details

This class inherits a constructor from FileModel

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FileModel

Class Method Details

.archive_monthsObject



29
30
31
# File 'app/models/article.rb', line 29

def self.archive_months
  @@archive_months ||= files.map{|f| File.basename(f).split('-')[0..1] }.uniq
end

.find_articles(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/article.rb', line 3

def self.find_articles(options = {})
  options = {
    :per_page => 10,
    :page => 1
  }.merge(options)
  
  options[:offset] = (options[:page]-1) * options[:per_page]
  options[:limit] = options[:per_page]

  if options[:year] and options[:month]
    options[:match] = %r(\/#{options[:year]}-#{options[:month].to_s.rjust(2,'0')})
  end

  all(options)
end

.recent(number = 5) ⇒ Object



19
20
21
# File 'app/models/article.rb', line 19

def self.recent(number = 5)
  @@recent ||= all(:limit => number)
end

.tag_indexObject



36
37
38
39
40
41
42
43
44
45
# File 'app/models/article.rb', line 36

def self.tag_index
  return @@tag_index if @@tag_index.present?
  all.each do |a| 
   a.tags.split(',').map(&:strip).each do |t| 
     @@tag_index[t] ||= []
     @@tag_index[t] = (@@tag_index[t] << a.key).uniq
    end
  end
  @@tag_index
end

.tagged(tag) ⇒ Object



23
24
25
26
27
# File 'app/models/article.rb', line 23

def self.tagged(tag) 
  tag_index[tag.strip].to_a.map do |key|
    Article.find(key)
  end
end