Class: Village::Article

Inherits:
FileModel show all
Includes:
Gravtastic
Defined in:
lib/village/article.rb

Defined Under Namespace

Classes: Sanitizer

Constant Summary collapse

CONTENT_PATH =
"app/views/articles"
TagHelper =
Class.new.extend ActionView::Helpers::TagHelper

Instance Attribute Summary

Attributes inherited from FileModel

#content_path, #extension, #folders, #slug

Attributes included from Attributes

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileModel

#content_html, create_class_methods_on, #to_key, #to_s

Constructor Details

#initialize(path) ⇒ Article

Returns a new instance of Article.



79
80
81
82
83
# File 'lib/village/article.rb', line 79

def initialize(path)
  super path
  attributes[:permalink_format] = Village::Config.permalink_format
  @date_str, @slug = File.basename(path).match(/^(\d+-\d+-\d+)-(.*)(\.[^.]+)$/).captures
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Village::Attributes

Class Method Details

.categoriesObject



34
35
36
37
38
39
40
41
42
# File 'lib/village/article.rb', line 34

def categories
  @categories ||= all.map do |article| 
    article.categories if article.attributes[:categories].present?
  end.compact.flatten.inject(Hash.new(0)) do |h,e| 
    h[e] += 1; h
  end.inject({}) do |r, e| 
    r[e.first] = e.last; r
  end
end

.feedObject



64
65
66
# File 'lib/village/article.rb', line 64

def feed
  order.first(Village::Config.page_size)
end

.feed_last_modifiedObject



68
69
70
# File 'lib/village/article.rb', line 68

def feed_last_modified
  feed.first.try(:last_modified) || Time.now.utc
end

.orderObject



20
21
22
# File 'lib/village/article.rb', line 20

def order
  all.select(&:visible?).sort_by(&:date).reverse
end

.reset!Object



72
73
74
75
76
# File 'lib/village/article.rb', line 72

def reset!
  @files = nil
  @tags = nil
  @categories = nil
end

.tagsObject



24
25
26
27
28
29
30
31
32
# File 'lib/village/article.rb', line 24

def tags
  @tags ||= all.map do |article| 
    article.tags if article.attributes[:tags].present?
  end.compact.flatten.inject(Hash.new(0)) do |h,e| 
    h[e] += 1; h
  end.inject({}) do |r, e| 
    r[e.first] = e.last; r
  end
end

.where(conditions = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/village/article.rb', line 44

def where(conditions = {})
  conditions = conditions.symbolize_keys
  conditions.assert_valid_keys :year, :month, :day, :slug, :to_param, :tag, :category
  [:year, :month, :day].each do |key|
    conditions[key] = conditions[key].to_i if conditions[key].present?
  end
  order.select do |article|
    conditions.all? do |key, value| 
      case key
      when :tag
        article.tags.include?(value) if article.attributes[:tags].present?
      when :category
        article.categories.include?(value) if article.attributes[:categories].present?
      else
        article.send(key) == value
      end
    end
  end
end

Instance Method Details

#dateObject



85
86
87
# File 'lib/village/article.rb', line 85

def date
  @date ||= Time.zone.parse(attributes[:date] || @date_str).to_date
end

#emailObject



89
90
91
92
93
# File 'lib/village/article.rb', line 89

def email
  if attributes[:author] && attributes[:author][:email]
    attributes[:author][:email]
  end
end

#summary_htmlObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/village/article.rb', line 104

def summary_html
  if attributes[:summary].present?
    TagHelper. :p, summary
  else
    html = Sanitizer.new.sanitize(content_html)
    doc = Nokogiri::HTML.fragment(html)
    para = doc.search('p').detect { |p| p.text.present? }
    para.try(:to_html).try(:html_safe)
  end
end

#timestampObject Also known as: last_modified



95
96
97
# File 'lib/village/article.rb', line 95

def timestamp
  date.to_time_in_current_zone
end

#to_paramObject



115
116
117
118
119
120
121
122
# File 'lib/village/article.rb', line 115

def to_param
  case attributes[:permalink_format]
  when :day   then "%04d/%02d/%02d/%s" % [year, month, day, slug]
  when :month then "%04d/%02d/%s" % [year, month, slug]
  when :year  then "%04d/%s" % [year, slug]
  when :slug  then slug
  end
end

#visible?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/village/article.rb', line 100

def visible?
  timestamp <= Time.zone.now
end