Skip to content

Commit

Permalink
Fix problem with search with diacritic
Browse files Browse the repository at this point in the history
  • Loading branch information
rubydev committed Nov 12, 2011
1 parent 8f6e463 commit 510d2ac
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/models/account.rb
Expand Up @@ -58,7 +58,7 @@ class Account < ActiveRecord::Base
scope :assigned_to, lambda { |user| where(:assigned_to => user.id) }

scope :search, lambda { |query|
query = query.gsub(/[^\w\s\-\.']/, '').strip
query = query.gsub(/[^\w\s\-\.'\p{L}]/u, '').strip
where('upper(name) LIKE upper(:m) OR upper(email) LIKE upper(:m)', :m => "%#{query}%")
}

Expand Down
2 changes: 1 addition & 1 deletion app/models/campaign.rb
Expand Up @@ -57,7 +57,7 @@ class Campaign < ActiveRecord::Base
scope :assigned_to, lambda { |user| where('assigned_to = ?', user.id) }

scope :search, lambda { |query|
query = query.gsub(/[^\w\s\-\.']/, '').strip
query = query.gsub(/[^\w\s\-\.'\p{L}]/u, '').strip
where('upper(name) LIKE upper(?)', "%#{query}%")
}

Expand Down
2 changes: 1 addition & 1 deletion app/models/contact.rb
Expand Up @@ -67,7 +67,7 @@ class Contact < ActiveRecord::Base
scope :assigned_to, lambda { |user| { :conditions => ["assigned_to = ?", user.id ] } }

scope :search, lambda { |query|
query = query.gsub(/[^@\w\s\-\.']/, '').strip
query = query.gsub(/[^@\w\s\-\.'\p{L}]/u, '').strip
# We can't be sure that names are always entered in the right order, so we take the query and
# split it into all possible first/last name combinations.
# => "Zhong Fai Gao" matches last name "Zhong Fai" and "Fai Gao"
Expand Down
2 changes: 1 addition & 1 deletion app/models/lead.rb
Expand Up @@ -69,7 +69,7 @@ class Lead < ActiveRecord::Base
scope :assigned_to, lambda { |user| where('assigned_to = ?' , user.id) }

scope :search, lambda { |query|
query = query.gsub(/[^\w\s\-\.']/, '').strip
query = query.gsub(/[^\w\s\-\.'\p{L}]/u, '').strip
where('upper(first_name) LIKE upper(:s) OR upper(last_name) LIKE upper(:s) OR upper(company) LIKE upper(:m) OR upper(email) LIKE upper(:m)', :s => "#{query}%", :m => "%#{query}%")
}

Expand Down
2 changes: 1 addition & 1 deletion app/models/opportunity.rb
Expand Up @@ -60,7 +60,7 @@ class Opportunity < ActiveRecord::Base

# Search by name OR id
scope :search, lambda { |query|
query = query.gsub(/[^\w\s\-\.']/, '').strip
query = query.gsub(/[^\w\s\-\.'\p{L}]/u, '').strip
# postgresql does not like to compare string to integer field
if query =~ /^\d+$/
where('upper(name) LIKE upper(:name) OR opportunities.id = :id', :name => "%#{query}%", :id => query)
Expand Down
2 changes: 1 addition & 1 deletion app/models/task.rb
Expand Up @@ -93,7 +93,7 @@ class Task < ActiveRecord::Base
scope :completed_last_month, where('completed_at >= ? AND completed_at < ?', (Time.zone.now.beginning_of_month.utc - 1.day).beginning_of_month.utc, Time.zone.now.beginning_of_month.utc)

scope :search, lambda { |query|
query = query.gsub(/[^\w\s\-\.']/, '').strip
query = query.gsub(/[^\w\s\-\.'\p{L}]/u, '').strip
where('upper(name) LIKE upper(?)', "%#{query}%")
}

Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -76,7 +76,7 @@ class User < ActiveRecord::Base
scope :by_name, order('first_name, last_name, email')

scope :search, lambda { |query|
query = query.gsub(/[^\w\s\-\.']/, '').strip
query = query.gsub(/[^\w\s\-\.'\p{L}]/u, '').strip
where('upper(username) LIKE upper(:s) OR upper(first_name) LIKE upper(:s) OR upper(last_name) LIKE upper(:s)', :s => "#{query}%")
}

Expand Down

0 comments on commit 510d2ac

Please sign in to comment.