Class: Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Annotatable, DeprecatedTags, 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

Methods included from LocalTime

#adjust_time

Instance Attribute Details

#pagination_parametersObject

Returns the value of attribute pagination_parameters.



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

def pagination_parameters
  @pagination_parameters
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

Class Method Details

.date_column_namesObject



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

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

.descendant_class(class_name) ⇒ Object

Raises:

  • (ArgumentError)


311
312
313
314
315
316
317
318
# File 'app/models/page.rb', line 311

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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'app/models/page.rb', line 260

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



275
276
277
# File 'app/models/page.rb', line 275

def display_name=(string)
  display_name(string)
end

.find_by_path(path, live = true) ⇒ Object



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

def find_by_path(path, live = true)
  raise MissingRootPageError unless root
  root.find_by_path(path, live)
end

.find_by_url(*args) ⇒ Object



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

def find_by_url(*args)
  ActiveSupport::Deprecation.warn("`find_by_url' has been deprecated; use `find_by_path' instead.", caller)
  find_by_path(*args)
end

.is_descendant_class_name?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


307
308
309
# File 'app/models/page.rb', line 307

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

.load_subclassesObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'app/models/page.rb', line 279

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 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

.missing?Boolean

Returns:

  • (Boolean)


320
321
322
# File 'app/models/page.rb', line 320

def missing?
  false
end

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



298
299
300
301
302
303
304
305
# File 'app/models/page.rb', line 298

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

.rootObject



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

def root
  find_by_parent_id(nil)
end

.save_order(new_position) ⇒ Object



144
145
146
147
148
149
150
151
# File 'app/models/page.rb', line 144

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



233
234
235
# File 'app/models/page.rb', line 233

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

#cache?Boolean

Returns:

  • (Boolean)


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

def cache?
  true
end

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



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

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

#default_childObject



229
230
231
# File 'app/models/page.rb', line 229

def default_child
  self.class.default_child
end

#descriptionObject



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

def description
  self["description"]
end

#description=(value) ⇒ Object



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

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

#field(name) ⇒ Object



94
95
96
97
98
99
100
# File 'app/models/page.rb', line 94

def field(name)
  if new_record? or 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, live = true, clean = true) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/page.rb', line 190

def find_by_path(path, live = true, clean = true)
  return nil if virtual?
  path = clean_path(path) if clean
  my_path = self.path
  if (my_path == path) && (not live or published?)
    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, live, clean) # TODO: set to find_by_path after deprecation
      return found if found
    end
    children.each do |child|
      found = child.find_by_path(path, live, clean) # TODO: set to find_by_path after deprecation
      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})" if live
    return children.where([condition] + file_not_found_names).first
  end
  slug_child
end

#find_by_urlObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/models/page.rb', line 216

def find_by_path(path, live = true, clean = true)
  return nil if virtual?
  path = clean_path(path) if clean
  my_path = self.path
  if (my_path == path) && (not live or published?)
    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, live, clean) # TODO: set to find_by_path after deprecation
      return found if found
    end
    children.each do |child|
      found = child.find_by_path(path, live, clean) # TODO: set to find_by_path after deprecation
      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})" if live
    return children.where([condition] + file_not_found_names).first
  end
  slug_child
end

#has_or_inherits_part?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#has_part?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#headersObject



134
135
136
137
# File 'app/models/page.rb', line 134

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

#inherits_part?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#layout_with_inheritanceObject Also known as: layout



46
47
48
49
50
51
52
# File 'app/models/page.rb', line 46

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

#part(name) ⇒ Object



74
75
76
77
78
79
80
# File 'app/models/page.rb', line 74

def part(name)
  if new_record? or 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

#pathObject Also known as: url



118
119
120
121
122
123
124
# File 'app/models/page.rb', line 118

def path
  if parent?
    parent.child_path(self)
  else
    clean_path(slug)
  end
end

#process(request, response) ⇒ Object



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

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

#published?Boolean

Returns:

  • (Boolean)


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

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

#renderObject



169
170
171
172
173
174
175
# File 'app/models/page.rb', line 169

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

#render_part(part_name) ⇒ Object



177
178
179
180
181
182
183
184
# File 'app/models/page.rb', line 177

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

#render_snippet(snippet) ⇒ Object



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

def render_snippet(snippet)
  parse_object(snippet)
end

#response_codeObject



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

def response_code
  200
end

#scheduled?Boolean

Returns:

  • (Boolean)


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

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

#set_allowed_children_cacheObject



237
238
239
# File 'app/models/page.rb', line 237

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

#statusObject



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

def status
 Status.find(self.status_id)
end

#status=(value) ⇒ Object



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

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

#update_statusObject



218
219
220
221
222
223
224
225
226
227
# File 'app/models/page.rb', line 218

def update_status
  self.published_at = Time.zone.now if published? && self.published_at == nil

  if self.published_at != nil && (published? || scheduled?)
    self[:status_id] = Status[:scheduled].id if self.published_at  > Time.zone.now
    self[:status_id] = Status[:published].id if self.published_at <= Time.zone.now
  end

  true
end