diff --git a/lib/detectify/query_builder/base.rb b/lib/detectify/query_builder/base.rb index 466042f..6563ff4 100644 --- a/lib/detectify/query_builder/base.rb +++ b/lib/detectify/query_builder/base.rb @@ -4,8 +4,8 @@ class Base attr_reader :domain, :subdomain def initialize(domain, subdomain) - @domain = domain - @subdomain = subdomain + @domain = domain.downcase if domain.is_a?(String) + @subdomain = subdomain.downcase if subdomain.is_a?(String) end def build diff --git a/lib/detectify/query_builder/sql.rb b/lib/detectify/query_builder/sql.rb index 92513f4..05441d5 100644 --- a/lib/detectify/query_builder/sql.rb +++ b/lib/detectify/query_builder/sql.rb @@ -13,11 +13,11 @@ def build private def domain_clause - "#{Detectify.config.domain_column} = ?" if need_domain_clause? + "LOWER(#{Detectify.config.domain_column}) = ?" if need_domain_clause? end def subdomain_clause - "#{Detectify.config.subdomain_column} = ?" if need_subdomain_clause? + "LOWER(#{Detectify.config.subdomain_column}) = ?" if need_subdomain_clause? end def or_operator diff --git a/spec/detectify/query_builder/base_spec.rb b/spec/detectify/query_builder/base_spec.rb new file mode 100644 index 0000000..de2755f --- /dev/null +++ b/spec/detectify/query_builder/base_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +RSpec.describe Detectify::QueryBuilder::Base do + let(:domain) { 'DOMAIN' } + let(:subdomain) { 'SUBDOMAIN' } + subject { Detectify::QueryBuilder::Base.new(domain, subdomain) } + + describe '#initialize' do + it 'sets domain in downcase' do + expect(subject.domain).to eq(domain.downcase) + end + + it 'sets subdomain in downcase' do + expect(subject.subdomain).to eq(subdomain.downcase) + end + end +end diff --git a/spec/detectify/query_builder/sql_spec.rb b/spec/detectify/query_builder/sql_spec.rb index c469698..6795e17 100644 --- a/spec/detectify/query_builder/sql_spec.rb +++ b/spec/detectify/query_builder/sql_spec.rb @@ -9,14 +9,14 @@ let(:domain) { 'example.com' } let(:subdomain) { nil } - it { is_expected.to eq(['domain = ?', 'example.com']) } + it { is_expected.to eq(['LOWER(domain) = ?', 'example.com']) } end context 'with only subdomain' do let(:domain) { nil } let(:subdomain) { 'example' } - it { is_expected.to eq(['subdomain = ?', 'example']) } + it { is_expected.to eq(['LOWER(subdomain) = ?', 'example']) } end context 'with domain and subdomain' do @@ -25,7 +25,7 @@ it do is_expected.to eq([ - 'domain = ? OR subdomain = ?', 'example.com', 'example' + 'LOWER(domain) = ? OR LOWER(subdomain) = ?', 'example.com', 'example' ]) end end @@ -38,7 +38,7 @@ let(:domain) { 'example.com' } let(:subdomain) { nil } - it { is_expected.to eq(['domain = ?', 'example.com']) } + it { is_expected.to eq(['LOWER(domain) = ?', 'example.com']) } end context 'with only subdomain' do @@ -52,7 +52,7 @@ let(:domain) { 'example.com' } let(:subdomain) { 'example' } - it { is_expected.to eq(['domain = ?', 'example.com']) } + it { is_expected.to eq(['LOWER(domain) = ?', 'example.com']) } end after { Detectify.reset_config } @@ -72,14 +72,14 @@ let(:domain) { nil } let(:subdomain) { 'example' } - it { is_expected.to eq(['subdomain = ?', 'example']) } + it { is_expected.to eq(['LOWER(subdomain) = ?', 'example']) } end context 'with domain and subdomain' do let(:domain) { 'example.com' } let(:subdomain) { 'example' } - it { is_expected.to eq(['subdomain = ?', 'example']) } + it { is_expected.to eq(['LOWER(subdomain) = ?', 'example']) } end after { Detectify.reset_config }