Class: Schnitzelpress::Post

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/schnitzelpress/post.rb

Instance Method Summary collapse

Instance Method Details

#article_post?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/schnitzelpress/post.rb', line 149

def article_post?
  link.nil?
end

#dayObject



161
162
163
# File 'lib/schnitzelpress/post.rb', line 161

def day
  published_at.day
end

#disqus?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/schnitzelpress/post.rb', line 177

def disqus?
  disqus && published?
end

#disqus_identifierObject



61
62
63
# File 'lib/schnitzelpress/post.rb', line 61

def disqus_identifier
  "post-#{id}"
end

#draft?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/schnitzelpress/post.rb', line 141

def draft?
  status == :draft
end

#home_page?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/schnitzelpress/post.rb', line 165

def home_page?
  slug == 'home'
end

Returns:

  • (Boolean)


145
146
147
# File 'lib/schnitzelpress/post.rb', line 145

def link_post?
  link.present?
end

#monthObject



157
158
159
# File 'lib/schnitzelpress/post.rb', line 157

def month
  published_at.month
end

#nil_if_blankObject



102
103
104
105
106
107
# File 'lib/schnitzelpress/post.rb', line 102

def nil_if_blank
  attributes.keys.each do |attr|
    self[attr].strip! if self[attr].is_a?(String)
    self[attr] = nil  if self[attr] == ""
  end
end

#page?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/schnitzelpress/post.rb', line 133

def page?
  !post?
end

#post?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/schnitzelpress/post.rb', line 129

def post?
  published_at.present?
end

#previous_slugsObject



69
70
71
# File 'lib/schnitzelpress/post.rb', line 69

def previous_slugs
  slugs[0..-2]
end

#published?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/schnitzelpress/post.rb', line 137

def published?
  status == :published
end

#published_at=(v) ⇒ Object



73
74
75
76
# File 'lib/schnitzelpress/post.rb', line 73

def published_at=(v)
  v = Chronic.parse(v) if v.is_a?(String)
  super(v)
end

#renderObject



122
123
124
125
126
127
# File 'lib/schnitzelpress/post.rb', line 122

def render
  @@markdown ||= Redcarpet::Markdown.new(MarkdownRenderer,
    :autolink => true, :space_after_headers => true, :fenced_code_blocks => true)

  @@markdown.render(body.to_s)
end

#set_defaultsObject



85
86
87
88
89
# File 'lib/schnitzelpress/post.rb', line 85

def set_defaults
  if slug.blank?
    self.slug = (title || body.truncate(40, :separator => ' ')).parameterize
  end
end

#slugObject



65
66
67
# File 'lib/schnitzelpress/post.rb', line 65

def slug
  slugs.try(:last)
end

#slug=(v) ⇒ Object



78
79
80
81
82
83
# File 'lib/schnitzelpress/post.rb', line 78

def slug=(v)
  unless v.blank?
    slugs.delete(v)
    slugs << v
  end
end

#to_htmlObject



113
114
115
116
117
118
119
120
# File 'lib/schnitzelpress/post.rb', line 113

def to_html
  if body_html.nil?
    update_body_html
    save
  end

  body_html
end

#to_urlObject



169
170
171
172
173
174
175
# File 'lib/schnitzelpress/post.rb', line 169

def to_url
  if home_page?
    '/'
  else
    published_at.present? ? "/#{sprintf '%04d', year}/#{sprintf '%02d', month}/#{sprintf '%02d', day}/#{slug}/" : "/#{slug}/"
  end
end

#update_body_htmlObject



109
110
111
# File 'lib/schnitzelpress/post.rb', line 109

def update_body_html
  self.body_html = render
end

#validate_slugObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/schnitzelpress/post.rb', line 91

def validate_slug
  conflicting_posts = Post.where(:slugs => slug)
  if published_at.present?
    conflicting_posts = conflicting_posts.for_day(published_at.year, published_at.month, published_at.day)
  end

  if conflicting_posts.any? && conflicting_posts.first != self
    errors[:slug] = "This slug is already in use by another post."
  end
end

#yearObject



153
154
155
# File 'lib/schnitzelpress/post.rb', line 153

def year
  published_at.year
end