Sha256: 1b905063f5985f3c8f93a54421ed9db62d14932bfe7d33ca6cdbaaa393712015

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module Seiten

  class PageStore

    attr_accessor :storage_type, :storage_file, :storage_directory

    def initialize(options={})
      @storage_type      = options[:storage_type]
      @storage_file      = options[:storage_file]
      @storage_directory = options[:storage_directory]

      @storage_type ||= Seiten.config[:storage_type]
      @storage_file ||= File.join(Rails.root, Seiten.config[:storage_file])
    end

    def load_pages(options={})

      pages  = options[:pages]
      parent_id = options[:parent_id]
      url       = options[:url]

      # Setting default values
      if storage_type == :yaml
        pages ||= YAML.load_file(storage_file)
      end
      parent_id   ||= nil
      url         ||= ""
      @id         ||= 1
      @navigation ||= []

      pages.each_index do |i|

        page = pages[i]
        page["id"] = @id
        page["parent_id"] = parent_id
        @id += 1

        # Build link
        slug = page["url"].nil? ? page["title"].parameterize : page["url"]
        if slug[0] == "/" || !!(slug.match(/^http:\/\/.+/))
          page["slug"] = slug
        else
          page["slug"] = "#{url}/#{slug}"
        end

        if page["nodes"]
          load_pages(pages: page["nodes"], parent_id: page["id"], url: page["slug"])
        end

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

      @navigation
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
seiten-0.0.1 lib/seiten/page_store.rb