From 158164d7abf7c5516ecc8f2b53a76bec2a966427 Mon Sep 17 00:00:00 2001 From: Stan Bienaives Date: Sat, 21 Nov 2015 01:32:36 +0100 Subject: [PATCH 1/7] Settings --- Gemfile | 8 +++++++- Gemfile.lock | 34 ++++++++++++++++++++++++++++------ config/example.database.yml | 29 ----------------------------- config/initializers/assets.rb | 2 +- db/structure.sql | 8 ++++---- 5 files changed, 40 insertions(+), 41 deletions(-) delete mode 100644 config/example.database.yml diff --git a/Gemfile b/Gemfile index 0d31318b8dc..e89e763430e 100644 --- a/Gemfile +++ b/Gemfile @@ -57,7 +57,8 @@ gem "actionpack-page_caching" # Omniauth for authentication gem "omniauth" gem "omniauth-openid" -gem "openstreetmap-omniauth-google-oauth2", ">= 0.2.6.1", :require => "omniauth-google-oauth2" +gem "omniauth-google-oauth2" +#gem "openstreetmap-omniauth-google-oauth2", ">= 0.2.6.1", :require => "omniauth-google-oauth2" gem "omniauth-facebook" gem "omniauth-windowslive" @@ -85,6 +86,10 @@ gem "soap4r-ruby1.9" gem "dalli" gem "kgio" +# indexing city in algolia +gem "cities" +gem "algoliasearch" + # Gems useful for development group :development do gem "vendorer" @@ -103,4 +108,5 @@ group :development, :test do gem "konacha" gem "poltergeist" gem "coveralls", :require => false + gem "pry-rails" end diff --git a/Gemfile.lock b/Gemfile.lock index a19d2e7fa60..1ca44e12308 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,6 +39,10 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + addressable (2.3.8) + algoliasearch (1.6.1) + httpclient (~> 2.4) + json (>= 1.5.1) arel (6.0.0) ast (2.0.0) astrolabe (1.3.0) @@ -54,11 +58,14 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + cities (0.3.1) + multi_json (~> 1.8) climate_control (0.0.3) activesupport (>= 3.0) cliver (0.3.2) cocaine (0.5.7) climate_control (>= 0.0.3, < 1.0) + coderay (1.1.0) coffee-rails (4.1.0) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -127,6 +134,7 @@ GEM nokogiri (>= 1.5.9) mail (2.6.3) mime-types (>= 1.16, < 3) + method_source (0.8.2) mime-types (2.6.1) mini_portile (0.6.2) minitest (5.7.0) @@ -155,6 +163,12 @@ GEM rack (~> 1.0) omniauth-facebook (2.0.1) omniauth-oauth2 (~> 1.2) + omniauth-google-oauth2 (0.2.8) + addressable (~> 2.3) + jwt (~> 1.0) + multi_json (~> 1.3) + omniauth (>= 1.1.1) + omniauth-oauth2 (>= 1.1.1) omniauth-oauth2 (1.3.0) oauth2 (~> 1.0) omniauth (~> 1.2) @@ -164,11 +178,6 @@ GEM omniauth-windowslive (0.0.9) multi_json (>= 1.0.3) omniauth-oauth2 (~> 1.0) - openstreetmap-omniauth-google-oauth2 (0.2.6.1) - jwt (~> 1.0) - multi_json (~> 1.3) - omniauth (>= 1.1.1) - omniauth-oauth2 (>= 1.1.1) paperclip (4.2.2) activemodel (>= 3.0.0) activesupport (>= 3.0.0) @@ -183,6 +192,12 @@ GEM multi_json (~> 1.0) websocket-driver (>= 0.2.0) powerpack (0.1.1) + pry (0.10.3) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) + pry-rails (0.3.4) + pry (>= 0.9.10) psych (2.0.13) r2 (0.2.5) rack (1.6.2) @@ -252,6 +267,7 @@ GEM json (~> 1.8) simplecov-html (~> 0.10.0) simplecov-html (0.10.0) + slop (3.6.0) soap4r-ruby1.9 (2.0.5) sprockets (2.12.3) hike (~> 1.2) @@ -295,8 +311,10 @@ PLATFORMS DEPENDENCIES SystemTimer (>= 1.1.3) actionpack-page_caching + algoliasearch autoprefixer-rails bigdecimal (~> 1.1.0) + cities coffee-rails (~> 4.1.0) composite_primary_keys (~> 8.1.0) coveralls @@ -319,12 +337,13 @@ DEPENDENCIES oauth-plugin (>= 0.5.1) omniauth omniauth-facebook + omniauth-google-oauth2 omniauth-openid omniauth-windowslive - openstreetmap-omniauth-google-oauth2 (>= 0.2.6.1) paperclip (~> 4.0) pg poltergeist + pry-rails psych r2 rack-cors @@ -341,3 +360,6 @@ DEPENDENCIES uglifier (>= 1.3.0) validates_email_format_of (>= 1.5.1) vendorer + +BUNDLED WITH + 1.10.6 diff --git a/config/example.database.yml b/config/example.database.yml deleted file mode 100644 index 4c77998da1d..00000000000 --- a/config/example.database.yml +++ /dev/null @@ -1,29 +0,0 @@ -# Using a recent release (9.1 or higher) of PostgreSQL (http://postgresql.org/) is recommended. -# See https://github.com/openstreetmap/openstreetmap-website/blob/master/INSTALL.md#database-setup for detailed setup instructions. -# -development: - adapter: postgresql - database: openstreetmap -# username: openstreetmap -# password: openstreetmap -# host: localhost - encoding: utf8 - -# Warning: The database defined as 'test' will be erased and -# re-generated from your development database when you run 'rake'. -# Do not set this db to the same as development or production. -test: - adapter: postgresql - database: osm_test -# username: osm_test -# password: osm_test -# host: localhost - encoding: utf8 - -production: - adapter: postgresql - database: osm -# username: osm -# password: osm -# host: localhost - encoding: utf8 diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index ffc28393180..1570beca23b 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -1,7 +1,7 @@ # Be sure to restart your server when you modify this file. # Version of your assets, change this if you want to expire all your assets. -Rails.application.config.assets.version = "1.0" +Rails.application.config.assets.version = "1.1" # Add additional assets to the asset load path # Rails.application.config.assets.paths << Emoji.images_path diff --git a/db/structure.sql b/db/structure.sql index c7e26ac66c2..c465b153fcb 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -126,7 +126,7 @@ CREATE TYPE user_status_enum AS ENUM ( CREATE FUNCTION maptile_for_point(bigint, bigint, integer) RETURNS integer LANGUAGE c STRICT - AS '/srv/www/master.osm.compton.nu/db/functions/libpgosm.so', 'maptile_for_point'; + AS '/Users/sebastienvian/Code/openstreetmap-website/db/functions/libpgosm', 'maptile_for_point'; -- @@ -135,7 +135,7 @@ CREATE FUNCTION maptile_for_point(bigint, bigint, integer) RETURNS integer CREATE FUNCTION tile_for_point(integer, integer) RETURNS bigint LANGUAGE c STRICT - AS '/srv/www/master.osm.compton.nu/db/functions/libpgosm.so', 'tile_for_point'; + AS '/Users/sebastienvian/Code/openstreetmap-website/db/functions/libpgosm', 'tile_for_point'; -- @@ -143,8 +143,8 @@ CREATE FUNCTION tile_for_point(integer, integer) RETURNS bigint -- CREATE FUNCTION xid_to_int4(xid) RETURNS integer - LANGUAGE c IMMUTABLE STRICT - AS '/srv/www/master.osm.compton.nu/db/functions/libpgosm.so', 'xid_to_int4'; + LANGUAGE c STRICT + AS '/Users/sebastienvian/Code/openstreetmap-website/db/functions/libpgosm', 'xid_to_int4'; SET default_tablespace = ''; From 75638d78758fa31465b8d5210a0b15a7599b455d Mon Sep 17 00:00:00 2001 From: Stan Bienaives Date: Sat, 21 Nov 2015 12:26:30 +0100 Subject: [PATCH 2/7] Rake task to populate cities index in algola --- lib/tasks/algolia.rake | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lib/tasks/algolia.rake diff --git a/lib/tasks/algolia.rake b/lib/tasks/algolia.rake new file mode 100644 index 00000000000..6181991e215 --- /dev/null +++ b/lib/tasks/algolia.rake @@ -0,0 +1,61 @@ +namespace "algolia" do + desc "Index cities in algolia" + task :index do + + # init Algolia + Algolia.init( + :application_id => "XXXXXXXXXXX", + :api_key => "XXXXXXXXXXXXXXXXXXXXX" + ) + index = Algolia::Index.new("cities") + + # init Cities gem + Cities.data_path = '~/Tmp/cities' + + # fetching countries code from xml + countries = File.open("config/countries.xml") { |f| Nokogiri::XML(f) } + countries = countries.xpath('//country').map do |country| + { + code: country.at_xpath('countryCode').content, + name: country.at_xpath('countryName').content + } + end + + # indexing cities in algolia + countries.each do |country| + + p "" + p "###################################" + p "Indexing county: #{country[:name]}" + p "###################################" + p "" + + Cities.cities_in_country(country[:code]) + .each_slice(1000) do |batch| + + cities = batch.map do |city| + # get city Object + city = city.last + + { + name: city.name, + country: country[:name], + country_code: country[:code], + population: city.population, + _geoloc: { + lat: city.latitude, + lng: city.longitude + } + + } + + end + + p "Indexing 1000" + index.add_objects(cities) + + + end + end + end +end From 93aa2dce0e713090d73346896b5a04dd8984833f Mon Sep 17 00:00:00 2001 From: Stan Bienaives Date: Sat, 21 Nov 2015 12:52:19 +0100 Subject: [PATCH 3/7] Search and diplay cities autocomplete --- app/assets/javascripts/index.js | 1 + .../javascripts/index/search_algolia.js | 70 +++++++++++++++++++ app/assets/stylesheets/common.scss | 1 + app/assets/stylesheets/search.scss | 44 ++++++++++++ app/views/layouts/_search_algolia.html.erb | 30 ++++++++ app/views/layouts/map.html.erb | 4 +- 6 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/index/search_algolia.js create mode 100644 app/assets/stylesheets/search.scss create mode 100644 app/views/layouts/_search_algolia.html.erb diff --git a/app/assets/javascripts/index.js b/app/assets/javascripts/index.js index 5e437c6ec47..fbb4c9b0d35 100644 --- a/app/assets/javascripts/index.js +++ b/app/assets/javascripts/index.js @@ -8,6 +8,7 @@ //= require leaflet.polyline //= require leaflet.query //= require index/search +//= require index/search_algolia //= require index/browse //= require index/export //= require index/notes diff --git a/app/assets/javascripts/index/search_algolia.js b/app/assets/javascripts/index/search_algolia.js new file mode 100644 index 00000000000..b3f6135f947 --- /dev/null +++ b/app/assets/javascripts/index/search_algolia.js @@ -0,0 +1,70 @@ +// Component responsible for displaying the search results +var SearchResultComponent = function ( cities ){ + this.cities = cities; + this.container = $('#sidebar_content'); +} + +SearchResultComponent.prototype = { + + clear: function () { + this.container.html(""); + }, + + render: function ( cities ) { + + this.cities = cities; + this.container.css('display','block'); + this.clear(); + this.displayCities( this.cities ); + }, + + displayCity: function ( city ) { + + var country_code = city.country_code.toLowerCase(); + var search_result = $(" ") + search_result.attr( "href", "#map=7/"+ city._geoloc.lat + "/" + city._geoloc.lng ); + flag = $("
") + block = $("

