Updated agent picker to include agent description

Dustin Miller 9 years ago
parent
commit
784142f056
1 changed files with 21 additions and 1 deletions
  1. 21 1
      app/views/agents/_form.html.erb

+ 21 - 1
app/views/agents/_form.html.erb

@@ -23,8 +23,28 @@
23 23
           <% if @agent.new_record? %>
24 24
             <div class="form-group type-select">
25 25
               <%= f.label :type %>
26
-              <%= f.select :type, options_for_select([['Select an Agent Type', 'Agent']] + Agent.types.map(&:to_s).sort.map {|type| [type.gsub(/^.*::/, ''), type] }, @agent.type), {}, :class => 'select2 form-control' %>
26
+              <%= f.select :type, options_for_select([['Select an Agent Type', 'Agent', {title: ''}]] + Agent.types.map {|type| [type.name.gsub(/^.*::/, '').underscore.humanize.titleize, type, {title: type.description.to_s.strip.split(/\r?\n/)[0]}] }, @agent.type), {}, :class => 'form-control' %>
27 27
             </div>
28
+            <script>
29
+              function formatAgentForSelect(agent) {
30
+                var originalOption = agent.element;
31
+                var description = $(originalOption).attr('title');
32
+                description = (/^[a-zA-Z]/.test(description)) ? description : '';
33
+                return '<strong>' + agent.text + '</strong><br/>' + description;
34
+              }
35
+              $(function() {
36
+                $('#agent_type').select2({
37
+                  width: 'resolve',
38
+                  formatResult: formatAgentForSelect,
39
+                  escapeMarkup: function(m) { return m; },
40
+                  matcher: function(term, text, opt) {
41
+                    var description = opt.attr('title');
42
+                    return text.toUpperCase().indexOf(term.toUpperCase())>=0
43
+                      || description.toUpperCase().indexOf(term.toUpperCase())>=0;
44
+                  }
45
+                });
46
+              });
47
+            </script>
28 48
           <% end %>
29 49
         </div>
30 50