Skip to content

Latest commit

 

History

History
120 lines (80 loc) · 2.43 KB

page.md

File metadata and controls

120 lines (80 loc) · 2.43 KB

Page attributes concept

Store page attributes in global @page var. Structure in modules and classes benefit from inheritance.

Installation

  • copy page.rb to app/models/page.rb
  • copy page_spec.rb to spec/models/page_spec.rb
  • add before_action, layout and the protected methods inside application_controller.rb
  • extend application_helper.rb
  • copy application_helper_spec.rb to spec/helpers/application_helper_spec.rb

Usage

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

# [...]

before_action :create_default_page

layout :page_layout

# [...]

# ================================================================= PROTECTED
protected

  def create_default_page
    create_myshop_basic_page
  end

  def page_layout
    @page.layout
  end

  def create_myshop_basic_page
    @page = Page::MyShop::Basic.new
  end

  def create_myshop_checkout_page
    @page = Page::MyShop::Checkout.new
  end

  def create_myshop_products_page
    @page = Page::MyShop::Products.new
  end

app/helpers/application_helper.rb

# encoding: utf-8
module ApplicationHelper

  # html markup helper
  def title_element(prefix = '', suffix = '')
    content_tag(:title, prefix + @page.title + @page.name + suffix)
  end

  def description_meta_tag
    tag(:meta, { name: "description", content: @page.meta_description })
  end

  def keywords_meta_tag
    tag(:meta, { name: "keywords", content: @page.meta_keywords })
  end

  def body_element(body_class = nil)
    body_id = "#{controller_path.tr('/', '_')}_#{action_name}"
    tag("body", { id: body_id , class: body_class }, true)
  end

example use in app/views/layouts/application.html.erb

<%= title_element %>

<%= description_meta_tag %>
<%= keywords_meta_tag %>

example use in app/views/layouts/application.html.erb generate <body class="...">

<%= body_element(@page.body_class) %>

example in app/controllers/*_controller.rb

change title, meta desc for singe page or complete controller with before_filter

def index
  @page.title = "custom title"
  @page.meta_description = "custom meta"
  # add values with
  @page.body_class += " classic lightgrey"
end

example in app/views/**/*.html.erb

<%# or update/change the values inside .html.erb %>
<%= debug(@page) %>
<% @page.title = "custom title" %>
<% @page.meta_description = "custom meta" %>