Class: Chosen::MainController

Inherits:
Volt::ModelController
  • Object
show all
Defined in:
app/chosen/controllers/main_controller.rb

Instance Method Summary collapse

Instance Method Details

#before_index_removeObject



38
39
40
# File 'app/chosen/controllers/main_controller.rb', line 38

def before_index_remove
  `$(#{selector}).chosen("destroy");`
end

#blurObject

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

#errorsObject

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_nameObject



82
83
84
# File 'app/chosen/controllers/main_controller.rb', line 82

def field_name
  @field_name
end

#indexObject

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.options
end

#index_readyObject

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
  options = attrs.chosen_options || {}
  options[:width] ||= "100%"
  `$(#{selector}).chosen(#{options.to_n}).change(function(e, params) {
    e.stopPropagation();
    $(".my_select_box").trigger('chosen:change', params);
  });`
end

#labelObject



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

#markedObject



91
92
93
# File 'app/chosen/controllers/main_controller.rb', line 91

def marked
  model_inst.marked_fields[@field_name]
end

#model_instObject

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

#optionsObject



95
96
97
98
99
100
101
102
103
104
# File 'app/chosen/controllers/main_controller.rb', line 95

def options
  attrs.options.then do |options|
    if options[0].is_a?(Hash)
      options_hash = options
    else
      options_hash = options.collect { |option| {value: option, label: option }}
    end
    options_hash
  end
end

#random_idObject



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

#selectorObject



51
52
53
# File 'app/chosen/controllers/main_controller.rb', line 51

def selector
  ".#{@unique_klass}"
end

#setup_fieldObject



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_klassObject



47
48
49
# File 'app/chosen/controllers/main_controller.rb', line 47

def unique_klass
  @unique_klass ||= random_id
end

#update_chosenObject



42
43
44
45
# File 'app/chosen/controllers/main_controller.rb', line 42

def update_chosen
  `$(#{selector}).trigger('chosen:updated');`
  nil
end