Commit 03254351531bde27210efaef9f25c0529fb00b9a
Exists in
master
and in
29 other branches
Merge branch 'AI3003-City_State_AutoComplete' of https://gitlab.com/unb-gama/noo…
…sfero into AI3003-City_State_AutoComplete
Showing
8 changed files
with
134 additions
and
20 deletions
Show diff stats
app/controllers/my_profile/maps_controller.rb
@@ -31,23 +31,11 @@ class MapsController < MyProfileController | @@ -31,23 +31,11 @@ class MapsController < MyProfileController | ||
31 | end | 31 | end |
32 | 32 | ||
33 | def search_city | 33 | def search_city |
34 | - | ||
35 | - term = params[:term]; | ||
36 | - | ||
37 | - regions = NationalRegion.search_city(term + "%", true).map {|r|{ :label => r.city , :category => r.state}} | ||
38 | - | ||
39 | - render :json => regions | ||
40 | - | 34 | + render :json => MapsHelper.search_city(params[:term]) |
41 | end | 35 | end |
42 | 36 | ||
43 | def search_state | 37 | def search_state |
44 | - | ||
45 | - term = params[:term]; | ||
46 | - | ||
47 | - regions = NationalRegion.search_state(term + "%", true).map {|r|{ :label => r.state}} | ||
48 | - | ||
49 | - render :json => regions | ||
50 | - | 38 | + render :json => MapsHelper.search_state(params[:term]) |
51 | end | 39 | end |
52 | 40 | ||
53 | end | 41 | end |
app/controllers/public/account_controller.rb
@@ -291,6 +291,23 @@ class AccountController < ApplicationController | @@ -291,6 +291,23 @@ class AccountController < ApplicationController | ||
291 | render :text => user_data.to_json, :layout => false, :content_type => "application/javascript" | 291 | render :text => user_data.to_json, :layout => false, :content_type => "application/javascript" |
292 | end | 292 | end |
293 | 293 | ||
294 | + def search_cities | ||
295 | + if request.xhr? and params[:state_name] and params[:city_name] | ||
296 | + render :json => MapsHelper.search_city(params[:city_name], params[:state_name]) | ||
297 | + else | ||
298 | + render :json => [].to_json | ||
299 | + end | ||
300 | + end | ||
301 | + | ||
302 | + def search_state | ||
303 | + if request.xhr? and params[:state_name] | ||
304 | + render :json => MapsHelper.search_state(params[:state_name]) | ||
305 | + else | ||
306 | + render :json => [].to_json | ||
307 | + end | ||
308 | + end | ||
309 | + | ||
310 | + | ||
294 | protected | 311 | protected |
295 | 312 | ||
296 | def redirect? | 313 | def redirect? |
@@ -0,0 +1,14 @@ | @@ -0,0 +1,14 @@ | ||
1 | +module MapsHelper | ||
2 | + def self.search_city term, state="" | ||
3 | + cities = if state.empty? | ||
4 | + NationalRegion.search_city(term + "%", true) | ||
5 | + else | ||
6 | + NationalRegion.search_city(term + "%", true, state) | ||
7 | + end | ||
8 | + cities.map {|r|{ :label => r.city , :category => r.state}} | ||
9 | + end | ||
10 | + | ||
11 | + def self.search_state term | ||
12 | + NationalRegion.search_state(term + "%", true).map {|r|{ :label => r.state}} | ||
13 | + end | ||
14 | +end |
app/models/national_region.rb
@@ -12,7 +12,7 @@ class NationalRegion < ActiveRecord::Base | @@ -12,7 +12,7 @@ class NationalRegion < ActiveRecord::Base | ||
12 | adtional_contions = ""; | 12 | adtional_contions = ""; |
13 | 13 | ||
14 | if like | 14 | if like |
15 | - operator = "like" | 15 | + operator = "ilike" |
16 | find_return = :all | 16 | find_return = :all |
17 | end | 17 | end |
18 | 18 | ||
@@ -41,7 +41,7 @@ class NationalRegion < ActiveRecord::Base | @@ -41,7 +41,7 @@ class NationalRegion < ActiveRecord::Base | ||
41 | find_return = :first | 41 | find_return = :first |
42 | 42 | ||
43 | if like | 43 | if like |
44 | - operator = "like" | 44 | + operator = "ilike" |
45 | find_return = :all | 45 | find_return = :all |
46 | end | 46 | end |
47 | 47 |
app/views/profile_editor/_person_form.rhtml
@@ -19,8 +19,8 @@ | @@ -19,8 +19,8 @@ | ||
19 | <%= optional_field(@person, 'birth_date', labelled_form_field(_('Birth date'), '<div class="select-birth-date">' + pick_date(:profile_data, :birth_date, {:start_year => (Date.today.year - 100), :end_year => (Date.today.year - 5)}) + '</div>')) %> | 19 | <%= optional_field(@person, 'birth_date', labelled_form_field(_('Birth date'), '<div class="select-birth-date">' + pick_date(:profile_data, :birth_date, {:start_year => (Date.today.year - 100), :end_year => (Date.today.year - 5)}) + '</div>')) %> |
20 | <%= optional_field(@person, 'nationality', f.text_field(:nationality, :rel => _('Nationality'))) %> | 20 | <%= optional_field(@person, 'nationality', f.text_field(:nationality, :rel => _('Nationality'))) %> |
21 | <%= optional_field(@person, 'country', select_country(_('Country'), 'profile_data', 'country', {:class => 'type-select'})) %> | 21 | <%= optional_field(@person, 'country', select_country(_('Country'), 'profile_data', 'country', {:class => 'type-select'})) %> |
22 | -<%= optional_field(@person, 'state', f.text_field(:state, :rel => _('State'))) %> | ||
23 | -<%= optional_field(@person, 'city', f.text_field(:city, :rel => _('City'))) %> | 22 | +<%= optional_field(@person, 'state', f.text_field(:state, :id => 'state_field', :rel => _('State'))) %> |
23 | +<%= optional_field(@person, 'city', f.text_field(:city, :id => 'city_field', :rel => _('City'))) %> | ||
24 | <%= optional_field(@person, 'zip_code', labelled_form_field(_('ZIP code'), text_field(:profile_data, :zip_code, :rel => _('ZIP code')))) %> | 24 | <%= optional_field(@person, 'zip_code', labelled_form_field(_('ZIP code'), text_field(:profile_data, :zip_code, :rel => _('ZIP code')))) %> |
25 | <%= optional_field(@person, 'address', labelled_form_field(_('Address (street and number)'), text_field(:profile_data, :address, :rel => _('Address')))) %> | 25 | <%= optional_field(@person, 'address', labelled_form_field(_('Address (street and number)'), text_field(:profile_data, :address, :rel => _('Address')))) %> |
26 | <%= optional_field(@person, 'address_reference', labelled_form_field(_('Address reference'), text_field(:profile_data, :address_reference, :rel => _('Address reference')))) %> | 26 | <%= optional_field(@person, 'address_reference', labelled_form_field(_('Address reference'), text_field(:profile_data, :address_reference, :rel => _('Address reference')))) %> |
@@ -36,6 +36,7 @@ | @@ -36,6 +36,7 @@ | ||
36 | </div> | 36 | </div> |
37 | <% end %> | 37 | <% end %> |
38 | 38 | ||
39 | +<%= javascript_include_tag('city_state_validation') %> | ||
39 | <script type='text/javascript'> | 40 | <script type='text/javascript'> |
40 | function toggle_text_field(id, span_id) { | 41 | function toggle_text_field(id, span_id) { |
41 | if ($(id).value == "Others") { | 42 | if ($(id).value == "Others") { |
app/views/shared/_organization_custom_fields.rhtml
@@ -13,8 +13,8 @@ | @@ -13,8 +13,8 @@ | ||
13 | <%= optional_field(profile, 'address_reference', labelled_form_field(_('Address reference'), text_field(object_name, :address_reference))) %> | 13 | <%= optional_field(profile, 'address_reference', labelled_form_field(_('Address reference'), text_field(object_name, :address_reference))) %> |
14 | <%= optional_field(profile, 'district', labelled_form_field(_('District'), text_field(object_name, :district))) %> | 14 | <%= optional_field(profile, 'district', labelled_form_field(_('District'), text_field(object_name, :district))) %> |
15 | <%= optional_field(profile, 'zip_code', labelled_form_field(_('ZIP code'), text_field(object_name, :zip_code))) %> | 15 | <%= optional_field(profile, 'zip_code', labelled_form_field(_('ZIP code'), text_field(object_name, :zip_code))) %> |
16 | -<%= optional_field(profile, 'city', f.text_field(:city)) %> | ||
17 | -<%= optional_field(profile, 'state', f.text_field(:state)) %> | 16 | +<%= optional_field(profile, 'city', f.text_field(:city, :id =>'city_field')) %> |
17 | +<%= optional_field(profile, 'state', f.text_field(:state,:id =>'state_field')) %> | ||
18 | <%= optional_field(profile, 'country', select_country(_('Country'), object_name, 'country', {:class => 'type-select'})) %> | 18 | <%= optional_field(profile, 'country', select_country(_('Country'), object_name, 'country', {:class => 'type-select'})) %> |
19 | <%= optional_field(profile, 'tag_list', f.text_field(:tag_list)) %> | 19 | <%= optional_field(profile, 'tag_list', f.text_field(:tag_list)) %> |
20 | 20 | ||
@@ -29,3 +29,4 @@ | @@ -29,3 +29,4 @@ | ||
29 | <%= optional_field(profile, 'acronym', f.text_field(:acronym)) %> | 29 | <%= optional_field(profile, 'acronym', f.text_field(:acronym)) %> |
30 | <%= optional_field(profile, 'foundation_year', f.text_field(:foundation_year)) %> | 30 | <%= optional_field(profile, 'foundation_year', f.text_field(:foundation_year)) %> |
31 | <% end %> | 31 | <% end %> |
32 | +<%= javascript_include_tag('city_state_validation') %> |
@@ -0,0 +1,55 @@ | @@ -0,0 +1,55 @@ | ||
1 | +(function($){ | ||
2 | + $('[id$="_country"]').change(function(){ | ||
3 | + var country_selected = $('[id$="_country"] option:selected').val() | ||
4 | + if(country_selected == "BR") | ||
5 | + { | ||
6 | + $('#state_field').autocomplete({ | ||
7 | + source : function(request, response){ | ||
8 | + $.ajax({ | ||
9 | + type: "GET", | ||
10 | + url: '/account/search_state', | ||
11 | + data: {state_name: request.term}, | ||
12 | + success: function(result){ | ||
13 | + response(result); | ||
14 | + }, | ||
15 | + error: function(ajax, stat, errorThrown) { | ||
16 | + console.log('Link not found : ' + errorThrown); | ||
17 | + } | ||
18 | + }); | ||
19 | + }, | ||
20 | + | ||
21 | + minLength: 3 | ||
22 | + }); | ||
23 | + | ||
24 | + $('#city_field').autocomplete({ | ||
25 | + source : function(request, response){ | ||
26 | + $.ajax({ | ||
27 | + type: "GET", | ||
28 | + url: '/account/search_cities', | ||
29 | + data: {city_name: request.term, state_name: $("#state_field").val()}, | ||
30 | + success: function(result){ | ||
31 | + response(result); | ||
32 | + }, | ||
33 | + error: function(ajax, stat, errorThrown) { | ||
34 | + console.log('Link not found : ' + errorThrown); | ||
35 | + } | ||
36 | + }); | ||
37 | + }, | ||
38 | + | ||
39 | + minLength: 3 | ||
40 | + }); | ||
41 | + } | ||
42 | + else | ||
43 | + { | ||
44 | + if ($('#state_field').data('autocomplete')) { | ||
45 | + $('#state_field').autocomplete("destroy"); | ||
46 | + $('#state_field').removeData('autocomplete'); | ||
47 | + } | ||
48 | + | ||
49 | + if ($('#city_field').data('autocomplete')) { | ||
50 | + $('#city_field').autocomplete("destroy"); | ||
51 | + $('#city_field').removeData('autocomplete'); | ||
52 | + } | ||
53 | + } | ||
54 | + }) | ||
55 | +})(jQuery); |
test/functional/account_controller_test.rb
@@ -926,6 +926,30 @@ class AccountControllerTest < ActionController::TestCase | @@ -926,6 +926,30 @@ class AccountControllerTest < ActionController::TestCase | ||
926 | assert @response.body.blank? | 926 | assert @response.body.blank? |
927 | end | 927 | end |
928 | 928 | ||
929 | + should "Search for state" do | ||
930 | + create_state_and_city | ||
931 | + | ||
932 | + xhr :get, :search_state, :state_name=>"Rio Grande" | ||
933 | + | ||
934 | + json_response = ActiveSupport::JSON.decode(@response.body) | ||
935 | + label = json_response[0]['label'] | ||
936 | + | ||
937 | + assert_equal label, "Rio Grande do Sul" | ||
938 | + end | ||
939 | + | ||
940 | + should "Search for city" do | ||
941 | + create_state_and_city | ||
942 | + | ||
943 | + xhr :get, :search_cities, :state_name=>"Rio Grande do Sul", :city_name=>"Lavras" | ||
944 | + | ||
945 | + json_response = ActiveSupport::JSON.decode(@response.body) | ||
946 | + label = json_response[0]['label'] | ||
947 | + category = json_response[0]['category'] | ||
948 | + | ||
949 | + assert_equal category, "Rio Grande do Sul" | ||
950 | + assert_equal label, "Lavras do Sul" | ||
951 | + end | ||
952 | + | ||
929 | protected | 953 | protected |
930 | def new_user(options = {}, extra_options ={}) | 954 | def new_user(options = {}, extra_options ={}) |
931 | data = {:profile_data => person_data} | 955 | data = {:profile_data => person_data} |
@@ -954,4 +978,18 @@ class AccountControllerTest < ActionController::TestCase | @@ -954,4 +978,18 @@ class AccountControllerTest < ActionController::TestCase | ||
954 | environment.min_signup_delay = 0 | 978 | environment.min_signup_delay = 0 |
955 | environment.save! | 979 | environment.save! |
956 | end | 980 | end |
981 | + | ||
982 | + def create_state_and_city | ||
983 | + city = 'Lavras do Sul' | ||
984 | + state = 'Rio Grande do Sul' | ||
985 | + | ||
986 | + parent_region = fast_create(NationalRegion, :name => state, | ||
987 | + :national_region_code => '43', | ||
988 | + :national_region_type_id => NationalRegionType::STATE) | ||
989 | + | ||
990 | + fast_create(NationalRegion, :name => city, | ||
991 | + :national_region_code => '431150', | ||
992 | + :national_region_type_id => NationalRegionType::CITY, | ||
993 | + :parent_national_region_code => parent_region.national_region_code) | ||
994 | + end | ||
957 | end | 995 | end |