Module: HotwireCombobox::Helper

Defined in:
lib/hotwire_combobox/helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.hw_alias(method_name) ⇒ Object



6
7
8
9
10
# File 'lib/hotwire_combobox/helper.rb', line 6

def hw_alias(method_name)
  unless bypass_convenience_methods?
    alias_method method_name.to_s.sub(/^hw_/, ""), method_name
  end
end

Instance Method Details

#hw_call_method_or_proc(object, method_or_proc) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/hotwire_combobox/helper.rb', line 162

def hw_call_method_or_proc(object, method_or_proc)
  if method_or_proc.is_a? Proc
    method_or_proc.call object
  else
    hw_call_method object, method_or_proc
  end
end

#hw_combobox_chip_remover_attrs(display:, value:, **kwargs) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hotwire_combobox/helper.rb', line 104

def hw_combobox_chip_remover_attrs(display:, value:, **kwargs)
  {
    tabindex: "0",
    class: token_list("hw-combobox__chip__remover", kwargs[:class]),
    aria: { label: "Remove #{display}" },
    data: {
      action: "click->hw-combobox#removeChip:stop keydown->hw-combobox#navigateChip",
      hw_combobox_target: "chipDismisser",
      hw_combobox_value_param: value
    }
  }
end

#hw_combobox_dismissing_chip_remover_attrs(display, value) ⇒ Object



117
118
119
120
121
# File 'lib/hotwire_combobox/helper.rb', line 117

def hw_combobox_dismissing_chip_remover_attrs(display, value)
  hw_combobox_chip_remover_attrs(display: display, value: value).tap do |attrs|
    attrs[:data][:hw_combobox_target] = token_list(attrs[:data][:hw_combobox_target], "closer")
  end
end

#hw_combobox_options(options, render_in: {}, include_blank: nil, display: :to_combobox_display, **custom_methods) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hotwire_combobox/helper.rb', line 23

def hw_combobox_options(
      options,
      render_in: {},
      include_blank: nil,
      display: :to_combobox_display,
      **custom_methods)
  HotwireCombobox::Listbox::Item.collection_for \
    self,
    options,
    render_in: render_in,
    include_blank: include_blank,
    **custom_methods.merge(display: display)
end

#hw_combobox_page_stream_actionObject



149
150
151
# File 'lib/hotwire_combobox/helper.rb', line 149

def hw_combobox_page_stream_action
  params[:page].to_i > 0 ? :append : :update
end

#hw_combobox_selection_chip(display:, value:, for_id: , remover_attrs: hw_combobox_chip_remover_attrs(display: display, value: value)) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hotwire_combobox/helper.rb', line 58

def hw_combobox_selection_chip(
      display:,
      value:,
      for_id: params[:for_id],
      remover_attrs: hw_combobox_chip_remover_attrs(display: display, value: value))
  render "hotwire_combobox/selection_chip",
    display: display,
    value: value,
    for_id: for_id,
    remover_attrs: remover_attrs
end

#hw_combobox_selection_chips_for(objects, display: :to_combobox_display, value: :id, for_id: ) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hotwire_combobox/helper.rb', line 70

def hw_combobox_selection_chips_for(
      objects,
      display: :to_combobox_display,
      value: :id,
      for_id: params[:for_id])
  objects.map do |object|
    hw_combobox_selection_chip \
      display: hw_call_method(object, display),
      value: hw_call_method(object, value),
      for_id: for_id
  end.then { |chips| safe_join chips }
end

#hw_combobox_style_tag(*args, **kwargs) ⇒ Object



13
14
15
# File 'lib/hotwire_combobox/helper.rb', line 13

def hw_combobox_style_tag(*args, **kwargs)
  stylesheet_link_tag HotwireCombobox.stylesheet_path, *args, **kwargs
end

#hw_combobox_tag(name, options_or_src = [], render_in: {}, include_blank: nil, **kwargs, &block) ⇒ Object



17
18
19
20
21
# File 'lib/hotwire_combobox/helper.rb', line 17

def hw_combobox_tag(name, options_or_src = [], render_in: {}, include_blank: nil, **kwargs, &block)
  options, src = hw_extract_options_and_src options_or_src, render_in, include_blank
  component = HotwireCombobox::Component.new self, name, options: options, async_src: src, **kwargs
  render component, &block
end

#hw_dismissing_combobox_selection_chip(display:, value:, for_id: ) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/hotwire_combobox/helper.rb', line 83

def hw_dismissing_combobox_selection_chip(display:, value:, for_id: params[:for_id])
  hw_combobox_selection_chip \
    display: display,
    value: value,
    for_id: for_id,
    remover_attrs: hw_combobox_dismissing_chip_remover_attrs(display, value)
end

#hw_dismissing_combobox_selection_chips_for(objects, display: :to_combobox_display, value: :id, for_id: ) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hotwire_combobox/helper.rb', line 91

def hw_dismissing_combobox_selection_chips_for(
      objects,
      display: :to_combobox_display,
      value: :id,
      for_id: params[:for_id])
  objects.map do |object|
    hw_dismissing_combobox_selection_chip \
      display: hw_call_method(object, display),
      value: hw_call_method(object, value),
      for_id: for_id
  end.then { |chips| safe_join chips }
end

#hw_listbox_id(id) ⇒ Object

private library use only



137
138
139
# File 'lib/hotwire_combobox/helper.rb', line 137

def hw_listbox_id(id)
  "#{id}-hw-listbox"
end

#hw_paginated_combobox_options(options, for_id: , src: hw_fullpath_for_pagination, next_page: nil, render_in: {}, include_blank: {}, **custom_methods) ⇒ Object Also known as: hw_async_combobox_options



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hotwire_combobox/helper.rb', line 37

def hw_paginated_combobox_options(
      options,
      for_id: params[:for_id],
      src: hw_fullpath_for_pagination,
      next_page: nil,
      render_in: {},
      include_blank: {},
      **custom_methods)
  include_blank = params[:page].to_i > 0 ? nil : include_blank
  options = hw_combobox_options options, render_in: render_in, include_blank: include_blank, **custom_methods
  this_page = render "hotwire_combobox/paginated_options", for_id: for_id, options: options
  next_page = render "hotwire_combobox/next_page", for_id: for_id, src: hw_combobox_next_page_uri(src, next_page, for_id)

  safe_join [ this_page, next_page ]
end

#hw_pagination_frame_id(id) ⇒ Object



145
146
147
# File 'lib/hotwire_combobox/helper.rb', line 145

def hw_pagination_frame_id(id)
  "#{id}__hw_combobox_pagination"
end

#hw_pagination_frame_wrapper_id(id) ⇒ Object



141
142
143
# File 'lib/hotwire_combobox/helper.rb', line 141

def hw_pagination_frame_wrapper_id(id)
  "#{id}__hw_combobox_pagination__wrapper"
end

#hw_uri_with_params(url_or_path, **params) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/hotwire_combobox/helper.rb', line 153

def hw_uri_with_params(url_or_path, **params)
  URI.parse(url_or_path).tap do |url_or_path|
    query = URI.decode_www_form(url_or_path.query || "").to_h.merge(params.compact_blank.stringify_keys)
    url_or_path.query = URI.encode_www_form query
  end.to_s
rescue URI::InvalidURIError
  url_or_path
end

#hw_within_combobox_selection_chip(for_id: , &block) ⇒ Object



54
55
56
# File 'lib/hotwire_combobox/helper.rb', line 54

def hw_within_combobox_selection_chip(for_id: params[:for_id], &block)
  render layout: "hotwire_combobox/layouts/selection_chip", locals: { for_id: for_id }, &block
end