Commit 55ae4eb8eac8818390cb57d890de9c2cbb106f51

Authored by Luciano Prestes
1 parent 8b432927

Refactory autocomplete javascript and ajax request

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
controllers/mpog_software_plugin_controller.rb
... ... @@ -125,29 +125,23 @@ class MpogSoftwarePluginController &lt; ApplicationController
125 125 render :json=>state_list.collect {|state| state.name }.to_json
126 126 end
127 127  
128   - def get_databases
129   - return render :json=>{} unless request.xhr? and params[:query]
130   -
131   - data = DatabaseDescription.where("name ILIKE ?", "%#{params[:query]}%").select("id, name").collect {|db|
132   - {:id=>db.id, :label=>db.name}
133   - }
134   - other = [DatabaseDescription.select("id, name").last].collect { |db|
135   - {:id=>db.id, :label=>db.name}
136   - }
137   -
138   - # Always has other in the list
139   - data |= other
140   -
141   - render :json=> data
142   - end
143   -
144   - def get_languages
145   - return render :json=>{} unless request.xhr? and params[:query]
  128 + def get_field_data
  129 + return render :json=>{} if !request.xhr? or params[:query].nil? or params[:field].nil?
  130 +
  131 + model = nil
  132 + case params[:field]
  133 + when "database"
  134 + model = DatabaseDescription
  135 + when "software_language"
  136 + model = ProgrammingLanguage
  137 + else
  138 + model = DatabaseDescription
  139 + end
146 140  
147   - data = ProgrammingLanguage.where("name ILIKE ?", "%#{params[:query]}%").select("id, name").collect {|db|
  141 + data = model.where("name ILIKE ?", "%#{params[:query]}%").select("id, name").collect {|db|
148 142 {:id=>db.id, :label=>db.name}
149 143 }
150   - other = [ProgrammingLanguage.select("id, name").last].collect { |db|
  144 + other = [model.select("id, name").last].collect { |db|
151 145 {:id=>db.id, :label=>db.name}
152 146 }
153 147  
... ...
public/mpog-software-validations.js
... ... @@ -22,38 +22,21 @@
22 22 }
23 23  
24 24 function database_autocomplete() {
25   - jQuery(".database_autocomplete").autocomplete({
26   - source : function(request, response){
27   - jQuery.ajax({
28   - type: "GET",
29   - url: "/plugin/mpog_software/get_databases",
30   - data: {query: request.term},
31   - success: function(result){
32   - response(result);
33   - }
34   - });
35   - },
36   -
37   - minLength: 1,
38   -
39   - select : function (event, selected) {
40   - var description = get_hidden_description_field(this, ".database_description_id");
41   - description.val(selected.item.id);
42   - description.attr("data-label", selected.item.label);
43   - }
44   - }).blur(function(){
45   - verify_autocomplete(this, ".database_description_id");
46   - });
  25 + enable_autocomplete("database", ".database_description_id", ".database_autocomplete");
47 26 }
48 27  
49 28  
50 29 function language_autocomplete() {
51   - jQuery(".language_autocomplete").autocomplete({
  30 + enable_autocomplete("software_language", ".programming_language_id", ".language_autocomplete");
  31 + }
  32 +
  33 + function enable_autocomplete(field_name, field_value_class, autocomplete_class) {
  34 + jQuery(autocomplete_class).autocomplete({
52 35 source : function(request, response){
53 36 jQuery.ajax({
54 37 type: "GET",
55   - url: "/plugin/mpog_software/get_languages",
56   - data: {query: request.term},
  38 + url: "/plugin/mpog_software/get_field_data",
  39 + data: {query: request.term, field: field_name},
57 40 success: function(result){
58 41 response(result);
59 42 }
... ... @@ -63,12 +46,12 @@
63 46 minLength: 0,
64 47  
65 48 select : function (event, selected) {
66   - var description = get_hidden_description_field(this, ".programming_language_id");
  49 + var description = get_hidden_description_field(this, field_value_class);
67 50 description.val(selected.item.id);
68 51 description.attr("data-label", selected.item.label);
69 52 }
70 53 }).blur(function(){
71   - verify_autocomplete(this, ".programming_language_id");
  54 + verify_autocomplete(this, field_value_class);
72 55 });
73 56 }
74 57  
... ...