"+ city.name +"

" + city.country + "

") + flag.appendTo(search_result); + block.appendTo(search_result); + search_result.appendTo(this.container); + }, + + displayCities: function ( cities ) { + + cities.forEach( function ( city ){ + this.displayCity( city ); + }.bind(this)) + } +} + +//initalizer for Algoliasearch +var SearchProxy = function ( ){ + this.client = algoliasearch('F7SOH92SLF', '073e04ff24420c3b1e97c0ecea951392'); + this.index = this.client.initIndex('cities'); +} + + +$(document).ready( function ( ){ + + var ResultComponent = new SearchResultComponent() + var search = new SearchProxy(); + + var search_input = $('.query_wrapper input'); + + // Clear result on focus + search_input.on('focus', function ( e ){ + Component.clear(); + + }) + + // Auto complete search on keyup + search_input.on('keyup', function( e ) { + var query = $(e.target).val(); + + search.index.search(query, { hitsPerPage: 10, page: 0 }, function(err, hits) { + ResultComponent.render( hits.hits ); + }); + }) + +}) diff --git a/app/assets/stylesheets/common.scss b/app/assets/stylesheets/common.scss index 21cecaf4aa6..0182d924f25 100644 --- a/app/assets/stylesheets/common.scss +++ b/app/assets/stylesheets/common.scss @@ -1,4 +1,5 @@ @import "parameters"; +@import "search"; /* Styles common to large and small screens */ diff --git a/app/assets/stylesheets/search.scss b/app/assets/stylesheets/search.scss new file mode 100644 index 00000000000..c7d36248740 --- /dev/null +++ b/app/assets/stylesheets/search.scss @@ -0,0 +1,44 @@ + + + +.search_forms { + .query_wrapper input { + + border: #eee 0.5px; + font-size: 20px; + background: #eee; + } + +} +#sidebar_content { + + .search_results { + display: block; + color: black; + + &:hover { + background-color: #eee; + text-decoration: none + } + height: 50px; + overflow: hidden; + border-bottom: 1px solid #eee; + + .flag { + float: left; + padding: 13px; + } + + .city { + padding-left: 50px; + padding-top: 3px; + + .country { + color: #999; + } + } + } + +} + + diff --git a/app/views/layouts/_search_algolia.html.erb b/app/views/layouts/_search_algolia.html.erb new file mode 100644 index 00000000000..7360bc97789 --- /dev/null +++ b/app/views/layouts/_search_algolia.html.erb @@ -0,0 +1,30 @@ +<% content_for :head do %> + <%= javascript_include_tag "//cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js" %> +<% end %> + +
+
+
+ <%= text_field_tag "query", params[:query], :placeholder => t("site.search.search"), :autofocus => autofocus, :class => 'overflow' , :autocomplete => 'off' %> +
+
+ +
+
+ +
+ <%= image_tag "marker-green.png", :class => "routing_marker", :data => { :type => "from" }, :draggable => "true" %> + <%= text_field_tag "route_from", params[:from], :placeholder => t('site.search.from') %> +
+
+ <%= image_tag "marker-red.png", :class => "routing_marker", :data => { :type => "to" }, :draggable => "true" %> + <%= text_field_tag "route_to" , params[:to] , :placeholder => t('site.search.to') %> +
+
+ + <%= submit_tag t('site.search.submit_text'), :class => "routing_go" %> +
+ +
<%= image_tag "searching.gif" %>
+
+
diff --git a/app/views/layouts/map.html.erb b/app/views/layouts/map.html.erb index 6ae087b06d7..c72ac871137 100644 --- a/app/views/layouts/map.html.erb +++ b/app/views/layouts/map.html.erb @@ -17,12 +17,12 @@ <% end %> <% content_for :header do %> - <%= render :partial => "layouts/search", :locals => { :autofocus => false } %> + <%= render :partial => "layouts/search_algolia", :locals => { :autofocus => false } %> <% end %> <% content_for :content do %>