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



125
126
127
128
129
130
131
# File 'lib/hotwire_combobox/helper.rb', line 125

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



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hotwire_combobox/helper.rb', line 63

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



76
77
78
79
80
# File 'lib/hotwire_combobox/helper.rb', line 76

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
# 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



108
109
110
# File 'lib/hotwire_combobox/helper.rb', line 108

def hw_combobox_page_stream_action
  hw_first_page? ? :update : :append
end

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



42
43
44
45
# File 'lib/hotwire_combobox/helper.rb', line 42

def hw_combobox_selection_chip(display:, value:, for_id: params[:for_id], remover_attrs: nil)
  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



47
48
49
50
51
# File 'lib/hotwire_combobox/helper.rb', line 47

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, request: request, **kwargs
  render component, &block
end

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



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

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



57
58
59
60
61
# File 'lib/hotwire_combobox/helper.rb', line 57

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_first_page?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/hotwire_combobox/helper.rb', line 112

def hw_first_page?
  params[:page].to_i.zero?
end

#hw_listbox_id(id) ⇒ Object

private library use only



96
97
98
# File 'lib/hotwire_combobox/helper.rb', line 96

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



27
28
29
30
31
32
33
34
# File 'lib/hotwire_combobox/helper.rb', line 27

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 = hw_first_page? ? include_blank : nil
  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



104
105
106
# File 'lib/hotwire_combobox/helper.rb', line 104

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

#hw_pagination_frame_wrapper_id(id) ⇒ Object



100
101
102
# File 'lib/hotwire_combobox/helper.rb', line 100

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

#hw_uri_with_params(url_or_path, **params) ⇒ Object



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

def hw_uri_with_params(url_or_path, **params)
  URI.parse(url_or_path).tap do |url_or_path|
    query = Rack::Utils.parse_nested_query(url_or_path.query || "").merge(params.compact_blank.stringify_keys)
    url_or_path.query = Rack::Utils.build_nested_query query
  end.to_s
rescue URI::InvalidURIError
  url_or_path
end

#hw_within_combobox_selection_chip(for_id: , &block) ⇒ Object



38
39
40
# File 'lib/hotwire_combobox/helper.rb', line 38

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