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

Make Address.find_all_by_name_or_abbr case-insensitive #2043

Merged

Conversation

jordan-brough
Copy link
Contributor

@jordan-brough jordan-brough commented Jun 27, 2017

This is helpful when importing data from third parties. E.g. PayPal.

Note: I believe the spec failures are due to #2045

@@ -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)
Copy link
Member

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)
  )
)

end

context 'by abbr' do
let(:search_term) { 'California' }
Copy link
Member

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.

@@ -6,7 +6,7 @@ class State < Spree::Base
validates :country, :name, presence: true

def self.find_all_by_name_or_abbr(name_or_abbr)
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@jordan-brough
Copy link
Contributor Author

jordan-brough commented Jun 28, 2017

@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.

@jordan-brough jordan-brough force-pushed the case-insensitive-state-search branch from 67dfa8d to 3fc42a9 Compare June 28, 2017 19:08
@solidusio solidusio deleted a comment from houndci-bot Jun 28, 2017
@jordan-brough jordan-brough force-pushed the case-insensitive-state-search branch from 3fc42a9 to 67f7752 Compare June 28, 2017 19:26
@jhawthorn jhawthorn merged commit 3270fdc into solidusio:master Jul 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants