Module: Card::Set::Abstract::FilterFormHelper::HtmlFormat

Extended by:
Card::Set::AbstractFormat
Defined in:
tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb

Instance Method Summary collapse

Instance Method Details

#autocomplete_filter(type_code, options_card = nil) ⇒ Object



67
68
69
70
71
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 67

def autocomplete_filter type_code, options_card=nil
  options_card ||= Card::Name[type_code, :type, :by_name]
  text_filter type_code, class: "#{type_code}_autocomplete",
                         "data-options-card": options_card
end

#filter_name(field, multi = false) ⇒ Object



96
97
98
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 96

def filter_name field, multi=false
  "filter[#{field}]#{'[]' if multi}"
end

#filter_options(field) ⇒ Object



100
101
102
103
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 100

def filter_options field
  raw = send("#{field}_options")
  raw.is_a?(Array) ? raw : option_hash_to_array(raw)
end

#multiselect_filter(field, default = nil, options = nil) ⇒ Object



25
26
27
28
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 25

def multiselect_filter field, default=nil, options=nil
  options ||= filter_options field
  multiselect_filter_tag field, default, options
end

#multiselect_filter_tag(field, default, options, html_options = {}) ⇒ Object



78
79
80
81
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 78

def multiselect_filter_tag field, default, options, html_options={}
  html_options[:multiple] = true
  select_filter_tag field, default, options, html_options
end

#multiselect_filter_type_based(type_codename) ⇒ Object



73
74
75
76
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 73

def multiselect_filter_type_based type_codename
  options = type_options type_codename
  multiselect_filter type_codename, nil, options
end

#option_hash_to_array(hash) ⇒ Object



105
106
107
108
109
110
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 105

def option_hash_to_array hash
  hash.each_with_object([]) do |(key, value), array|
    array << [key, value.to_s.downcase]
    array
  end
end

#range_filter(field, opts = {}) ⇒ Object



40
41
42
43
44
45
46
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 40

def range_filter field, opts={}
  add_class opts, "simple-text range-filter-subfield"
  output [range_sign(:from),
          sub_text_filter(field, :from, opts),
          range_sign(:to),
          sub_text_filter(field, :to, opts)]
end

#range_sign(side) ⇒ Object



48
49
50
51
52
53
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 48

def range_sign side
  dir = side == :from ? "right" : "left"
  wrap_with :span, class: "input-group-prepend" do
    fa_icon("chevron-circle-#{dir}", class: "input-group-text")
  end
end

#select_filter(field, default = nil, options = nil) ⇒ Object



19
20
21
22
23
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 19

def select_filter field, default=nil, options=nil
  options ||= filter_options field
  options.unshift(["--", ""]) unless default
  select_filter_tag field, default, options
end

#select_filter_tag(field, default, options, html_options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 83

def select_filter_tag field, default, options, html_options={}
  name = filter_name field, html_options[:multiple]
  default = filter_param(field) || default
  options = options_for_select(options, default)

  pointer_class = html_options[:multiple] ? "pointer-multiselect" : "pointer-select"
  other_classes = "filter-input #{field} _filter_input_field _no-select2 form-control"
  # _no-select2 because select is initiated after filter is opened.
  add_class html_options, "#{pointer_class} #{other_classes}"

  select_tag name, options, html_options
end

#select_filter_type_based(type_codename, order = "asc") ⇒ Object



61
62
63
64
65
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 61

def select_filter_type_based type_codename, order="asc"
  # take the card name as default label
  options = type_options type_codename, order, 80
  select_filter type_codename, nil, options
end

#sort_optionsObject



15
16
17
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 15

def sort_options
  {}
end

#sub_text_filter(field, subfield, opts = {}) ⇒ Object



55
56
57
58
59
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 55

def sub_text_filter field, subfield, opts={}
  name = "filter[#{field}][#{subfield}]"
  value = filter_hash.dig field, subfield
  text_filter_with_name_and_value name, value, opts
end

#text_filter(field, opts = {}) ⇒ Object



30
31
32
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 30

def text_filter field, opts={}
  text_filter_with_name_and_value filter_name(field), filter_param(field), opts
end

#text_filter_with_name_and_value(name, value, opts) ⇒ Object



34
35
36
37
38
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 34

def text_filter_with_name_and_value name, value, opts
  opts[:class] ||= "simple-text"
  add_class opts, "form-control"
  text_field_tag name, value, opts
end

#trim_option(option, max_length) ⇒ Object



120
121
122
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 120

def trim_option option, max_length
  option.size > max_length ? "#{option[0..max_length]}..." : option
end

#type_options(type_codename, order = "asc", max_length = nil) ⇒ Object



112
113
114
115
116
117
118
# File 'tmpsets/set/mod020-search/abstract/01_filter_form_helper.rb', line 112

def type_options type_codename, order="asc", max_length=nil
  type_card = Card[type_codename]
  res = Card.search type_id: type_card.id, return: :name, sort: "name", dir: order
  return res unless max_length

  res.map { |i| [trim_option(i, max_length), i] }
end