Class: Primer::ViewComponents::SelectPanelItemsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/primer/view_components/select_panel_items_controller.rb

Overview

:nodoc: :nocov:

Constant Summary collapse

SELECT_PANEL_ITEMS =
[
  { value: 1, title: "Photon torpedo", description: "Starship-mounted missile" },
  { value: 2, title: "Bat'leth", description: "The Klingon warrior's preferred means of achieving honor" },
  { value: 3, title: "Phaser", description: "The iconic handheld laser beam" },
  { value: 4, title: "Lightsaber", description: "An elegant weapon for a more civilized age", recent: true },
  { value: 5, title: "Proton pack", description: "Ghostbusting equipment" },
  { value: 6, title: "Sonic screwdriver", description: "The Time Lord's multi-purpose tool" },
  { value: 7, title: "Tricorder", description: "Handheld sensor device", recent: true },
  { value: 8, title: "TARDIS", description: "Time and relative dimension in space" }
]
"select-panel-seen-uuid-"

Instance Method Summary collapse

Instance Method Details

#indexObject



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
62
63
64
65
66
67
68
69
70
# File 'app/controllers/primer/view_components/select_panel_items_controller.rb', line 21

def index
  # delay a bit so loading spinners, etc can be seen
  sleep 2

  if params.fetch(:fail, "false") == "true"
    uuid = params[:uuid]

    # use the uuid to succeed for the first request and fail for all subsequent requests
    if !uuid || seen_uuid?(uuid)
      render status: :internal_server_error, plain: "An error occurred"
      return
    end

    mark_seen_uuid(uuid) if uuid
  end

  show_results = params.fetch(:show_results, "true") == "true"
  query = (params[:q] || "").downcase

  results = if show_results
              SELECT_PANEL_ITEMS.select do |item|
                [item[:title], item[:description]].join(" ").downcase.include?(query)
              end
            else
              []
            end

  if allows_selection?
    results = results.map(&:dup)
    results.each do |result|
      if selected_items.any? { |item| result[:title].downcase.include?(item) }
        result[:selected] = true
        break if single_select?
      end
    end
  end

  clean_up_old_uuids(uuid)

  respond_to do |format|
    format.any(:html, :html_fragment) do
      render(
        "primer/view_components/select_panel_items/index",
        locals: { results: results },
        layout: false,
        formats: [:html, :html_fragment]
      )
    end
  end
end