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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PageBuilder.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



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

def defaults
  @defaults
end

#localeObject (readonly)

Returns the value of attribute locale.



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

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

Instance Method Details

#display_nameObject



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

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

#identifierObject



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

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

#modelObject



56
57
58
59
60
61
62
63
64
65
# 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

#pathObject



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

def path
  @path ||= find_route
end

#scopeObject



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

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

#scope_attributesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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