Commit 2f18b880fb262772fc72ba663677349677215b8b

Authored by MoisesMachado
1 parent 9e45dbee

ActionItem527: change the selection of georrefereced regions to a select of cities by state


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2169 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/search_controller.rb
@@ -138,6 +138,11 @@ class SearchController < ApplicationController @@ -138,6 +138,11 @@ class SearchController < ApplicationController
138 [ :events, N_('Events') ] 138 [ :events, N_('Events') ]
139 ] 139 ]
140 140
  141 + def cities
  142 + @cities = City.find(:all, :order => 'name', :conditions => ['parent_id = ? and lat is not null and lng is not null', params[:state_id]])
  143 + render :action => 'cities', :layout => false
  144 + end
  145 +
141 def complete_region 146 def complete_region
142 # FIXME this logic should be in the model 147 # FIXME this logic should be in the model
143 @regions = Region.find(:all, :conditions => [ '(name like ? or name like ?) and lat is not null and lng is not null', '%' + params[:region][:name] + '%', '%' + params[:region][:name].capitalize + '%' ]) 148 @regions = Region.find(:all, :conditions => [ '(name like ? or name like ?) and lat is not null and lng is not null', '%' + params[:region][:name] + '%', '%' + params[:region][:name].capitalize + '%' ])
@@ -150,7 +155,7 @@ class SearchController < ApplicationController @@ -150,7 +155,7 @@ class SearchController < ApplicationController
150 @product_category = ProductCategory.find(params[:product_category]) if params[:product_category] 155 @product_category = ProductCategory.find(params[:product_category]) if params[:product_category]
151 156
152 # FIXME name is not unique 157 # FIXME name is not unique
153 - @region = Region.find_by_name(params[:region][:name]) if params[:region] 158 + @region = City.find_by_id(params[:city]) if params[:city]
154 159
155 # how many assets we are searching for? 160 # how many assets we are searching for?
156 number_of_result_assets = @searching.values.select{|v| v}.size 161 number_of_result_assets = @searching.values.select{|v| v}.size
@@ -176,7 +181,6 @@ class SearchController < ApplicationController @@ -176,7 +181,6 @@ class SearchController < ApplicationController
176 end 181 end
177 182
178 render :action => 'index' 183 render :action => 'index'
179 - render :text => 'bla'  
180 end 184 end
181 185
182 alias :assets :index 186 alias :assets :index
app/helpers/application_helper.rb
@@ -483,25 +483,6 @@ module ApplicationHelper @@ -483,25 +483,6 @@ module ApplicationHelper
483 html.join "\n" 483 html.join "\n"
484 end 484 end
485 485
486 -# def select_city(name, top_level='Nacional')  
487 -# city_field_name = "#{object}[#{method}]"  
488 -# state_field_name = "#{object}_#{method}_state"  
489 -# region_field_name = "#{object}_#{method}_region"  
490 -#  
491 -# selected_state = nil  
492 -# selected_region = nil  
493 -#  
494 -# regions = Region.find_by_name(top_level).children  
495 -#  
496 -# select_tag(region_field_name, options_for_select(regions.map {|r| [r.name, r.id] } + ['---','']) +  
497 -# select_tag(state_field_name, options_for_select(['---', ''])) +  
498 -# select_tag(city_fied_name, options_for_select(['---',''])) +  
499 -#  
500 -# observe_field(country_field_name, :update => state_field_name, :url => { :controller => 'geography', :action => 'states' }, :with => 'country' ) +  
501 -# observe_field(country_field_name, :update => city_field_name, :url => { :controller => 'geography', :action => 'cities_by_country' }, :with => 'country') +  
502 -# observe_field(state_field_name, :update => city_field_name, :url => { :controller => 'geography', :action => 'cities' }, :with => 'state_id')  
503 -# end  
504 -  
505 def file_field_or_thumbnail(label, image, i) 486 def file_field_or_thumbnail(label, image, i)
506 display_form_field label, ( 487 display_form_field label, (
507 render :partial => (image && image.valid? ? 'shared/show_thumbnail' : 'shared/change_image'), 488 render :partial => (image && image.valid? ? 'shared/show_thumbnail' : 'shared/change_image'),
app/helpers/forms_helper.rb
@@ -24,7 +24,7 @@ module FormsHelper @@ -24,7 +24,7 @@ module FormsHelper
24 text_field_tag( name, value, options ) 24 text_field_tag( name, value, options )
25 end 25 end
26 26
27 - def labelled_select( human_name, name, value_method, text_method, selected, collection, options ) 27 + def labelled_select( human_name, name, value_method, text_method, selected, collection, options={} )
28 options[:id] ||= 'select-' + FormsHelper.next_id_number 28 options[:id] ||= 'select-' + FormsHelper.next_id_number
29 content_tag('label', human_name, :for => options[:id]) + 29 content_tag('label', human_name, :for => options[:id]) +
30 select_tag( name, options_from_collection_for_select(collection, value_method, text_method, selected), options) 30 select_tag( name, options_from_collection_for_select(collection, value_method, text_method, selected), options)
@@ -53,6 +53,20 @@ module FormsHelper @@ -53,6 +53,20 @@ module FormsHelper
53 javascript_tag('new Autocompleter.Local(%s, %s, %s)' % [ id.to_json, "autocomplete-for-#{id}".to_json, choices.to_json ] ) 53 javascript_tag('new Autocompleter.Local(%s, %s, %s)' % [ id.to_json, "autocomplete-for-#{id}".to_json, choices.to_json ] )
54 end 54 end
55 55
  56 + def select_city
  57 + states = State.find(:all, :order => 'name')
  58 + states = [State.new(:name => '---')] + states
  59 + cities = [City.new(:name => '---')]
  60 +
  61 + state_id = 'state-' + FormsHelper.next_id_number
  62 + city_id = 'city-' + FormsHelper.next_id_number
  63 +
  64 + content_tag('div', labelled_select(_('State:'), 'state', :id, :name, nil, states, :id => state_id), :class => 'select_state_for_origin' ) +
  65 + content_tag('div', labelled_select(_('City:'), 'city', :id, :name, nil, cities, :id => city_id), :class => 'select_city_for_origin' ) +
  66 +
  67 + observe_field(state_id, :update => city_id, :url => { :controller => 'search', :action => 'cities' }, :with => 'state_id')
  68 + end
  69 +
56 protected 70 protected
57 def self.next_id_number 71 def self.next_id_number
58 if defined? @@id_num 72 if defined? @@id_num
app/models/category.rb
@@ -8,12 +8,6 @@ class Category < ActiveRecord::Base @@ -8,12 +8,6 @@ class Category < ActiveRecord::Base
8 validates_inclusion_of :display_color, :in => [ 1, 2, 3, 4, nil ] 8 validates_inclusion_of :display_color, :in => [ 1, 2, 3, 4, nil ]
9 validates_uniqueness_of :display_color, :scope => :environment_id, :if => (lambda { |cat| ! cat.display_color.nil? }), :message => N_('%{fn} was already assigned to another category.') 9 validates_uniqueness_of :display_color, :scope => :environment_id, :if => (lambda { |cat| ! cat.display_color.nil? }), :message => N_('%{fn} was already assigned to another category.')
10 10
11 - def validate  
12 - if self.parent && (self.class != self.parent.class)  
13 - self.errors.add(:type, _("%{fn} must be the same as the parents'"))  
14 - end  
15 - end  
16 -  
17 # Finds all top level categories for a given environment. 11 # Finds all top level categories for a given environment.
18 def self.top_level_for(environment) 12 def self.top_level_for(environment)
19 self.find(:all, :conditions => ['parent_id is null and environment_id = ?', environment.id ]) 13 self.find(:all, :conditions => ['parent_id is null and environment_id = ?', environment.id ])
app/models/city.rb 0 → 100644
@@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
  1 +class City < Region
  2 +end
app/models/state.rb 0 → 100644
@@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
  1 +class State < Region
  2 +end
app/views/search/_sellers_form.rhtml
@@ -9,14 +9,13 @@ @@ -9,14 +9,13 @@
9 </dir> 9 </dir>
10 </div> 10 </div>
11 11
  12 + <div class="formfield search-from-opt">
  13 + <%= select_city %>
  14 + </div>
  15 +
12 <div class="formfield search-distance-opt"> 16 <div class="formfield search-distance-opt">
13 <%= labelled_text_field _('Distance (km): '), 'radius' %> 17 <%= labelled_text_field _('Distance (km): '), 'radius' %>
14 </div> 18 </div>
15 - <div class="formfield search-from-opt">  
16 - <%= labelled_text_field _('From: '), 'region[name]', nil, :id => 'search_region_block' %>  
17 - <div id="search_region_block_auto_complete" class="auto-complete"></div>  
18 - <%= auto_complete_field('search_region_block', :url => {:controller => 'search', :action => 'complete_region'}) %>  
19 - </div>  
20 19
21 <div class="button-bar"> 20 <div class="button-bar">
22 <%= submit_tag _('Search'), :class => 'button with-text icon-search' %> 21 <%= submit_tag _('Search'), :class => 'button with-text icon-search' %>
db/migrate/044_update_regions_to_become_states_and_cities.rb 0 → 100644
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +class UpdateRegionsToBecomeStatesAndCities < ActiveRecord::Migration
  2 + def self.up
  3 + execute "update categories set type = 'State' where (select id from categories where type = 'Region' and parent_id in (select id from categories where type = 'Region' and name = 'Nacional'))"
  4 +
  5 + execute "update categories set type = 'City' where parent_id in (select id from categories where type = 'State')"
  6 + end
  7 +
  8 + def self.down
  9 + end
  10 +end
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 # 9 #
10 # It's strongly recommended to check this file into your version control system. 10 # It's strongly recommended to check this file into your version control system.
11 11
12 -ActiveRecord::Schema.define(:version => 43) do 12 +ActiveRecord::Schema.define(:version => 44) do
13 13
14 create_table "article_versions", :force => true do |t| 14 create_table "article_versions", :force => true do |t|
15 t.integer "article_id" 15 t.integer "article_id"
test/functional/search_controller_test.rb
@@ -528,7 +528,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -528,7 +528,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase
528 end 528 end
529 529
530 should 'find profiles by radius and region' do 530 should 'find profiles by radius and region' do
531 - region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0) 531 + city = City.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0)
532 ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0) 532 ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0)
533 p1 = create_user('test2').person 533 p1 = create_user('test2').person
534 p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.save! 534 p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.save!
@@ -536,7 +536,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -536,7 +536,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase
536 p2 = create_user('test4').person 536 p2 = create_user('test4').person
537 p2.name = 'test 4'; p2.lat = 30.0; p2.lng = 30.0; p2.save! 537 p2.name = 'test 4'; p2.lat = 30.0; p2.lng = 30.0; p2.save!
538 538
539 - get :index, :region => { :name => region.name }, :radius => 10, :query => 'test' 539 + get :index, :city => city.id, :radius => 10, :query => 'test'
540 540
541 assert_includes assigns('results')[:enterprises], ent1 541 assert_includes assigns('results')[:enterprises], ent1
542 assert_not_includes assigns('results')[:enterprises], ent2 542 assert_not_includes assigns('results')[:enterprises], ent2
@@ -586,6 +586,35 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -586,6 +586,35 @@ class SearchControllerTest &lt; Test::Unit::TestCase
586 assert_not_includes assigns(:regions), r2 586 assert_not_includes assigns(:regions), r2
587 assert_no_tag :tag => 'ul', :descendant => { :tag => 'li', :content => r2.name } 587 assert_no_tag :tag => 'ul', :descendant => { :tag => 'li', :content => r2.name }
588 end 588 end
  589 +
  590 + should 'return options of cities by its state' do
  591 + state1 = State.create!(:name => 'State One', :environment => Environment.default)
  592 + state2 = State.create!(:name => 'State Two', :environment => Environment.default)
  593 + city1 = City.create!(:name => 'City One', :environment => Environment.default, :lat => 111.07, :lng => '88.9', :parent => state1)
  594 + city2 = City.create!(:name => 'City Two', :environment => Environment.default, :lat => 111.07, :lng => '88.9', :parent => state2)
  595 +
  596 + get :cities, :state_id => state1.id
  597 + assert_includes assigns(:cities), city1
  598 + assert_tag :tag => 'option', :content => city1.name, :attributes => {:value => city1.id}
  599 + assert_not_includes assigns(:cities), city2
  600 + assert_no_tag :tag => 'option', :content => city2.name, :attributes => {:value => city2.id}
  601 + end
  602 +
  603 + should 'render cities results without layout' do
  604 + get :cities, :state_id => 1
  605 + assert_no_tag :tag => 'body'
  606 + end
  607 +
  608 + should 'list only georeferenced cities' do
  609 + state = State.create!(:name => 'State One', :environment => Environment.default)
  610 + city1 = City.create!(:name => 'City One', :environment => Environment.default, :lat => 111.07, :lng => '88.9', :parent => state)
  611 + city2 = City.create!(:name => 'City Two', :environment => Environment.default, :parent => state)
  612 +
  613 + get :cities, :state_id => state.id
  614 +
  615 + assert_includes assigns(:cities), city1
  616 + assert_not_includes assigns(:cities), city2
  617 + end
589 618
590 should 'search for events' do 619 should 'search for events' do
591 person = create_user('teste').person 620 person = create_user('teste').person
test/unit/city_test.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class CityTest < ActiveSupport::TestCase
  4 + # Replace this with your real tests.
  5 + def test_truth
  6 + assert true
  7 + end
  8 +end
test/unit/state_test.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class StateTest < ActiveSupport::TestCase
  4 + # Replace this with your real tests.
  5 + def test_truth
  6 + assert true
  7 + end
  8 +end