Class: Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Annotatable, StandardTags, TrustyCms::Taggable
Defined in:
app/models/page.rb

Direct Known Subclasses

FileNotFoundPage, RailsPage

Defined Under Namespace

Classes: MissingRootPageError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Annotatable

included

Methods included from TrustyCms::Taggable

included, #render_tag, #tag_descriptions, #tags, #warn_of_tag_deprecation

Instance Attribute Details

#pagination_parametersObject

Returns the value of attribute pagination_parameters.



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

def pagination_parameters
  @pagination_parameters
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

Class Method Details

.date_column_namesObject



273
274
275
# File 'app/models/page.rb', line 273

def date_column_names
  columns.collect { |c| c.name if c.sql_type =~ /(date|time)/ }.compact
end

.descendant_class(class_name) ⇒ Object

Raises:

  • (ArgumentError)


330
331
332
333
334
335
336
337
338
# File 'app/models/page.rb', line 330

def descendant_class(class_name)
  raise ArgumentError.new('argument must be a valid descendant of Page') unless is_descendant_class_name?(class_name)

  if ['', nil, 'Page'].include?(class_name)
    Page
  else
    class_name.constantize
  end
end

.display_name(string = nil) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'app/models/page.rb', line 277

def display_name(string = nil)
  if string
    @display_name = string
  else
    @display_name ||= begin
      n = name.to_s
      n.sub(/^(.+?)Page$/, '\1')
      n.gsub(/([A-Z])/, ' \1')
      n.strip
    end
  end
  @display_name = @display_name + ' - not installed' if missing? && @display_name !~ /not installed/
  @display_name
end

.display_name=(string) ⇒ Object



292
293
294
# File 'app/models/page.rb', line 292

def display_name=(string)
  display_name(string)
end

.find_by_path(path, can_view_drafts = false) ⇒ Object



267
268
269
270
271
# File 'app/models/page.rb', line 267

def find_by_path(path, can_view_drafts = false)
  raise MissingRootPageError unless root

  root.find_by_path(path, can_view_drafts)
end

.is_descendant_class_name?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


326
327
328
# File 'app/models/page.rb', line 326

def is_descendant_class_name?(class_name)
  (Page.descendants.map(&:to_s) + [nil, '', 'Page']).include?(class_name)
end

.load_subclassesObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'app/models/page.rb', line 296

def load_subclasses
  ([TRUSTY_CMS_ROOT] + TrustyCms::Extension.descendants.map(&:root)).each do |path|
    Dir["#{path}/app/models/*_page.rb"].each do |page|
      $1.camelize.constantize if page =~ %r{/([^/]+)\.rb}
    end
  end
  if database_exists?
    if ActiveRecord::Base.connection.data_sources.include?('pages') && Page.column_names.include?('class_name') # Assume that we have bootstrapped
      Page.connection.select_values("SELECT DISTINCT class_name FROM pages WHERE class_name <> '' AND class_name IS NOT NULL").each do |p|
        begin
          p.constantize
        rescue NameError, LoadError
          # Rubocop: The use of eval is a serious security risk.
          # eval(%Q{class #{p} < Page; acts_as_tree; def self.missing?; true end end}, TOPLEVEL_BINDING)
          Rails.logger.error NameError
        end
      end
    end
  end
end

.missing?Boolean

Returns:

  • (Boolean)


340
341
342
# File 'app/models/page.rb', line 340

def missing?
  false
end

.new_with_defaults(config = TrustyCms::Config) ⇒ Object



317
318
319
320
321
322
323
324
# File 'app/models/page.rb', line 317

def new_with_defaults(config = TrustyCms::Config)
  page = new
  page.parts.concat default_page_parts(config)
  page.fields.concat default_page_fields(config)
  default_status = config['defaults.page.status']
  page.status = Status[default_status] if default_status
  page
end

.parent_pages(homepage_id) ⇒ Object



168
169
170
# File 'app/models/page.rb', line 168

def self.parent_pages(homepage_id)
  where(parent_id: homepage_id).or(where(id: homepage_id))
end

.ransackable_attributes(auth_object = nil) ⇒ Object



164
165
166
# File 'app/models/page.rb', line 164

def self.ransackable_attributes(auth_object = nil)
  ['site_id', 'title', 'slug']
end

.rootObject



263
264
265
# File 'app/models/page.rb', line 263

def root
  find_by_parent_id(nil)
end

.save_order(new_position) ⇒ Object



155
156
157
158
159
160
161
162
# File 'app/models/page.rb', line 155

def self.save_order(new_position)
  dictionary = Hash[*new_position.flatten(1)]
  dictionary.each do |id, position|
    page = Page.find_by_id(id)
    page.position = position if page.position.present?
    page.save
  end
end

Instance Method Details

#allowed_children_lookupObject



247
248
249
# File 'app/models/page.rb', line 247

def allowed_children_lookup
  [default_child, *Page.descendants.sort_by(&:name)].uniq
end

#cache?Boolean

Returns:

  • (Boolean)


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

