diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 038fca2..3e599e4 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -138,6 +138,11 @@ class SearchController < ApplicationController [ :events, N_('Events') ] ] + def cities + @cities = City.find(:all, :order => 'name', :conditions => ['parent_id = ? and lat is not null and lng is not null', params[:state_id]]) + render :action => 'cities', :layout => false + end + def complete_region # FIXME this logic should be in the model @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 @product_category = ProductCategory.find(params[:product_category]) if params[:product_category] # FIXME name is not unique - @region = Region.find_by_name(params[:region][:name]) if params[:region] + @region = City.find_by_id(params[:city]) if params[:city] # how many assets we are searching for? number_of_result_assets = @searching.values.select{|v| v}.size @@ -176,7 +181,6 @@ class SearchController < ApplicationController end render :action => 'index' - render :text => 'bla' end alias :assets :index diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 6de647f..32018cc 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -483,25 +483,6 @@ module ApplicationHelper html.join "\n" end -# def select_city(name, top_level='Nacional') -# city_field_name = "#{object}[#{method}]" -# state_field_name = "#{object}_#{method}_state" -# region_field_name = "#{object}_#{method}_region" -# -# selected_state = nil -# selected_region = nil -# -# regions = Region.find_by_name(top_level).children -# -# select_tag(region_field_name, options_for_select(regions.map {|r| [r.name, r.id] } + ['---','']) + -# select_tag(state_field_name, options_for_select(['---', ''])) + -# select_tag(city_fied_name, options_for_select(['---',''])) + -# -# observe_field(country_field_name, :update => state_field_name, :url => { :controller => 'geography', :action => 'states' }, :with => 'country' ) + -# observe_field(country_field_name, :update => city_field_name, :url => { :controller => 'geography', :action => 'cities_by_country' }, :with => 'country') + -# observe_field(state_field_name, :update => city_field_name, :url => { :controller => 'geography', :action => 'cities' }, :with => 'state_id') -# end - def file_field_or_thumbnail(label, image, i) display_form_field label, ( render :partial => (image && image.valid? ? 'shared/show_thumbnail' : 'shared/change_image'), diff --git a/app/helpers/forms_helper.rb b/app/helpers/forms_helper.rb index aa947a0..be33d60 100644 --- a/app/helpers/forms_helper.rb +++ b/app/helpers/forms_helper.rb @@ -24,7 +24,7 @@ module FormsHelper text_field_tag( name, value, options ) end - def labelled_select( human_name, name, value_method, text_method, selected, collection, options ) + def labelled_select( human_name, name, value_method, text_method, selected, collection, options={} ) options[:id] ||= 'select-' + FormsHelper.next_id_number content_tag('label', human_name, :for => options[:id]) + select_tag( name, options_from_collection_for_select(collection, value_method, text_method, selected), options) @@ -53,6 +53,20 @@ module FormsHelper javascript_tag('new Autocompleter.Local(%s, %s, %s)' % [ id.to_json, "autocomplete-for-#{id}".to_json, choices.to_json ] ) end + def select_city + states = State.find(:all, :order => 'name') + states = [State.new(:name => '---')] + states + cities = [City.new(:name => '---')] + + state_id = 'state-' + FormsHelper.next_id_number + city_id = 'city-' + FormsHelper.next_id_number + + content_tag('div', labelled_select(_('State:'), 'state', :id, :name, nil, states, :id => state_id), :class => 'select_state_for_origin' ) + + content_tag('div', labelled_select(_('City:'), 'city', :id, :name, nil, cities, :id => city_id), :class => 'select_city_for_origin' ) + + + observe_field(state_id, :update => city_id, :url => { :controller => 'search', :action => 'cities' }, :with => 'state_id') + end + protected def self.next_id_number if defined? @@id_num diff --git a/app/models/category.rb b/app/models/category.rb index 5fd5339..0049a6d 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -8,12 +8,6 @@ class Category < ActiveRecord::Base validates_inclusion_of :display_color, :in => [ 1, 2, 3, 4, nil ] validates_uniqueness_of :display_color, :scope => :environment_id, :if => (lambda { |cat| ! cat.display_color.nil? }), :message => N_('%{fn} was already assigned to another category.') - def validate - if self.parent && (self.class != self.parent.class) - self.errors.add(:type, _("%{fn} must be the same as the parents'")) - end - end - # Finds all top level categories for a given environment. def self.top_level_for(environment) self.find(:all, :conditions => ['parent_id is null and environment_id = ?', environment.id ]) diff --git a/app/models/city.rb b/app/models/city.rb new file mode 100644 index 0000000..f490aae --- /dev/null +++ b/app/models/city.rb @@ -0,0 +1,2 @@ +class City < Region +end diff --git a/app/models/state.rb b/app/models/state.rb new file mode 100644 index 0000000..c659537 --- /dev/null +++ b/app/models/state.rb @@ -0,0 +1,2 @@ +class State < Region +end diff --git a/app/views/search/_sellers_form.rhtml b/app/views/search/_sellers_form.rhtml index f78a8c1..5f81181 100644 --- a/app/views/search/_sellers_form.rhtml +++ b/app/views/search/_sellers_form.rhtml @@ -9,14 +9,13 @@ +
+ <%= select_city %> +
+
<%= labelled_text_field _('Distance (km): '), 'radius' %>
-
- <%= labelled_text_field _('From: '), 'region[name]', nil, :id => 'search_region_block' %> -
- <%= auto_complete_field('search_region_block', :url => {:controller => 'search', :action => 'complete_region'}) %> -
<%= submit_tag _('Search'), :class => 'button with-text icon-search' %> diff --git a/db/migrate/044_update_regions_to_become_states_and_cities.rb b/db/migrate/044_update_regions_to_become_states_and_cities.rb new file mode 100644 index 0000000..11ddb50 --- /dev/null +++ b/db/migrate/044_update_regions_to_become_states_and_cities.rb @@ -0,0 +1,10 @@ +class UpdateRegionsToBecomeStatesAndCities < ActiveRecord::Migration + def self.up + 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'))" + + execute "update categories set type = 'City' where parent_id in (select id from categories where type = 'State')" + end + + def self.down + end +end diff --git a/db/schema.rb b/db/schema.rb index 2f5e80a..f6e9daa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 43) do +ActiveRecord::Schema.define(:version => 44) do create_table "article_versions", :force => true do |t| t.integer "article_id" diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index dab17fd..3244fe2 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -528,7 +528,7 @@ class SearchControllerTest < Test::Unit::TestCase end should 'find profiles by radius and region' do - region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0) + city = City.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0) ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0) p1 = create_user('test2').person p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.save! @@ -536,7 +536,7 @@ class SearchControllerTest < Test::Unit::TestCase p2 = create_user('test4').person p2.name = 'test 4'; p2.lat = 30.0; p2.lng = 30.0; p2.save! - get :index, :region => { :name => region.name }, :radius => 10, :query => 'test' + get :index, :city => city.id, :radius => 10, :query => 'test' assert_includes assigns('results')[:enterprises], ent1 assert_not_includes assigns('results')[:enterprises], ent2 @@ -586,6 +586,35 @@ class SearchControllerTest < Test::Unit::TestCase assert_not_includes assigns(:regions), r2 assert_no_tag :tag => 'ul', :descendant => { :tag => 'li', :content => r2.name } end + + should 'return options of cities by its state' do + state1 = State.create!(:name => 'State One', :environment => Environment.default) + state2 = State.create!(:name => 'State Two', :environment => Environment.default) + city1 = City.create!(:name => 'City One', :environment => Environment.default, :lat => 111.07, :lng => '88.9', :parent => state1) + city2 = City.create!(:name => 'City Two', :environment => Environment.default, :lat => 111.07, :lng => '88.9', :parent => state2) + + get :cities, :state_id => state1.id + assert_includes assigns(:cities), city1 + assert_tag :tag => 'option', :content => city1.name, :attributes => {:value => city1.id} + assert_not_includes assigns(:cities), city2 + assert_no_tag :tag => 'option', :content => city2.name, :attributes => {:value => city2.id} + end + + should 'render cities results without layout' do + get :cities, :state_id => 1 + assert_no_tag :tag => 'body' + end + + should 'list only georeferenced cities' do + state = State.create!(:name => 'State One', :environment => Environment.default) + city1 = City.create!(:name => 'City One', :environment => Environment.default, :lat => 111.07, :lng => '88.9', :parent => state) + city2 = City.create!(:name => 'City Two', :environment => Environment.default, :parent => state) + + get :cities, :state_id => state.id + + assert_includes assigns(:cities), city1 + assert_not_includes assigns(:cities), city2 + end should 'search for events' do person = create_user('teste').person diff --git a/test/unit/city_test.rb b/test/unit/city_test.rb new file mode 100644 index 0000000..06ed459 --- /dev/null +++ b/test/unit/city_test.rb @@ -0,0 +1,8 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class CityTest < ActiveSupport::TestCase + # Replace this with your real tests. + def test_truth + assert true + end +end diff --git a/test/unit/state_test.rb b/test/unit/state_test.rb new file mode 100644 index 0000000..ba8eae6 --- /dev/null +++ b/test/unit/state_test.rb @@ -0,0 +1,8 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class StateTest < ActiveSupport::TestCase + # Replace this with your real tests. + def test_truth + assert true + end +end -- libgit2 0.21.2