Class: Seiten::PageStore

Inherits:
Object
  • Object
show all
Defined in:
lib/seiten/page_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PageStore

Returns a new instance of PageStore.



7
8
9
10
11
12
13
# File 'lib/seiten/page_store.rb', line 7

def initialize(options={})
  @storage_type      = options[:storage_type] || Seiten.config[:default_storage_type]
  @storage_directory = options[:storage_directory] || File.join(Rails.root, Seiten.config[:default_storage_directory])
  @storage_language  = (options[:storage_language] || I18n.locale).to_sym
  @storage_file      = options[:storage_file] || load_storage_file
  @pages             = load_pages
end

Instance Attribute Details

#pagesObject

Returns the value of attribute pages.



5
6
7
# File 'lib/seiten/page_store.rb', line 5

def pages
  @pages
end

#storage_directoryObject

Returns the value of attribute storage_directory.



5
6
7
# File 'lib/seiten/page_store.rb', line 5

def storage_directory
  @storage_directory
end

#storage_fileObject

Returns the value of attribute storage_file.



5
6
7
# File 'lib/seiten/page_store.rb', line 5

def storage_file
  @storage_file
end

#storage_languageObject

Returns the value of attribute storage_language.



5
6
7
# File 'lib/seiten/page_store.rb', line 5

def storage_language
  @storage_language
end

#storage_typeObject

Returns the value of attribute storage_type.



5
6
7
# File 'lib/seiten/page_store.rb', line 5

def storage_type
  @storage_type
end

Class Method Details

.currentObject



27
28
29
# File 'lib/seiten/page_store.rb', line 27

def current
  Seiten.config[:current_page_store]
end

.default_localeObject

NOTE: Haven’t found out what’s the problem, but Rails has some weird I18n behaviour on initialization.

This method returns I18n.default_locale if config.i18n.default_locale is not set.


60
61
62
# File 'lib/seiten/page_store.rb', line 60

def default_locale
  Rails.application.config.i18n.default_locale || I18n.default_locale
end

.find_by(options = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/seiten/page_store.rb', line 35

def find_by(options={})
  tmp_storages = storages
  options.each do |option|
    tmp_storages = tmp_storages.select { |storage| storage.send(option[0]) == option[1] }
  end
  tmp_storages.first
end

.initialize_page_storesObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/seiten/page_store.rb', line 43

def initialize_page_stores
  # Check if config/navigation exists?
  # If true:  Initialize localized navigation page stores with locale language and directory
  # If false: Initialize the default storage file
  if File.directory?(File.join(Rails.root, Seiten.config[:default_storage_file]))
    Dir[File.join(Rails.root, Seiten.config[:default_storage_file], "*.yml")].each do |file|
      locale = File.basename(file, '.yml')
      Seiten::PageStore.storages << Seiten::PageStore.new(storage_language: locale, storage_directory: File.join(Rails.root, "app", "pages", locale))
    end
  else
    Seiten::PageStore.storages << Seiten::PageStore.new(storage_language: default_locale)
  end
  set_current_page_store(storage_language: default_locale)
end

.set_current_page_store(options = {}) ⇒ Object



31
32
33
# File 'lib/seiten/page_store.rb', line 31

def set_current_page_store(options={})
  Seiten.config[:current_page_store] = find_by(options) if options
end

.storagesObject



19
20
21
# File 'lib/seiten/page_store.rb', line 19

def storages
  @storages
end

.storages=(storages) ⇒ Object



23
24
25
# File 'lib/seiten/page_store.rb', line 23

def storages=(storages)
  @storages = storages
end

Instance Method Details



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/seiten/page_store.rb', line 78

def build_link(page, prefix_url="")

  # if url is nil parameterize title otherwise just use url
  slug = page["url"].nil? ? page["title"].parameterize : page["url"]

  # prepend prefix_url if slug is not root or external url
  unless slug[0] == "/" || !!(slug.match(/^https?:\/\/.+/)) || !prefix_url.present?
    slug = "#{prefix_url}/#{slug}"
  end

  # return nil if page slug is /
  if slug == "/" || page["root"] == true
    slug = nil
  end

  # remove leading slash if present
  if slug
    slug = slug[1..-1] if slug[0] == "/"
  end

  slug
end

#file_path(options = {}) ⇒ Object



74
75
76
# File 'lib/seiten/page_store.rb', line 74

def file_path(options={})
  File.join(storage_directory, options[:filename])
end

#load_pages(options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/seiten/page_store.rb', line 101

def load_pages(options={})

  pages      = options[:pages]
  parent_id  = options[:parent_id] # || nil
  layout     = options[:layout]
  prefix_url = options[:prefix_url] || ""

  # Setting default values
  if storage_type == :yaml
    pages ||= YAML.load_file(storage_file)
  end

  @id         ||= 1
  @navigation ||= []

  pages.each_index do |i|

    # Load page and set parent_id and generated page id
    page = pages[i]
    page["id"] = @id
    page["parent_id"] = parent_id
    page["layout"] ||= layout

    # Increment generated id
    @id += 1

    # Build link
    page["slug"] = build_link(page, prefix_url)

    # Set layout
    if page["layout"]
      if page["layout"].is_a?(String)
        inherited_layout = page["layout"]
      elsif page["layout"].is_a?(Hash)
        if page["layout"]["inherit"]
          inherited_layout = page["layout"]
        else
          inherited_layout = nil
        end
        page["layout"] = page["layout"]["name"]
      end
    end

    # Set redirect
    if page["redirect"]
      if page["redirect"].is_a?(TrueClass)
        page["redirect"] = build_link(page["nodes"].first, page["slug"])
      else
        page["redirect"] = page["redirect"][1..-1] if page["redirect"][0] == "/"
      end
    end

    # Load children
    if page["nodes"]
      load_pages(pages: page["nodes"], parent_id: page["id"], prefix_url: page["slug"], layout: inherited_layout, external: page["external"])
    end

    page_params = page.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
    @navigation << Page.new(page_params)
  end

  @navigation
end

#load_storage_fileObject



66
67
68
69
70
71
72
# File 'lib/seiten/page_store.rb', line 66

def load_storage_file
  if File.exists?(File.join(Rails.root, Seiten.config[:default_storage_file], "#{storage_language}.yml"))
    File.join(Rails.root, Seiten.config[:default_storage_file], "#{storage_language}.yml")
  else
    File.join(Rails.root, "#{Seiten.config[:default_storage_file]}.yml")
  end
end