Module: Lookbook::TargetableConcern

Extended by:
ActiveSupport::Concern
Included in:
InspectorController, PreviewsController
Defined in:
app/controllers/concerns/lookbook/targetable_concern.rb

Instance Method Summary collapse

Instance Method Details

#inspector_dataObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 94

def inspector_data
  return @inspector_data if @inspector_data.present?

  rendered_examples = @target.examples.map do |example|
    output = preview_controller.process(:render_example_to_string, @preview, example.name)
    RenderedExample.new(example, output, preview_controller.params)
  end

  @inspector_data ||= Lookbook::Store.new({
    context: Store.new({params: @params, path: params[:path]}),
    preview: @preview,
    examples: rendered_examples,
    target: @target,
    data: Lookbook.data,
    app: Lookbook
  })
end

#lookup_entitiesObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 16

def lookup_entities
  @target = Lookbook.previews.find_example_by_path(params[:path])
  if @target.present?
    @preview = @target.preview
    if params[:path] == @preview&.path
      redirect_to lookbook_inspect_path("#{params[:path]}/#{@preview.default_example.name}", params.permit!)
    end
  else
    @preview = Lookbook.previews.find_by_path(params[:path])
    if @preview.present?
      default_example = @preview.default_example
      redirect_to lookbook_inspect_path(default_example.path, params.permit!) if default_example
    else
      @preview = Lookbook.previews.find_by_path(path_segments.slice(0, path_segments.size - 1).join("/"))
    end
  end
end

#path_segmentsObject



127
128
129
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 127

def path_segments
  params[:path].split("/")
end

#set_display_optionsObject



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
62
63
64
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 34

def set_display_options
  @dynamic_display_options = []
  @static_display_options = []

  if @target.present?
    opts = @target.display_options
    @dynamic_display_options = opts.select { _2.is_a?(Array) || _2.is_a?(Hash) }
    @static_display_options = opts.except(*@dynamic_display_options.keys)

    if params[:_display]
      display_params = SearchParamParser.call(params[:_display])
      display_params.each do |name, value|
        if @dynamic_display_options.key?(name)
          cookies["lookbook-display-#{name}"] = value
        end
      end
    end

    @dynamic_display_options.each do |name, opts|
      choices = opts.is_a?(Hash) ? opts[:choices].to_a : opts
      @static_display_options[name] ||= cookies.fetch("lookbook-display-#{name}", choices.first)
    end

    unless params[:_display]
      display_params = @dynamic_display_options.map do |name, opts|
        [name, @static_display_options[name]]
      end.to_h
      request.query_parameters[:_display] = SearchParamBuilder.call(display_params)
    end
  end
end

#set_paramsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 66

def set_params
  @params = []

  if @target
    @params = @target.tags("param").map do |param_tag|
      Param.from_tag(
        param_tag,
        value: preview_controller.params[param_tag.name]
      )
    end

    # cast known param values to correct type
    @params.each do |param|
      if preview_controller.params.key?(param.name)
        preview_controller.params[param.name] = param.cast_value
      end
    end

    # set display and data params for use in preview layouts
    preview_controller.params[:lookbook] = {
      display: @static_display_options,
      data: Lookbook.data
    }
  end

  preview_controller.params.permit!
end

#set_titleObject



12
13
14
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 12

def set_title
  @title = @target.present? ? [@target&.label, @preview&.label].compact.join(" :: ") : "Not found"
end

#show_404(layout: nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 112

def show_404(layout: nil)
  locals = if @preview
    {
      message: "Example not found",
      description: "The '#{@preview.label}' preview does not have an example named '#{path_segments.last}'."
    }
  else
    {
      message: "Not found",
      description: "Looked for '#{params[:path]}'.<br>The preview may have been renamed or deleted."
    }
  end
  render_in_layout "lookbook/404", layout: layout, **locals
end