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 =
%i[
  placeholder data_url filter_param display_property value_property
  page_size minimum_input_length transform_request transform_response
].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.



42
43
44
45
# File 'app/components/kadmin/select2.rb', line 42

def initialize(options = {})
  @placeholder = options[:placeholder].to_s.freeze
  extract_ajax_options!(options).freeze if options.present?
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:



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

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)



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

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



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

def filter_param
  @filter_param
end

#minimum_input_lengthInteger (readonly)

Returns the minimum input length before trying to fetch remote data.

Returns:

  • (Integer)

    the minimum input length before trying to fetch remote data



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

def minimum_input_length
  @minimum_input_length
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



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

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



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

def placeholder
  @placeholder
end

#transform_requestString (readonly)

Returns name of callback to a globally-accessible function that can transform the request.

Returns:

  • (String)

    name of callback to a globally-accessible function that can transform the request



37
38
39
# File 'app/components/kadmin/select2.rb', line 37

def transform_request
  @transform_request
end

#transform_responseString (readonly)

Returns name of the callback to a globally-accessible function that can transform the response.

Returns:

  • (String)

    name of the callback to a globally-accessible function that can transform the response



40
41
42
# File 'app/components/kadmin/select2.rb', line 40

def transform_response
  @transform_response
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



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

def value_property
  @value_property
end

Class Method Details

.mark_tag_as_select2!(html_options) ⇒ Object



89
90
91
92
93
# File 'app/components/kadmin/select2.rb', line 89

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



76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/components/kadmin/select2.rb', line 76

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



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

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,
    'kadmin--minimum-input-length' => minimum_input_length,
    'kadmin--transform-request' => transform_request,
    'kadmin--transform-response' => transform_response
  }.compact
end