Class: Page

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

Overview

this is needed for now to make mass assignment security compatible with the translation of globalize3 Globalize::ActiveRecord::Translation.class_eval do

attr_accessible :locale

end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.tag_countsObject



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

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



68
69
70
# File 'app/models/page.rb', line 68

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

Instance Method Details

#author_nameObject



99
100
101
# File 'app/models/page.rb', line 99

def author_name
  author.try(:formal_name)
end

#author_name=(formal_name) ⇒ Object



102
103
104
105
106
107
# File 'app/models/page.rb', line 102

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



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

def children
  Page.published.with_parent_id(id)
end

#has_children?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/page.rb', line 46

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

#has_siblings?Boolean

Returns:

  • (Boolean)


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

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

#nicknameObject



36
37
38
# File 'app/models/page.rb', line 36

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

#orphan?Boolean

menu related instance methods

Returns:

  • (Boolean)


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

def orphan?
  parent_id == nil
end

#parentObject



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

def parent
  Page.find(parent_id)
end

#parent_page_titleObject

autocomplete related instance methods



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

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

#parent_page_title=(title) ⇒ Object



91
92
93
94
95
96
97
# File 'app/models/page.rb', line 91

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

#siblingsObject



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

def siblings
  Page.published.with_parent_id(parent_id)
end

#tag_listObject



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

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

#tag_list=(names) ⇒ Object



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

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

#to_paramObject



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

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