Class: Page

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.tag_countsObject



80
81
82
83
# File 'app/models/page.rb', line 80

def self.tag_counts
  Tag.select("tags.*, count(taggings.tag_id) as count").
    joins(:taggings).group("taggings.tag_id")
end

.tagged_with(name) ⇒ Object

tagging related methods



76
77
78
# File 'app/models/page.rb', line 76

def self.tagged_with(name)
  Tag.find_by_name!(name).pages
end

.tkh_search_indexable_fieldsObject



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

def self.tkh_search_indexable_fields
  indexable_fields = {
    title: 8,
    description: 3,
    body: 2
  }
end

Instance Method Details

#author_nameObject



107
108
109
# File 'app/models/page.rb', line 107

def author_name
  author.try(:formal_name)
end

#author_name=(formal_name) ⇒ Object



110
111
112
113
114
115
# File 'app/models/page.rb', line 110

def author_name=(formal_name)
  name_as_array = formal_name.split(',')
  last_name = name_as_array[0].strip
  first_name = name_as_array[1].strip
  self.author_id = User.where("last_name = ? AND first_name = ?", last_name, first_name).first.id
end

#childrenObject



58
59
60
# File 'app/models/page.rb', line 58

def children
  Page.published.with_parent_id(id)
end

#has_children?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/page.rb', line 54

def has_children?
  Page.with_parent_id(id).published.count >= 1
end

#has_siblings?Boolean

Returns:

  • (Boolean)


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

def has_siblings?
  Page.with_parent_id(parent_id).published.count >= 1
end

#nicknameObject



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

def nickname
  @nickname ||= short_title || title
end

#orphan?Boolean

menu related instance methods

Returns:

  • (Boolean)


50
51
52
# File 'app/models/page.rb', line 50

def orphan?
  parent_id == nil
end

#parentObject



62
63
64
# File 'app/models/page.rb', line 62

def parent
  Page.find(parent_id)
end

#parent_page_titleObject

autocomplete related instance methods



96
97
98
# File 'app/models/page.rb', line 96

def parent_page_title
  parent.try(:title) unless self.orphan?
end

#parent_page_title=(title) ⇒ Object



99
100
101
102
103
104
105
# File 'app/models/page.rb', line 99

def parent_page_title=(title)
  if title.present? && Page.where(title: title)
    self.parent_id = Page.where(title: title).first.id
  else
    self.parent_id = nil
  end
end

#published?Boolean

Returns:

  • (Boolean)


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

def published?
  published_at.present? ? true : false
end

#siblingsObject



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

def siblings
  Page.published.with_parent_id(parent_id)
end

#tag_listObject



85
86
87
# File 'app/models/page.rb', line 85

def tag_list
  tags.map(&:name).join(" ")
end

#tag_list=(names) ⇒ Object



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

def tag_list=(names)
  self.tags = names.split(" ").map do |n|
    Tag.where(name: n.strip).first_or_create!
  end
end

#to_paramObject



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

def to_param
  title ? "#{id}-#{title.to_url}" : id
end