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