-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
pages_controller.rb
106 lines (87 loc) · 3 KB
/
pages_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module Refinery
class PagesController < ::ApplicationController
include Pages::RenderOptions
before_action :find_page, :set_canonical
before_action :error_404, if: :action_has_page_finder?,
unless: :current_user_can_view_page?
# Save whole Page after delivery
after_action :write_cache?
# This action is usually accessed with the root path, normally '/'
def home
render_with_templates?
end
# This action can be accessed normally, or as nested pages.
# Assuming a page named "mission" that is a child of "about",
# you can access the pages with the following URLs:
#
# GET /pages/about
# GET /about
#
# GET /pages/mission
# GET /about/mission
#
def show
if should_skip_to_first_child?
redirect_to refinery.url_for(first_live_child.url) and return
elsif page.link_url.present?
redirect_to page.link_url and return
elsif should_redirect_to_friendly_url?
redirect_to refinery.url_for(page.url), :status => 301 and return
end
render_with_templates?
end
protected
def requested_friendly_id
if ::Refinery::Pages.scope_slug_by_parent
# Pick out last path component, or id if present
"#{params[:path]}/#{params[:id]}".split('/').last
else
# Remove leading and trailing slashes in path, but leave internal
# ones for global slug scoping
params[:path].to_s.gsub(%r{\A/+}, '').presence || params[:id]
end
end
def should_skip_to_first_child?
page.skip_to_first_child && first_live_child
end
def should_redirect_to_friendly_url?
requested_friendly_id != page.friendly_id || (
::Refinery::Pages.scope_slug_by_parent &&
params[:path].present? && params[:path].match(page.root.slug).nil?
)
end
def current_user_can_view_page?
page.live? || authorisation_manager.allow?(:plugin, "refinery_pages")
end
def action_has_page_finder?
self.class.instance_methods(false).include? action_page_finder
end
def first_live_child
page.children.order('lft ASC').live.first
end
def find_page(fallback_to_404 = true)
@page ||= send(action_page_finder) if action_has_page_finder?
@page || (error_404 if fallback_to_404)
end
alias_method :page, :find_page
def find_page_for_home
Refinery::Page.find_by link_url: '/'
end
def find_page_for_show
Refinery::Page.friendly.find_by_path_or_id params[:path], params[:id]
end
def set_canonical
@canonical = refinery.url_for @page.canonical if @page.present?
end
def write_cache?
# Don't cache the page with the site bar showing.
if Refinery::Pages.cache_pages_full && !authorisation_manager.allow?(:read, :site_bar)
cache_page(response.body, File.join('', 'refinery', 'cache', 'pages', request.path).to_s)
end
end
private
def action_page_finder
:"find_page_for_#{action_name}"
end
end
end