Sha256: 5adfdd5e9319b1bccfee2fd7a7cd53f036f6b4b99a3c1de7cbc5db0cbe0562d8

Contents?: true

Size: 1.85 KB

Versions: 15

Compression:

Stored size: 1.85 KB

Contents

module Lookbook
  class PreviewsController < ApplicationController
    include TargetableConcern
    include WithPreviewControllerConcern

    layout false

    before_action :permit_framing, only: [:show]

    def self.controller_path
      "lookbook/previews"
    end

    def index
      respond_to do |format|
        format.json do
          render(
            json: Lookbook.previews.map do |preview|
              {
                name: preview.name,
                scenarios: preview.scenarios.map { |scenario|
                  case scenario
                  when Lookbook::ScenarioEntity
                    scenario_json(scenario)
                  when Lookbook::ScenarioGroupEntity
                    {
                      name: scenario.name,
                      examples: scenario.scenarios.map { |s| scenario_json(s) }
                    }
                  end
                }
              }
            end
          )
        end
      end
    end

    def show
      raise_not_found("Preview not found") unless @target

      @preview_html = preview_controller.process(
        :render_in_layout_to_string,
        "lookbook/previews/group",
        inspector_data,
        layout: @preview.layout,
        append_html: (iframe_content_scripts if embedded?)
      )
    end

    private

    def embedded?
      params[:lookbook_embed] == "true"
    end

    def iframe_content_scripts
      render_to_string("lookbook/partials/_iframe_content_scripts", layout: nil)
    end

    def scenario_json(scenario)
      {
        name: scenario.name,
        inspect_path: scenario.url_path,
        preview_path: scenario.preview_path
      }
    end

    def permit_framing
      headers["X-Frame-Options"] = Lookbook.config.preview_embeds.policy if embedded?
      headers["X-Frame-Options"] = "SAMEORIGIN" if headers["X-Frame-Options"] == "DENY"
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
lookbook-2.0.5 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.4 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.3 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.2 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.1 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0.rc.3 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0.rc.2 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0.rc.1 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0.beta.9 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0.beta.8 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0.beta.7 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0.beta.6 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0.beta.5 app/controllers/lookbook/previews_controller.rb
lookbook-2.0.0.beta.4 app/controllers/lookbook/previews_controller.rb