class Para::SeoTools::Skeleton::PageBuilder

Attributes

config[R]
defaults[R]
locale[R]
name[R]
resource[R]

Public Class Methods

new(name, path: nil, resource: nil, **options) click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 11
def initialize(name, path: nil, resource: nil, **options)
  @name = Array.wrap(name).join(':')
  @path = path
  @resource = resource

  # Fetch locale on page build to allow calling the `page` skeleton
  # method inside a `I18n.with_locale` block
  #
  @locale = options.delete(:locale) || I18n.locale
  @defaults = options.delete(:defaults) || {}

  # Remaining options will be stored as config in a JSONB attribute
  @config = options
end

Private Class Methods

model_for(page) click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 78
def self.model_for(page)
  models[unique_identifier_for(page)] ||= ::Para::SeoTools::Page.new(
    identifier: page.identifier
  )
end
models() click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 84
def self.models
  @models ||= Para::SeoTools::Page.all.each_with_object({}) do |page, hash|
    hash[unique_identifier_for(page)] = page
  end
end
unique_identifier_for(page) click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 90
def self.unique_identifier_for(page)
  Para::SeoTools::PageScoping.new(page).unique_identifier
end

Public Instance Methods

display_name() click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 48
def display_name
  @display_name ||= [name, resource.try(:id)].compact.join(' #').humanize
end
identifier() click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 26
def identifier
  @identifier ||= [name, resource.try(:id)].compact.join(':')
end
model() click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 56
def model
  @model ||= self.class.model_for(self).tap do |page|
    # Override path (i.e.: slug changed)
    page.path = path if path.to_s != page.path
    page.locale = locale

    # Do not override meta tags if already present
    page.defaults = defaults
    page.config = config
  end
end
path() click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 52
def path
  @path ||= find_route
end
scope() click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 30
def scope
  @scope ||= config[:scope]
end
scope_attributes() click to toggle source
# File lib/para/seo_tools/skeleton/page_builder.rb, line 34
def scope_attributes
  scope.each_with_object({}) do |attribute, hash|
    hash[attribute] = if (value = config[attribute])
      value
    elsif respond_to?(attribute)
      send(attribute)
    else
      raise ScopeAttributeUndefined, "Your Skeleton page is scoped " +
        "with the '#{ attribute }' attribute but you did not pass it " +
        "as a parameter of the #page : #{ inspect }"
    end
  end
end

Private Instance Methods

find_route() click to toggle source

Find a route matching the name of the page, passing the resource for member routes to work out of the box

# File lib/para/seo_tools/skeleton/page_builder.rb, line 72
def find_route
  if respond_to?(:"#{ name }_path")
    send(:"#{ name }_path", resource)
  end
end