Sha256: 5fc11ff531fef4fbaac9999d3864144fb77dd4d4be4e302d58f9f105b2162376

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 KB

Contents

module Locomotive::Steam
  module Middlewares

    class TemplatizedPage < ThreadSafe

      include Helpers

      def _call
        if page && page.templatized?
          set_content_entry!
        end
      end

      protected

      def set_content_entry!
        # extract the slug of the content entry
        %r(^#{page.fullpath.gsub(Locomotive::Steam::WILDCARD, '([^\/]+)')}$) =~ path

        if entry = fetch_content_entry($1)
          # the entry will be available in the template under different keys
          ['content_entry', 'entry', entry.content_type.slug.singularize].each do |key|
           liquid_assigns[key] = entry
          end

          env['steam.content_entry'] = page.content_entry = entry

          # log it
          log "Found content entry: #{entry._label}"
        else
          redirect_to '/404', 302
        end
      end

      def fetch_content_entry(slug)
        if type = content_type_repository.find(page.content_type_id)
          decorate(content_entry_repository.with(type).by_slug(slug))
        else
          nil
        end
      end

      def decorate(entry)
        return nil if entry.nil?
        Locomotive::Steam::Decorators::I18nDecorator.new(entry, default_locale)
      end

      def content_type_repository
        services.repositories.content_type
      end

      def content_entry_repository
        services.repositories.content_entry
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.0.pre.beta.1 lib/locomotive/steam/middlewares/templatized_page.rb
locomotivecms_steam-1.0.0.pre.alpha.3 lib/locomotive/steam/middlewares/templatized_page.rb
locomotivecms_steam-1.0.0.pre.alpha.2 lib/locomotive/steam/middlewares/templatized_page.rb
locomotivecms_steam-1.0.0.pre.alpha.1 lib/locomotive/steam/middlewares/templatized_page.rb
locomotivecms_steam-1.0.0.pre.alpha lib/locomotive/steam/middlewares/templatized_page.rb