Class: Page

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/page.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_path(path) ⇒ Object



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

def self.find_by_path(path)
  # TODO: this gets called twice for all Page requests. should probably be more efficient.
  ancestors = path.split('/').reject { |s| s.empty? }
  result = Page.roots.find_by_slug(ancestors.shift)
  result = result.children.find_by_slug(ancestors.shift) while ancestors.any? && !!result
  result
end

.find_by_path!(path) ⇒ Object



19
20
21
22
23
24
25
# File 'app/models/page.rb', line 19

def self.find_by_path!(path)
  if result = Page.find_by_path(path)
    result
  else
    raise ActiveRecord::RecordNotFound, "Couldn't find Page with path = #{path}"
  end
end

.path_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.path_exists?(path)
  # TODO: if there's a more efficient way to do this, I should put it here
  Page.find_by_path(path)
end

Instance Method Details

#set_default_orderObject



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

def set_default_order
  self.order ||= siblings.any? ? siblings.order('"order" DESC').limit(1).first.order + 1 : 0
end

#should_generate_new_friendly_id?Boolean

Returns:

  • (Boolean)


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

def should_generate_new_friendly_id?
  slug.blank?
end

#urlObject



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

def url
  url = '/'
  url << ancestors.map { |a| a.slug }.join('/') << '/' if !is_root?
  url << slug
end