Sha256: 8e080f6cf5f7e81177f02d4c4e99d6834ace927131bf1ffcfadf4464a3959de3

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

module Locomotive::Builder
  class Server

    # Sanitize the path from the previous middleware in order
    # to make it work for the renderer.
    #
    class Page < Middleware

      def call(env)
        self.set_accessors(env)

        self.set_page!(env)

        app.call(env)
      end

      protected

      def set_page!(env)
        page = self.fetch_page

        if page
          self.log "Found page \"#{page.title}\" [/#{page.inspect}]"
        end

        env['builder.page'] = page
      end

      def fetch_page
        matchers = self.path_combinations(self.path)

        self.mounting_point.pages.values.detect do |_page|
          matchers.include?(_page.safe_fullpath) ||
          matchers.include?(_page.safe_fullpath.try(:underscore))
        end
      end

      def path_combinations(path)
        self._path_combinations(path.split('/'))
      end

      def _path_combinations(segments, can_include_template = true)
        return nil if segments.empty?

        segment = segments.shift

        (can_include_template ? [segment, '*'] : [segment]).map do |_segment|
          if (_combinations = _path_combinations(segments.clone, can_include_template && _segment != '*'))
            [*_combinations].map do |_combination|
              File.join(_segment, _combination)
            end
          else
            [_segment]
          end
        end.flatten
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
locomotivecms_builder-1.0.0.alpha8 lib/locomotive/builder/server/page.rb
locomotivecms_builder-1.0.0.alpha7 lib/locomotive/builder/server/page.rb
locomotivecms_builder-1.0.0.alpha6 lib/locomotive/builder/server/page.rb
locomotivecms_builder-1.0.0.alpha5 lib/locomotive/builder/server/page.rb
locomotivecms_builder-1.0.0.alpha4 lib/locomotive/builder/server/page.rb
locomotivecms_builder-1.0.0.alpha3 lib/locomotive/builder/server/page.rb