Class: Cms::Page

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

Constant Summary collapse

NAME_REGEX =
/^[^\/][a-zA-Z0-9_\-\.]+$/

Instance Method Summary collapse

Instance Method Details

#content=(text) ⇒ Object



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

def content=(text)
  write_attribute :content, text
  self.is_layout_page = !(text =~ /\{\{\s?content_for_layout\s?\}\}/).blank?
end

#content_typeObject



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

def content_type
  Cms::Editable.content_type name
end

#rendered_content(controller) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/cms/page.rb', line 55

def rendered_content(controller)
  render_options = {} #{:filters => [CmsFilters, AppFilters]}
  common_assigns = {'params' => ParamsDrop.new((controller.params || {}).except(:controller, :action)), 'site_url' => controller.request.protocol + controller.request.raw_host_with_port}

  context_obj = Cms::Context.new(Cms.context_class ? context : nil)

  # render the current page
  page = self
  template = Liquid::Template.parse(page.content)
  template.registers[:context] = context_obj
  template.registers[:controller] = controller
  content = template.render(common_assigns, render_options)
  common_assigns.update(template.instance_assigns)

  # and then render any layout files of the current page
  while page.layout_page do
    page = page.layout_page
    template = Liquid::Template.parse(page.content)
    template.registers[:context] = context_obj
    content = template.render({'content_for_layout' => content}.merge(common_assigns), render_options)
    common_assigns.update(template.instance_assigns)
  end

  return content
end

#reserved_slug?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/cms/page.rb', line 37

def reserved_slug?
  return false if self.slug.blank?

  # reserved paths
  return true if self.slug =~ /\A\/(stylesheets|scripts|images)\b/

  path_options = Rails.application.routes.recognize_path(slug)
  # if the :url option is nil, then one of the existing routes matched the slug
  return true if path_options[:url].nil?

  first_segment = (path_options[:url] || '/').split('/')[0]

  # if part of the path matches and of the route "controllers" then we have a route conflict
  Rails.application.routes.routes.any? do |r|
    r.path.split('/')[1] == first_segment
  end
end

#to_sObject



33
34
35
# File 'app/models/cms/page.rb', line 33

def to_s
  name
end

#urlObject



85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/cms/page.rb', line 85

def url
  case content_type
  when 'text/css'
    "/cms/stylesheets/#{name}"
  when 'text/javascript'
    "/cms/javascripts/#{name}"
  else
    slug = read_attribute(:slug)
    slug.present? ? slug : "/#{name}"
  end
end