Class: Chosen::MainController
- Inherits:
-
Volt::ModelController
- Object
- Volt::ModelController
- Chosen::MainController
- Defined in:
- app/chosen/controllers/main_controller.rb
Instance Method Summary collapse
- #before_index_remove ⇒ Object
-
#blur ⇒ Object
When a field goes out of focus, then we want to start checking a field.
-
#errors ⇒ Object
Find the errors for this field.
- #field_name ⇒ Object
-
#index ⇒ Object
Set the model to attrs.options so that we don’t render the view until the promise passed to attrs.options resolves.
-
#index_ready ⇒ Object
Currently catches the ‘change’ event and re-raises it as chosen:changed due to a framework conflict that was throwing an error.
- #label ⇒ Object
- #marked ⇒ Object
-
#model_inst ⇒ Object
Find the parent reactive value that produced the value (usually just model._field).
- #options ⇒ Object
- #random_id ⇒ Object
- #selected(value) ⇒ Object
- #selector ⇒ Object
- #setup_field ⇒ Object
- #unique_klass ⇒ Object
- #update_chosen ⇒ Object
Instance Method Details
#before_index_remove ⇒ Object
38 39 40 |
# File 'app/chosen/controllers/main_controller.rb', line 38 def before_index_remove `$(#{selector}).chosen("destroy");` end |
#blur ⇒ Object
When a field goes out of focus, then we want to start checking a field
87 88 89 |
# File 'app/chosen/controllers/main_controller.rb', line 87 def blur model_inst.mark_field!(@field_name) end |
#errors ⇒ Object
Find the errors for this field
78 79 80 |
# File 'app/chosen/controllers/main_controller.rb', line 78 def errors model_inst.marked_errors[@field_name] end |
#field_name ⇒ Object
82 83 84 |
# File 'app/chosen/controllers/main_controller.rb', line 82 def field_name @field_name end |
#index ⇒ Object
Set the model to attrs.options so that we don’t render the view until the promise passed to attrs.options resolves.
22 23 24 |
# File 'app/chosen/controllers/main_controller.rb', line 22 def index self.model = attrs. end |
#index_ready ⇒ Object
Currently catches the ‘change’ event and re-raises it as chosen:changed due to a framework conflict that was throwing an error. See related issue:
29 30 31 32 33 34 35 36 |
# File 'app/chosen/controllers/main_controller.rb', line 29 def index_ready = attrs. || {} [:width] ||= "100%" `$(#{selector}).chosen(#{.to_n}).change(function(e, params) { e.stopPropagation(); $(".my_select_box").trigger('chosen:change', params); });` end |
#label ⇒ Object
71 72 73 74 75 |
# File 'app/chosen/controllers/main_controller.rb', line 71 def label unless ['false','False'].include?(attrs.label) attrs.label || @field_name.titleize end end |
#marked ⇒ Object
91 92 93 |
# File 'app/chosen/controllers/main_controller.rb', line 91 def marked model_inst.marked_fields[@field_name] end |
#model_inst ⇒ Object
Find the parent reactive value that produced the value (usually just model._field)
67 68 69 |
# File 'app/chosen/controllers/main_controller.rb', line 67 def model_inst attrs.value_parent end |
#options ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'app/chosen/controllers/main_controller.rb', line 95 def attrs..then do || if [0].is_a?(Hash) = else = .collect { |option| {value: option, label: option }} end end end |
#random_id ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'app/chosen/controllers/main_controller.rb', line 55 def random_id "xxxxx".gsub /[xy]/ do |ch,| %x{ var r = Math.random() * 16 | 0, v = ch == "x" ? r : (r & 3 | 8); return v.toString(16); } end end |
#selected(value) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/chosen/controllers/main_controller.rb', line 106 def selected(value) if attrs.multiple == true || attrs.multiple == 'true' if attrs.value.include?(value) res = true else res = false end else if attrs.value == value res = true else res = false end end res end |
#selector ⇒ Object
51 52 53 |
# File 'app/chosen/controllers/main_controller.rb', line 51 def selector ".#{@unique_klass}" end |
#setup_field ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'app/chosen/controllers/main_controller.rb', line 10 def setup_field unless attrs.value_last_method field_type = self.class.to_s.underscore.gsub(/[_]Controller$/, '') raise "a <:chosen tag was used without passing a value attribute" end # Get the name of the field by looking at the method scope @field_name = attrs.value_last_method.gsub(/^[_]/, '') end |
#unique_klass ⇒ Object
47 48 49 |
# File 'app/chosen/controllers/main_controller.rb', line 47 def unique_klass @unique_klass ||= random_id end |
#update_chosen ⇒ Object
42 43 44 45 |
# File 'app/chosen/controllers/main_controller.rb', line 42 def update_chosen `$(#{selector}).trigger('chosen:updated');` nil end |