Class: Locomotive::Steam::Adapters::Filesystem::Sanitizers::Page

Inherits:
Object
  • Object
show all
Includes:
Locomotive::Steam::Adapters::Filesystem::Sanitizer
Defined in:
lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb

Instance Attribute Summary

Attributes included from Locomotive::Steam::Adapters::Filesystem::Sanitizer

#scope

Instance Method Summary collapse

Methods included from Locomotive::Steam::Adapters::Filesystem::Sanitizer

#apply_to, #apply_to_entity_with_dataset, #attach_site_to, #with

Instance Method Details

#apply_to_dataset(dataset) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 29

def apply_to_dataset(dataset)
  sorted_collection(dataset.records.values).each do |page|
    locales.each do |locale|
      set_parent_id(page)

      modify_if_parent_templatized(page, locale)

      # the following method needs to be called first
      set_fullpath_for(page, locale)

      use_default_locale_template_path(page, locale)
    end
  end
end

#apply_to_entity(entity) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 17

def apply_to_entity(entity)
  super

  record_id(entity) # required to get the parent_id

  locales.each do |locale|
    set_default_redirect_type(entity, locale)
  end

  check_and_mark_as_templatized(entity)
end

#check_and_mark_as_templatized(page) ⇒ Object



128
129
130
131
132
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 128

def check_and_mark_as_templatized(page)
  if content_type = page[:content_type]
    mark_as_templatized(page, content_type)
  end
end

#depth(page) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 89

def depth(page)
  return page.depth if page.depth

  page.depth = page[:_fullpath].split('/').size

  if system_pages?(page)
    page.depth = 0
  end

  page.depth
end

#fetch_localized_fullpath(fullpath, locale) ⇒ Object



116
117
118
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 116

def fetch_localized_fullpath(fullpath, locale)
  @localized[locale][fullpath]
end

#mark_as_templatized(page, content_type) ⇒ Object



134
135
136
137
138
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 134

def mark_as_templatized(page, content_type)
  @templatized_ids[page._id]  = content_type
  page[:templatized]          = true
  page[:target_klass_name]    = "Locomotive::ContentEntry#{content_type}"
end

#modify_if_parent_templatized(page, locale) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 140

def modify_if_parent_templatized(page, locale)
  parent_templatized = @templatized_ids[page.parent_id]

  if page[:templatized]
    page[:slug][locale] = Locomotive::Steam::WILDCARD unless parent_templatized
  elsif parent_templatized
    mark_as_templatized(page, parent_templatized)
  end
end

#parent_fullpath(page) ⇒ Object



110
111
112
113
114
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 110

def parent_fullpath(page)
  return nil if page._fullpath == 'index' || page._fullpath == '404'
  path = page._fullpath.split('/')[0..-2].join('/')
  path.blank? ? 'index' : path
end

#record_id(entity) ⇒ Object



124
125
126
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 124

def record_id(entity)
  @ids[entity[:_fullpath]] = entity._id
end

#set_default_redirect_type(page, locale) ⇒ Object



69
70
71
72
73
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 69

def set_default_redirect_type(page, locale)
  if page.redirect_url[locale]
    page.attributes[:redirect_type] ||= 301
  end
end

#set_fullpath_for(page, locale) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 75

def set_fullpath_for(page, locale)
  slug = fullpath = page.slug[locale].try(:to_s)

  return if slug.blank?

  if page.depth > 1
    base = parent_fullpath(page)
    fullpath = (fetch_localized_fullpath(base, locale) || base) + '/' + slug
  end

  set_localized_fullpath(page._fullpath, fullpath, locale)
  page[:fullpath][locale] = fullpath
end

#set_localized_fullpath(fullpath, value, locale) ⇒ Object



120
121
122
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 120

def set_localized_fullpath(fullpath, value, locale)
  @localized[locale][fullpath] = value
end

#set_parent_id(page) ⇒ Object

when this is called, the @ids hash has been populated completely



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 45

def set_parent_id(page)
  page._fullpath ||= page.attributes.delete(:_fullpath)

  return if page._fullpath == '404'

  parent_key = parent_fullpath(page)

  page[:parent_ids] = @parent_ids[parent_key] || []
  page[:parent_id]  = @ids[parent_key]

  @parent_ids[page._fullpath] = page.parent_ids + [page._id]
end

#setup(scope) ⇒ Object



10
11
12
13
14
15
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 10

def setup(scope)
  super.tap do
    @ids, @parent_ids, @templatized_ids = {}, {}, {}
    @localized      = locales.inject({}) { |m, l| m[l] = {}; m }
  end
end

#sorted_collection(collection) ⇒ Object



106
107
108
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 106

def sorted_collection(collection)
  collection.sort_by { |page| depth(page) }
end

#system_pages?(page) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 101

def system_pages?(page)
  page.depth == 1 &&
  %w(index 404).include?(page.slug.values.compact.first)
end

#use_default_locale_template_path(page, locale) ⇒ Object

If the page does not have a template in a locale then use the template of the default locale



61
62
63
64
65
66
67
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 61

def use_default_locale_template_path(page, locale)
  paths = page.template_path

  if paths[locale] == false
    paths[locale] = paths[default_locale]
  end
end