Class: Nesta::Page

Inherits:
FileModel show all
Defined in:
lib/nesta/models.rb

Constant Summary

Constants inherited from FileModel

FileModel::FORMATS

Instance Attribute Summary

Attributes inherited from FileModel

#filename, #mtime

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileModel

#==, #abspath, #description, find_file_for_path, #flagged_as?, #index_page?, #initialize, #keywords, #last_modified, #layout, load, #metadata, needs_loading?, #parse_metadata, #path, #permalink, purge_cache, #template, #to_html

Constructor Details

This class inherits a constructor from Nesta::FileModel

Class Method Details

.find_allObject


207
208
209
# File 'lib/nesta/models.rb', line 207

def self.find_all
  super.select { |p| ! p.hidden? }
end

.find_articlesObject


211
212
213
214
215
# File 'lib/nesta/models.rb', line 211

def self.find_articles
  find_all.select do |page|
    page.date && page.date < DateTime.now
  end.sort { |x, y| y.date <=> x.date }
end

.find_by_path(path) ⇒ Object


202
203
204
205
# File 'lib/nesta/models.rb', line 202

def self.find_by_path(path)
  page = load(path)
  page && page.hidden? ? nil : page
end

.model_path(basename = nil) ⇒ Object


198
199
200
# File 'lib/nesta/models.rb', line 198

def self.model_path(basename = nil)
  Nesta::Config.page_path(basename)
end

Instance Method Details

#articlesObject


332
333
334
# File 'lib/nesta/models.rb', line 332

def articles
  Page.find_articles.select { |article| article.categories.include?(self) }
end

#atom_idObject


261
262
263
# File 'lib/nesta/models.rb', line 261

def atom_id
  ('atom id')
end

#body(scope = Object.new) ⇒ Object


287
288
289
# File 'lib/nesta/models.rb', line 287

def body(scope = Object.new)
  convert_to_html(@format, scope, body_markup)
end

#body_markupObject


276
277
278
279
280
281
282
283
284
285
# File 'lib/nesta/models.rb', line 276

def body_markup
  case @format
    when :mdown, :md
      markup.sub(/^#[^#].*$\r?\n(\r?\n)?/, '')
    when :haml
      markup.sub(/^\s*%h1\s+.*$\r?\n(\r?\n)?/, '')
    when :textile
      markup.sub(/^\s*h1\.\s+.*$\r?\n(\r?\n)?/, '')
  end
end

#categoriesObject


291
292
293
294
# File 'lib/nesta/models.rb', line 291

def categories
  paths = category_strings.map { |specifier| specifier.sub(/:-?\d+$/, '') }
  valid_paths(paths).map { |p| Page.find_by_path(p) }
end

#date(format = nil) ⇒ Object


251
252
253
254
255
256
257
258
259
# File 'lib/nesta/models.rb', line 251

def date(format = nil)
  @date ||= if ("date")
    if format == :xmlschema
      Time.parse(("date")).xmlschema
    else
      DateTime.parse(("date"))
    end
  end
end

#draft?Boolean


217
218
219
# File 'lib/nesta/models.rb', line 217

def draft?
  flagged_as?('draft')
end

#headingObject


225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/nesta/models.rb', line 225

def heading
  regex = case @format
    when :mdown, :md
      /^#\s*(.*?)(\s*#+|$)/
    when :haml
      /^\s*%h1\s+(.*)/
    when :textile
      /^\s*h1\.\s+(.*)/
    end
  markup =~ regex
  Regexp.last_match(1) or raise HeadingNotSet, "#{abspath} needs a heading"
end

#hidden?Boolean


221
222
223
# File 'lib/nesta/models.rb', line 221

def hidden?
  draft? && Nesta::App.production?
end

238
239
240
241
242
# File 'lib/nesta/models.rb', line 238

def link_text
  ('link text') || heading
rescue HeadingNotSet
  raise LinkTextNotSet, "Need to link to '#{abspath}' but can't get link text"
end

#pagesObject


318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/nesta/models.rb', line 318

def pages
  in_category = Page.find_all.select do |page|
    page.date.nil? && page.categories.include?(self)
  end
  in_category.sort do |x, y|
    by_priority = y.priority(path) <=> x.priority(path)
    if by_priority == 0
      x.link_text.downcase <=> y.link_text.downcase
    else
      by_priority
    end
  end
end

#parentObject


303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/nesta/models.rb', line 303

def parent
  if abspath == '/'
    nil
  else
    parent_path = File.dirname(path)
    while parent_path != '.' do
      parent = Page.load(parent_path)
      return parent unless parent.nil?
      parent_path = File.dirname(parent_path)
    end
    return categories.first unless categories.empty?
    Page.load('index')
  end
end

#priority(category) ⇒ Object


296
297
298
299
300
301
# File 'lib/nesta/models.rb', line 296

def priority(category)
  category_string = category_strings.detect do |string|
    string =~ /^#{category}([,:\s]|$)/
  end
  category_string && category_string.split(':', 2)[-1].to_i
end

#read_moreObject


265
266
267
# File 'lib/nesta/models.rb', line 265

def read_more
  ('read more') || Nesta::Config.read_more
end

#receives_comments?Boolean


336
337
338
# File 'lib/nesta/models.rb', line 336

def receives_comments?
  ! date.nil?
end

#summaryObject


269
270
271
272
273
274
# File 'lib/nesta/models.rb', line 269

def summary
  if summary_text = ("summary")
    summary_text.gsub!('\n', "\n")
    convert_to_html(@format, Object.new, summary_text)
  end
end

#titleObject


244
245
246
247
248
249
# File 'lib/nesta/models.rb', line 244

def title
  ('title') || link_text
rescue LinkTextNotSet
  return Nesta::Config.title if abspath == '/'
  raise
end