Class: Lookbook::EmbedsController

Inherits:
ApplicationController show all
Includes:
ActionView::Helpers::SanitizeHelper, TargetableConcern, WithPreviewControllerConcern
Defined in:
app/controllers/lookbook/embeds_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WithPreviewControllerConcern

#preview_controller

Methods included from TargetableConcern

#inspector_data, #path_segments, #set_display_options, #set_title

Methods inherited from ApplicationController

#index, #not_found

Class Method Details

.controller_pathObject



15
16
17
# File 'app/controllers/lookbook/embeds_controller.rb', line 15

def self.controller_path
  "lookbook/embeds"
end

Instance Method Details

#lookupObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/lookbook/embeds_controller.rb', line 19

def lookup
  props = Store.new(params[:props] ? JsonParser.call(sanitize(params[:props])) : {})
  if props.preview.present?
    preview = Engine.previews.find_by_preview_class(props.preview)
    if preview.present?
      props.scenarios ||= props.scenario || ""
      scenario = preview.scenario(Array(props.scenarios).first)

      boolean_options = ["display_option_controls"]
      array_options = ["panels", "actions", "scenarios"]
      param_prefix = "param_"

      options = {}
      embed_params = {}

      props.each do |key, value|
        key = key.to_s.strip.tr("-", "_")
        value.strip!

        if array_options.include?(key)
          options[key] = if value == "false"
            []
          elsif value == "true"
            ["*"]
          else
            value.split(",").map(&:strip)
          end
        elsif boolean_options.include?(key)
          options[key] = (value == "false") ? false : !!value
        elsif key.start_with?(param_prefix)
          embed_params[key.gsub(param_prefix, "")] = value
        end
      end

      embed_params[:_options] = SearchParamEncoder.call(options)
      embed_params.symbolize_keys!

      return redirect_to lookbook_embed_url(scenario ? scenario.lookup_path : preview.lookup_path, embed_params)
    end
  end

  raise_not_found("Preview not found")
end

#showObject



63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/lookbook/embeds_controller.rb', line 63

def show
  @embed = true
  unless @target
    @target = @scenario_choices.first || @preview&.default_scenario
    if @target
      redirect_to lookbook_embed_path(@target.lookup_path, req_params)
    else
      raise_not_found("Preview not found")
    end
  end
end