Class: Post

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
Gravtastic
Defined in:
app/models/post.rb

Defined Under Namespace

Classes: Sanitizer

Constant Summary collapse

FILENAME_FORMAT =
/^(\d+-\d+-\d+)-(.*)(\.[^.]+)$/
TagHelper =
Class.new.extend ActionView::Helpers::TagHelper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Post

Returns a new instance of Post.



11
12
13
14
# File 'app/models/post.rb', line 11

def initialize(path)
  @path = path
  @date_str, @slug = File.basename(path).match(FILENAME_FORMAT).captures
end

Instance Attribute Details

#slugObject (readonly)

Returns the value of attribute slug.



7
8
9
# File 'app/models/post.rb', line 7

def slug
  @slug
end

Class Method Details

.allObject



96
97
98
99
100
101
# File 'app/models/post.rb', line 96

def all
  file_extensions = Postmarkdown::Config.options[:markdown_file_extensions].join(',')
  @@posts ||= Dir.glob(Rails.root + "app/posts/*.{#{file_extensions}}").map do |filename|
    Post.new filename
  end.select(&:visible?).sort_by(&:date).reverse
end

.feedObject



126
127
128
# File 'app/models/post.rb', line 126

def feed
  all.first(10)
end

.feed_last_modifiedObject



130
131
132
# File 'app/models/post.rb', line 130

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

.find(id) ⇒ Object



114
115
116
# File 'app/models/post.rb', line 114

def find(id)
  where(:to_param => id).first or raise ActiveRecord::RecordNotFound, "Could not find post with ID #{id.inspect}"
end

.firstObject



118
119
120
# File 'app/models/post.rb', line 118

def first
  all.first
end

.lastObject



122
123
124
# File 'app/models/post.rb', line 122

def last
  all.last
end

.reset!Object



134
135
136
# File 'app/models/post.rb', line 134

def reset!
  @@posts = nil
end

.where(conditions = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'app/models/post.rb', line 103

def where(conditions = {})
  conditions = conditions.symbolize_keys
  conditions.assert_valid_keys :year, :month, :day, :slug, :to_param
  [:year, :month, :day].each do |key|
    conditions[key] = conditions[key].to_i if conditions[key].present?
  end
  all.select do |post|
    conditions.all? { |key, value| post.send(key) == value }
  end
end

Instance Method Details

#authorObject



47
48
49
# File 'app/models/post.rb', line 47

def author
  [:author]
end

#contentObject



38
39
40
41
# File 'app/models/post.rb', line 38

def content
  load_content
  @content
end

#content_htmlObject



74
75
76
# File 'app/models/post.rb', line 74

def content_html
  RDiscount.new(content).to_html.html_safe
end

#dateObject



55
56
57
# File 'app/models/post.rb', line 55

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

#emailObject



51
52
53
# File 'app/models/post.rb', line 51

def email
  [:email]
end

#metadataObject



33
34
35
36
# File 'app/models/post.rb', line 33

def 
  load_content
  @metadata
end


25
26
27
# File 'app/models/post.rb', line 25

def permalink_format
  Postmarkdown::Config.options[:permalink_format]
end

#summary_htmlObject



84
85
86
87
88
89
90
91
92
93
# File 'app/models/post.rb', line 84

def summary_html
  if [: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



61
62
63
# File 'app/models/post.rb', line 61

def timestamp
  date.to_time_in_current_zone
end

#titleObject



43
44
45
# File 'app/models/post.rb', line 43

def title
  [:title] || slug.titleize
end

#to_keyObject



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

def to_key
  [slug]
end

#to_paramObject



16
17
18
19
20
21
22
23
# File 'app/models/post.rb', line 16

def to_param
  case 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

#to_sObject



70
71
72
# File 'app/models/post.rb', line 70

def to_s
  "#{title.inspect} (#{slug})"
end

#visible?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/post.rb', line 66

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