Class: Page

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#controller_actionObject

Returns the value of attribute controller_action.



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

def controller_action
  @controller_action
end

#no_publish_windowObject

Returns the value of attribute no_publish_window.



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

def no_publish_window
  @no_publish_window
end

Class Method Details



156
157
158
159
# File 'app/models/page.rb', line 156

def breadcrumbs_for(user, url)
  root = Page.published.viewable_by(user).find_by_url(url)
  root.nil? ? [] : root.self_and_ancestors
end

.default_layoutObject



161
162
163
# File 'app/models/page.rb', line 161

def default_layout
  PageEngine.layouts.first
end

.viewable_by(user) ⇒ Object

Scopes



167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/models/page.rb', line 167

def viewable_by(user)
  if PageEngine.class_exists?('Role') && PageEngine.class_exists?('User')
    if user
      # This is a bit of a kludge until I can figure out how to get it to work properly in a single sql query
      includes(:page_roles).where("pages.id in (?) or (page_roles.required_role_id is null and page_roles.excluded_role_id is null)", PageRole.viewable_page_ids_for(user))
    else
      includes(:page_roles).where({ 'page_roles.required_role_id' => nil })
    end
  else
    scoped
  end      
end

.with_roles(roles) ⇒ Object



180
181
182
183
184
185
186
# File 'app/models/page.rb', line 180

def with_roles(roles)
  if Extras.class_exists?('Role')
    includes(:roles).where([ "roles.id IN (?)", roles.join(',') ])
  else
    scoped
  end
end

.with_url(request, params) ⇒ Object



188
189
190
191
192
193
# File 'app/models/page.rb', line 188

def with_url(request, params)
  url = request.fullpath
  url.gsub!(/\?.*/, '') # Strip away anything after the ? as it's not needed

  where(["pages.permalink = ? or url = ? or (controller = ? and action = ?)", params[:permalink], url, params[:controller], params[:action]])
end

Instance Method Details

#css_statusObject



140
141
142
143
# File 'app/models/page.rb', line 140

def css_status
  return "live" if self.published?
  status.downcase
end

#duplicateObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/page.rb', line 87

def duplicate
  page = Page.new(self.attributes)
  page.title += " (copy) #{Time.now.strftime('%Y%m%d_%H%M%S')}"
  page.permalink = page.permalink + "-copy-#{Time.now.strftime('%Y%m%d-%H%M%S')}"
  page.save
  page.move_to_right_of self

  # Duplicate each of the objects associated with the original
  # Page parts
  self.page_parts.each do |page_part|
    page.page_parts << page_part.duplicate
  end

  # Roles
  if PageEngine.class_exists?('Role')
    page.required_roles = self.required_roles 
    page.excluded_roles = self.excluded_roles
  end

  # Assets
  if PageEngine.class_exists?('Asset')
    self.attachables.each do |attachable|
      page.attachables << attachable.duplicate
    end
  end

  page
end

#is_child_of?(page) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_child_of? page
  self.parent_id == page.id ? true : false
end

#is_parent_of?(page) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_parent_of? page
  self.id == page.parent_id ? true : false
end

#is_viewable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/page.rb', line 116

def is_viewable_by?(user)
  if PageEngine.class_exists?('Role')
    if PageEngine.class_exists?('User') && user
      return true if self.roles.length == 0
      self.role_ids.length != (self.role_ids - user.role_ids.uniq).length        
    else
      self.roles.length == 0
    end
  else
    true
  end
end

#lft=(x) ⇒ Object

Override the protected methods of awesome_nested_set so lft and rgt can be set



146
147
148
# File 'app/models/page.rb', line 146

def lft=(x)
  self[:lft] = x    
end

#move_children_to_parentObject



129
130
131
132
133
134
135
136
137
138
# File 'app/models/page.rb', line 129

def move_children_to_parent
  self.children.each do |child|
    if self.parent
      child.move_to_child_of self.parent
    else
      child.move_to_root
    end
  end
  self.reload
end

#no_publish_window_set?Boolean

Returns:

  • (Boolean)


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

def no_publish_window_set?
  self.publish_from.nil? && self.publish_to.nil?
end

#number_of_childrenObject



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

def number_of_children
  (self.rgt - self.lft - 1) / 2
end

#published?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'app/models/page.rb', line 63

def published?
  if self.no_publish_window_set?
    self.status == "Published"
  else
    self.publish_from < DateTime.now && self.publish_to > DateTime.now && self.status == "Published"
  end
end

#rgt=(x) ⇒ Object



150
151
152
# File 'app/models/page.rb', line 150

def rgt=(x)
  self[:rgt] = x
end

#to_paramObject



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

def to_param
  permalink
end