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



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

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)

      # make sure we'll deal with a Hash and not a string
      transform_sections_content(page, locale)
    end
  end
end

#apply_to_entity(entity) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# 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_automatic_translations(entity, locale)
    set_default_redirect_type(entity, locale)
  end

  check_and_mark_as_templatized(entity)
end

#check_and_mark_as_templatized(page) ⇒ Object



146
147
148
149
150
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 146

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

#depth(page) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 107

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



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

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

#mark_as_templatized(page, content_type) ⇒ Object



152
153
154
155
156
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 152

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



172
173
174
175
176
177
178
179
180
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 172

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



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

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



142
143
144
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 142

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

#set_automatic_translations(page, locale) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 93

def set_automatic_translations(page, locale)
  return if locale == default_locale

  if page[:template_path][locale].blank?
    %i(
      title slug fullpath template_path redirect_url
      sections_content sections_dropzone_content
      seo_title meta_description meta_keywords
    ).each do |name|
      page[name][locale] ||= page[name][default_locale]
    end
  end
end

#set_default_redirect_type(page, locale) ⇒ Object



73
74
75
76
77
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 73

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



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 79

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



138
139
140
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 138

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



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 49

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



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

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

#system_pages?(page) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#transform_sections_content(page, locale) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 158

def transform_sections_content(page, locale)
  [:sections_dropzone_content, :sections_content].each do |name|
    if content = page[name][locale]
      return unless content.is_a?(String)

      begin
        page[name][locale] = MultiJson.load(content)
      rescue MultiJson::ParseError => e
        raise Locomotive::Steam::JsonParsingError.new(e, page.template_path[locale], content)
      end
    end
  end
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



65
66
67
68
69
70
71
# File 'lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb', line 65

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

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