-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Make Address.find_all_by_name_or_abbr case-insensitive #2043
Make Address.find_all_by_name_or_abbr case-insensitive #2043
Conversation
core/app/models/spree/state.rb
Outdated
@@ -6,7 +6,7 @@ class State < Spree::Base | |||
validates :country, :name, presence: true | |||
|
|||
def self.find_all_by_name_or_abbr(name_or_abbr) | |||
where('name = ? OR abbr = ?', name_or_abbr, name_or_abbr) | |||
where('lower(name) = lower(:name_or_abbr) OR lower(abbr) = lower(:name_or_abbr)', name_or_abbr: name_or_abbr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than using lower
using arel matches would be preferred. This will use ILIKE
in postgres, and LIKE
in MySQL.
These types of comparisons can make better use of indexes than relying upon the SQL function return value.
Example:
where(
arel_table[:name].matches(name_or_abbr).or(
arel_table[:abbr].matches(name_or_abbr)
)
)
core/spec/models/spree/state_spec.rb
Outdated
end | ||
|
||
context 'by abbr' do | ||
let(:search_term) { 'California' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't match the context.
core/app/models/spree/state.rb
Outdated
@@ -6,7 +6,7 @@ class State < Spree::Base | |||
validates :country, :name, presence: true | |||
|
|||
def self.find_all_by_name_or_abbr(name_or_abbr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer by_name_or_abbr
or with_name_or_abbr
. Could just be me, but I'd expect find_all
to perform a query (maybe because of old rails 2.x's find(:all)
?) and this just builds a scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely agree. I'll switch to with_name_or_abbr
with a deprecation for the old method.
@gmacdougall @jhawthorn updated. If all looks good I'll squash these down. Update: #2045 was causing red specs so I went ahead and squashed and rebased on latest master. |
67dfa8d
to
3fc42a9
Compare
This is helpful when importing data from third parties. E.g. PayPal.
3fc42a9
to
67f7752
Compare
This is helpful when importing data from third parties. E.g. PayPal.
Note: I believe the spec failures are due to #2045