def cache?
  true
end

#child_path(child) ⇒ Object Also known as: child_url



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

def child_path(child)
  clean_path(path + '/' + child.slug)
end

#default_childObject



243
244
245
# File 'app/models/page.rb', line 243

def default_child
  self.class.default_child
end

#descriptionObject



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

def description
  self['description']
end

#description=(value) ⇒ Object



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

def description=(value)
  self['description'] = value
end

#field(name) ⇒ Object



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

def field(name)
  if new_record? || fields.any?(&:new_record?)
    fields.detect { |f| f.name.downcase == name.to_s.downcase }
  else
    fields.find_by_name name.to_s
  end
end

#find_by_path(path, can_view_drafts = false, clean = true) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/models/page.rb', line 210

def find_by_path(path, can_view_drafts = false, clean = true)
  return nil if virtual?

  path = clean_path(path) if clean
  my_path = self.path
  if (my_path == path) && (published? || hidden? || can_view_drafts)
    return self
  elsif path =~ /^#{Regexp.quote(my_path)}([^\/]*)/
    slug_child = children.find_by_slug($1)
    if slug_child
      found = slug_child.find_by_path(path, can_view_drafts, clean)
      return found if found
    end
    children.each do |child|
      found = child.find_by_path(path, can_view_drafts, clean)
      return found if found
    end
  end

  unless slug_child
    file_not_found_types = ([FileNotFoundPage] + FileNotFoundPage.descendants)
    file_not_found_names = file_not_found_types.collect { |x| x.name }
    condition = (['class_name = ?'] * file_not_found_names.length).join(' or ')
    condition = "status_id = #{Status[:published].id} and (#{condition})" unless can_view_drafts
    return children.where([condition] + file_not_found_names).first
  end
  slug_child
end

#handle_hidden_statusObject



255
256
257
258
259
260
# File 'app/models/page.rb', line 255

def handle_hidden_status
  return unless status_id == Status[:hidden].id
  return unless TrustyCms::Application.config.on_hidden_callback

  TrustyCms::Application.config.on_hidden_callback.call(self)
end

#has_or_inherits_part?(name) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/page.rb', line 93

def has_or_inherits_part?(name)
  part?(name) || inherits_part?(name)
end

#headersObject



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

def headers
  # Return a blank hash that child classes can override or merge
  {}
end

#hidden?Boolean

Returns:

  • (Boolean)


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

def hidden?
  status == Status[:hidden]
end

#inherits_part?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def inherits_part?(name)
  !part?(name) && ancestors.any? { |page| page.part?(name) }
end

#layout_with_inheritanceObject Also known as: layout



52
53
54
55
56
57
58
# File 'app/models/page.rb', line 52

def layout_with_inheritance
  if layout_without_inheritance
    layout_without_inheritance
  else
    parent.layout if parent?
  end
end

#part(name) ⇒ Object



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

def part(name)
  if new_record? || parts.to_a.any?(&:new_record?)
    parts.to_a.find { |p| p.name == name.to_s }
  else
    parts.find_by_name name.to_s
  end
end

#part?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def part?(name)
  !part(name).nil?
end

#pathObject Also known as: url



129
130
131
132
133
# File 'app/models/page.rb', line 129

def path
  return '' if slug.blank?

  parent.present? ? clean_path("#{parent.path}/#{slug}") : clean_path(slug)
end

#process(request, response) ⇒ Object



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

def process(request, response)
  @request = request
  @response = response
  set_response_headers(@response)
  @response.body = render
  @response.status = response_code
end

#published?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/models/page.rb', line 109

def published?
  status == Status[:published]
end

#renderObject



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

def render
  if layout
    parse_object(layout)
  else
    render_part(:body)
  end
end

#render_part(part_name) ⇒ Object



197
198
199
200
201
202
203
204
# File 'app/models/page.rb', line 197

def render_part(part_name)
  part = part(part_name)
  if part
    parse_object(part)
  else
    ''
  end
end

#render_snippet(snippet) ⇒ Object



206
207
208
# File 'app/models/page.rb', line 206

def render_snippet(snippet)
  parse_object(snippet)
end

#response_codeObject



185
186
187
# File 'app/models/page.rb', line 185

def response_code
  200
end

#scheduled?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'app/models/page.rb', line 117

def scheduled?
  status == Status[:scheduled]
end

#set_allowed_children_cacheObject



251
252
253
# File 'app/models/page.rb', line 251

def set_allowed_children_cache
  self.allowed_children_cache = allowed_children_lookup.collect(&:name).join(',')
end

#statusObject



121
122
123
# File 'app/models/page.rb', line 121

def status
  Status.find(status_id)
end

#status=(value) ⇒ Object



125
126
127
# File 'app/models/page.rb', line 125

def status=(value)
  self.status_id = value.id
end

#update_published_datetimeObject



239
240
241
# File 'app/models/page.rb', line 239

def update_published_datetime
  self.published_at = Time.zone.now if published? && published_at.blank?
end