Module: Card::Set::Abstract::Filter::FormHelper::HtmlFormat

Extended by:
Card::Set::AbstractFormat
Defined in:
card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb,
card/tmpsets/set/mod036-card-mod-search/abstract/03_filter/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#autocomplete_filter(type_code, options_card = nil) ⇒ Object



59
60
61
62
63
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 59

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



86
87
88
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 86

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

#filter_options(field) ⇒ Object



90
91
92
93
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 90

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



21
22
23
24
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 21

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



65
66
67
68
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 65

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

#normalize_select_filter_tag_html_options(field, html_options) ⇒ Object

alters html_options hash



78
79
80
81
82
83
84
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 78

def normalize_select_filter_tag_html_options field, html_options
  pointer_suffix = html_options[:multiple] ? "multiselect" : "select"
  add_class html_options, "pointer-#{pointer_suffix} filter-input #{field} " \
                          "_filter_input_field _no-select2 form-control"
  # _no-select2 because select is initiated after filter is opened.
  html_options[:id] = "filter-input-#{unique_id}"
end

#option_hash_to_array(hash) ⇒ Object



95
96
97
98
99
100
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 95

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

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



37
38
39
40
41
42
43
44
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 37

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

#range_sign(side) ⇒ Object



46
47
48
49
50
51
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 46

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



15
16
17
18
19
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 15

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



70
71
72
73
74
75
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 70

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

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



53
54
55
56
57
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 53

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

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



26
27
28
29
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 26

def text_filter field, default=nil, opts={}
  value = filter_param(field) || default
  text_filter_with_name_and_value filter_name(field), value, opts
end

#text_filter_with_name_and_value(name, value, opts) ⇒ Object



31
32
33
34
35
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 31

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



109
110
111
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 109

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



102
103
104
105
106
107
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 102

def type_options type_codename, order="asc", max_length=nil
  Card.cache.fetch "#{type_codename}-TYPE-OPTIONS" do
    res = Card.search type: type_codename, return: :name, sort: "name", dir: order
    max_length ? (res.map { |i| [trim_option(i, max_length), i] }) : res
  end
end

#view: filter_name_formgroupObject



11
12
13
# File 'card/tmpsets/set/mod035-card-mod-search/abstract/03_filter/form_helper.rb', line 11

view :filter_name_formgroup, cache: :never do
  text_filter :name
end