Skip to content

Commit

Permalink
Merge pull request #2043 from jordan-brough/case-insensitive-state-se…
Browse files Browse the repository at this point in the history
…arch

Make Address.find_all_by_name_or_abbr case-insensitive
  • Loading branch information
jhawthorn authored Jul 7, 2017
2 parents e821c67 + 67f7752 commit 3270fdc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/app/models/spree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def state_validate
# ensure state_name belongs to country without states, or that it matches a predefined state name/abbr
if state_name.present?
if country.states.present?
states = country.states.find_all_by_name_or_abbr(state_name)
states = country.states.with_name_or_abbr(state_name)

if states.size == 1
self.state = states.first
Expand Down
12 changes: 10 additions & 2 deletions core/app/models/spree/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ 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)
scope :with_name_or_abbr, ->(name_or_abbr) do
where(
arel_table[:name].matches(name_or_abbr).or(
arel_table[:abbr].matches(name_or_abbr)
)
)
end
class << self
alias_method :find_all_by_name_or_abbr, :with_name_or_abbr
deprecate find_all_by_name_or_abbr: :with_name_or_abbr, deprecator: Spree::Deprecation
end

# table of { country.id => [ state.id , state.name ] }, arrays sorted by name
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
let(:address) { build(:address, country: country) }

before do
allow(country.states).to receive_messages find_all_by_name_or_abbr: [state]
allow(country.states).to receive_messages with_name_or_abbr: [state]
end

context 'address does not require state' do
Expand Down
35 changes: 31 additions & 4 deletions core/spec/models/spree/state_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
require 'spec_helper'

describe Spree::State, type: :model do
it "can find a state by name or abbr" do
state = create(:state, name: "California", abbr: "CA")
expect(Spree::State.find_all_by_name_or_abbr("California")).to include(state)
expect(Spree::State.find_all_by_name_or_abbr("CA")).to include(state)
describe '.with_name_or_abbr' do
subject do
Spree::State.with_name_or_abbr(search_term)
end

let!(:state) { create(:state, name: "California", abbr: "CA") }

context 'by invalid term' do
let(:search_term) { 'NonExistent' }
it { is_expected.to be_empty }
end

context 'by name' do
let(:search_term) { 'California' }
it { is_expected.to include(state) }
end

context 'by abbr' do
let(:search_term) { 'CA' }
it { is_expected.to include(state) }
end

context 'by case-insensitive abbr' do
let(:search_term) { 'CaLiFoRnIa' }
it { is_expected.to include(state) }
end

context 'by case-insensitive abbr' do
let(:search_term) { 'cA' }
it { is_expected.to include(state) }
end
end

it "can find all states group by country id" do
Expand Down

0 comments on commit 3270fdc

Please sign in to comment.