Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for searching on multiple languages. #227

Merged
merged 1 commit into from
Sep 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
bundler_args: --without assets:development:production
language: ruby
rvm:
- 2.1.1
- 2.1.2
addons:
postgresql: "9.3"
before_script:
Expand Down
5 changes: 4 additions & 1 deletion app/models/concerns/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Search
extend ActiveSupport::Concern

included do
scope :language, ->(language) { where('languages @@ :q', q: language) }
scope :keyword, ->(keyword) { keyword_search(keyword) }
scope :category, ->(category) { joins(services: :categories).where(categories: { name: category }) }

Expand Down Expand Up @@ -57,6 +56,10 @@ module Search
module ClassMethods
require 'exceptions'

def language(lang)
where('languages && ARRAY[?]', lang)
end

def service_area(sa)
joins(:services).where('services.service_areas @@ :q', q: sa)
end
Expand Down
6 changes: 0 additions & 6 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@ class Location < ActiveRecord::Base

serialize :emails, Array

serialize :languages, Array
# enumerize :languages, in: [:arabic, :cantonese, :french, :german,
# :mandarin, :polish, :portuguese, :russian, :spanish, :tagalog, :urdu,
# :vietnamese,
# ], multiple: true, scope: true

serialize :urls, Array

auto_strip_attributes :description, :hours, :name, :short_desc,
Expand Down
2 changes: 1 addition & 1 deletion app/validators/service_area_validator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ServiceAreaValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?
return if value.blank? || SETTINGS[:valid_service_areas].blank?
default_message = "#{value} #{I18n.t('errors.messages.invalid_service_area')}"

unless SETTINGS[:valid_service_areas].include?(value)
Expand Down
29 changes: 29 additions & 0 deletions db/migrate/20140909031145_convert_languages_to_array_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ConvertLanguagesToArrayType < ActiveRecord::Migration
def up
execute "drop index locations_languages"
change_column :locations, :languages, "text[] USING (string_to_array(languages, '\n- '))", default: []
add_index :locations, :languages, using: 'gin'

Location.find_each do |loc|
if loc.languages.nil?
loc.update(languages: [])
else
clean_langs = loc.languages.select { |lang| lang != '---' }.
map { |lang| lang.strip.gsub("\n", '') }
loc.update(languages: clean_langs)
end
end
end

def down
execute "drop index index_locations_on_languages"
change_column :locations, :languages, "text USING (array_to_string(languages, ','))"

Location.find_each do |loc|
next if loc.languages.blank?
loc.update(languages: loc.languages.split(','))
end

execute "create index locations_languages on locations using gin(to_tsvector('english', languages))"
end
end
Loading