Class: Kadmin::Select2
- Inherits:
-
Object
- Object
- Kadmin::Select2
- 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
-
#data_url ⇒ String
readonly
NOTE: using this assumes the backend URL uses a Finder object.
-
#display_property ⇒ String
readonly
The name of the display property for the model (if doing remote fetching).
-
#filter_param ⇒ String
readonly
The name of the filter param when doing remote fetching.
-
#minimum_input_length ⇒ Integer
readonly
The minimum input length before trying to fetch remote data.
-
#page_size ⇒ Integer
readonly
The page size value to fetch on each select2 remote fetch.
-
#placeholder ⇒ String
readonly
Will be used as a placeholder if given.
-
#transform_request ⇒ String
readonly
Name of callback to a globally-accessible function that can transform the request.
-
#transform_response ⇒ String
readonly
Name of the callback to a globally-accessible function that can transform the response.
-
#value_property ⇒ String
readonly
The name of the property used as the selected value.
Class Method Summary collapse
- .mark_tag_as_select2!(html_options) ⇒ Object
- .prepare_form_tag_options(options = {}, html_options = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Select2
constructor
A new instance of Select2.
- #to_data ⇒ Object
Methods included from Presentable
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( = {}) @placeholder = [:placeholder].to_s.freeze ().freeze if .present? end |
Instance Attribute Details
#data_url ⇒ String (readonly)
NOTE: using this assumes the backend URL uses a Finder object
19 20 21 |
# File 'app/components/kadmin/select2.rb', line 19 def data_url @data_url end |
#display_property ⇒ String (readonly)
Returns 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_param ⇒ String (readonly)
Returns 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_length ⇒ Integer (readonly)
Returns 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_size ⇒ Integer (readonly)
Returns 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 |
#placeholder ⇒ String (readonly)
Returns will be used as a placeholder if given.
14 15 16 |
# File 'app/components/kadmin/select2.rb', line 14 def placeholder @placeholder end |
#transform_request ⇒ String (readonly)
Returns 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_response ⇒ String (readonly)
Returns 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_property ⇒ String (readonly)
Returns 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!() css_classes = Array.wrap([:class]) css_classes << CSS_CLASS_MARKER [: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 ( = {}, = {}) = .dup = .extract!(*DATA_ATTRIBUTES) select2 = new() = { data: {}, class: '' }.merge() [:data].merge!(select2.to_data) mark_tag_as_select2!() return , end |
Instance Method Details
#to_data ⇒ Object
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 |