Class: Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Entity, Lockable, Postable
Defined in:
app/models/page.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_page(params) ⇒ Object



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

def create_page(params)
  path = params[:path].downcase
  raise "Invalid path" unless path =~ /^[-_a-z0-9]+$/
  Page.create :title => params[:title], :path => path, :content => params[:content], :locked => true
end

.from_path(path) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


88
89
90
91
92
# File 'app/models/page.rb', line 88

def from_path(path)
  page = posted.where(:path => path).first
  raise ActiveRecord::RecordNotFound.new("No page found!") unless page
  page
end

.in_sitemapObject



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

def in_sitemap
  where :in_sitemap => true
end

.orderedObject



78
79
80
# File 'app/models/page.rb', line 78

def ordered
  order "pages.title ASC"
end

.preview_from_path(path) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


82
83
84
85
86
# File 'app/models/page.rb', line 82

def preview_from_path(path)
  page = where(:path => path).first
  raise ActiveRecord::RecordNotFound.new("No page found!") unless page
  page
end

.search(query) ⇒ Object



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

def search(query)
  ordered.where "LOWER(title) LIKE :query OR LOWER(path) LIKE :query OR LOWER(content) LIKE :query", :query => "%#{query.downcase}%"
end

.sitemapObject



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

def sitemap
  posted.in_sitemap
end

.update_page(params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/page.rb', line 44

def update_page(params)
  path = params[:path].downcase
  raise "Invalid path" unless path =~ /^[-_a-z0-9]+$/
  page = Page.find params[:id].to_i
  page.ensure_unlocked!
  page.title = params[:title]
  page.path = path
  page.content = params[:content]
  page.locked = true
  page.comments = !!params[:comments]
  page.in_sitemap = !!params[:in_sitemap]

  if params[:posted]
    page.posted_at = Date.today
  else
    page.posted_at = nil
  end

  page.save!
  page
end

Instance Method Details

#edit_urlObject



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

def edit_url
  "/admin/page/#{id}/edit"
end

#has_comments?Boolean

Returns:

  • (Boolean)


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

def has_comments?
  comments
end

#in_sitemap?Boolean

Returns:

  • (Boolean)


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

def in_sitemap?
  in_sitemap
end

#preview_urlObject



21
22
23
# File 'app/models/page.rb', line 21

def preview_url
  "/admin/page/#{path}/preview"
end

#to_backup_entriesObject



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

def to_backup_entries
  page = Backup::Entry.new id, path, "markdown", content
  meta = Backup::Entry.new id, "#{path}.meta", "json", to_json(:except => :content)
  [page, meta]
end

#urlObject



17
18
19
# File 'app/models/page.rb', line 17

def url
  "/#{path}"
end