Class: Para::SeoTools::Skeleton::PageBuilder

Inherits:
Object
  • Object
show all
Includes:
Helpers::DefaultDataMethodsHelper
Defined in:
lib/para/seo_tools/skeleton/page_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::DefaultDataMethodsHelper

#default_data_from_method_for, #default_description_for, #default_image_for, #default_title_for

Constructor Details

#initialize(name, path: nil, resource: nil, **options) ⇒ PageBuilder

Returns a new instance of PageBuilder.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 12

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 10

def config
  @config
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



10
11
12
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 10

def defaults
  @defaults
end

#localeObject (readonly)

Returns the value of attribute locale.



10
11
12
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 10

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 10

def name
  @name
end

#resourceObject (readonly)

Returns the value of attribute resource.



10
11
12
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 10

def resource
  @resource
end

Instance Method Details

#display_nameObject



49
50
51
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 49

def display_name
  @display_name ||= [name, resource.try(:id)].compact.join(' #').humanize
end

#identifierObject



27
28
29
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 27

def identifier
  @identifier ||= [name, resource.try(:id)].compact.join(':')
end

#modelObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 57

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 = process_defaults
    page.config = config
  end
end

#pathObject



53
54
55
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 53

def path
  @path ||= find_route
end

#scopeObject



31
32
33
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 31

def scope
  @scope ||= config[:scope]
end

#scope_attributesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/para/seo_tools/skeleton/page_builder.rb', line 35

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