app/controllers/archangel/frontend/pages_controller.rb in archangel-0.3.0 vs app/controllers/archangel/frontend/pages_controller.rb in archangel-0.4.0
- old
+ new
@@ -8,123 +8,112 @@
##
# Frontend pages controller
#
class PagesController < FrontendController
before_action :set_resource, only: %i[show]
- before_action :assign_meta_tags, if: -> { request.get? },
- unless: -> { request.xhr? }
+ before_action :assign_resource_meta_tags, if: -> { request.get? },
+ unless: -> { request.xhr? }
##
# Frontend page
#
# Formats
# HTML, JSON
#
# Params
- # [String] path - the path to the page
+ # [String] permalink - the permalink to the page
#
# Request
- # GET /:path
- # GET /:path.json
+ # GET /:permalink
+ # GET /:permalink.json
#
# Response
# {
# "id": 123,
# "title": "Page Title",
- # "path": "path/to/page",
+ # "permalink": "/path/to/page",
# "content": "</p>Content of the page</p>",
# "homepage": false,
# "published_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
# "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
# "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
# }
#
def show
- return redirect_to_homepage if redirect_to_homepage?
+ return render_or_redirect_to_homepage if redirect_to_homepage?
respond_to do |format|
format.html do
- render inline: liquid_rendered_template_content,
+ render inline: liquid_rendered_design_content,
layout: layout_from_theme
end
format.json do
@page.content = liquid_rendered_content
- render json: @page, layout: false
+ render(template: "archangel/frontend/pages/show", layout: false)
end
end
end
protected
##
# Find and assign resource to the view
#
def set_resource
- page_path = params.fetch(:path, nil)
+ page_permalink = params.fetch(:permalink, nil)
- @page = page_path.blank? ? find_homepage : find_page(page_path)
+ @page = current_site.pages.available.find_by!(permalink: page_permalink)
end
##
# Assign meta tags to view
#
- def assign_meta_tags
- apply_meta_tags(page_meta_tags)
+ def assign_resource_meta_tags
+ assign_meta_tags(resource_meta_tags)
end
##
# Meta tags for the page
#
# @return [Object] the page meta tags
#
- def page_meta_tags
- [
+ def resource_meta_tags
+ meta_tags = [
current_site.metatags,
@page.metatags
].flatten.inject({}) do |tags, metatag|
tags.merge(metatag.name => metatag.content)
- end.merge(title: @page.title)
+ end
+
+ { image_src: current_site.logo.url }.merge(meta_tags)
+ .merge(title: @page.title)
end
+ def render_or_redirect_to_homepage
+ return redirect_to_homepage if current_site.homepage_redirect?
+
+ render_error("archangel/errors/error_404", :not_found)
+ end
+
##
- # Check to redirect to homepage root path
+ # Check to redirect to homepage root permalink
#
# @return [Boolean] redirect or not
#
def redirect_to_homepage?
- return false unless @page
-
- (params.fetch(:path, nil) == @page.path) && @page.homepage?
+ @page.homepage?
end
##
- # Redirect to homepage root path is page is marked as the homepage
+ # Redirect to homepage root permalink is page is marked as the homepage
#
def redirect_to_homepage
- redirect_to root_path, status: :moved_permanently
+ redirect_to frontend_root_path, status: :moved_permanently
end
##
- # Find the homepage
- #
- # @return [Object] the homepage
- #
- def find_homepage
- current_site.pages.published.homepage.first!
- end
-
- ##
- # Find the requested page
- #
- # @return [Object] the page
- #
- def find_page(path)
- current_site.pages.published.find_by!(path: path)
- end
-
- ##
# Render content
#
# @return [String] the rendered Liquid content
#
def liquid_rendered_content
@@ -132,18 +121,18 @@
Archangel::RenderService.call(@page.content, variables)
end
##
- # Render content with template
+ # Render content with design
#
# @return [String] the rendered Liquid template
#
- def liquid_rendered_template_content
+ def liquid_rendered_design_content
content = liquid_rendered_content
variables = default_liquid_assign.merge(content_for_layout: content)
- Archangel::TemplateRenderService.call(@page.template, variables)
+ Archangel::DesignRenderService.call(@page.design, variables)
end
def default_liquid_assign
{
current_page: request.fullpath,