Class: Kadmin::Select2

Inherits:
Object
  • Object
show all
Includes:
Presentable
Defined in:
app/components/kadmin/select2.rb

Constant Summary collapse

CSS_CLASS_MARKER =
'kadmin-select2'
DATA_ATTRIBUTES =
[
  :placeholder, :data_url, :filter_param, :display_property, :value_property, :page_size
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Presentable

#present

Constructor Details

#initialize(options = {}) ⇒ Select2

Returns a new instance of Select2.



32
33
34
35
# File 'app/components/kadmin/select2.rb', line 32

def initialize(options = {})
  @placeholder = options[:placeholder].to_s.freeze
  extract_ajax_options!(options).freeze unless options.blank?
end

Instance Attribute Details

#data_urlString (readonly)

NOTE: using this assumes the backend URL uses a Finder object

Returns:

  • (String)

    if given, will set up remote fetching to this URL

See Also:



18
19
20
# File 'app/components/kadmin/select2.rb', line 18

def data_url
  @data_url
end

#display_propertyString (readonly)

Returns the name of the display property for the model (if doing remote fetching).

Returns:

  • (String)

    the name of the display property for the model (if doing remote fetching)



24
25
26
# File 'app/components/kadmin/select2.rb', line 24

def display_property
  @display_property
end

#filter_paramString (readonly)

Returns the name of the filter param when doing remote fetching.

Returns:

  • (String)

    the name of the filter param when doing remote fetching



21
22
23
# File 'app/components/kadmin/select2.rb', line 21

def filter_param
  @filter_param
end

#page_sizeInteger (readonly)

Returns the page size value to fetch on each select2 remote fetch.

Returns:

  • (Integer)

    the page size value to fetch on each select2 remote fetch



30
31
32
# File 'app/components/kadmin/select2.rb', line 30

def page_size
  @page_size
end

#placeholderString (readonly)

Returns will be used as a placeholder if given.

Returns:

  • (String)

    will be used as a placeholder if given



13
14
15
# File 'app/components/kadmin/select2.rb', line 13

def placeholder
  @placeholder
end

#value_propertyString (readonly)

Returns the name of the property used as the selected value.

Returns:

  • (String)

    the name of the property used as the selected value



27
28
29
# File 'app/components/kadmin/select2.rb', line 27

def value_property
  @value_property
end

Class Method Details

.mark_tag_as_select2!(html_options) ⇒ Object



73
74
75
76
77
# File 'app/components/kadmin/select2.rb', line 73

def mark_tag_as_select2!(html_options)
  css_classes = Array.wrap(html_options[:class])
  css_classes << CSS_CLASS_MARKER
  html_options[:class] = css_classes.join(' ')
end

.prepare_form_tag_options(options = {}, html_options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/components/kadmin/select2.rb', line 60

def prepare_form_tag_options(options = {}, html_options = {})
  options = options.dup
  select2_options = options.extract!(*DATA_ATTRIBUTES)

  select2 = new(select2_options)

  html_options = { data: {}, class: '' }.merge(html_options)
  html_options[:data].merge!(select2.to_data)
  mark_tag_as_select2!(html_options)

  return options, html_options
end

Instance Method Details

#to_dataObject



48
49
50
51
52
53
54
55
56
57
# File 'app/components/kadmin/select2.rb', line 48

def to_data
  {
    'placeholder' => placeholder,
    'ajax--url' => data_url,
    'kadmin--filter-param' => filter_param,
    'kadmin--display-property' => display_property,
    'kadmin--value-property' => value_property,
    'kadmin--page-size' => page_size
  }
end