From 698cea17003be86126902d7928cd2ee6e880306e Mon Sep 17 00:00:00 2001 From: Alessandro Barbieri Date: Sat, 21 Jan 2023 00:41:17 +0100 Subject: [PATCH] Issue #15 - Kept multiple whitespace in embedded params using gsub only on query template. Updated travis.yml to Ruby 2.6.0 to avoid bundler install error. --- .travis.yml | 2 +- lib/sql_query.rb | 10 ++++++++-- spec/sql_query_spec.rb | 10 ++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 42c590c..5ec63b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ gemfile: - gemfiles/5.0.gemfile rvm: - - '2.3.3' + - '2.6.0' addons: postgresql: '9.4' diff --git a/lib/sql_query.rb b/lib/sql_query.rb index b5b580a..09c7627 100644 --- a/lib/sql_query.rb +++ b/lib/sql_query.rb @@ -34,7 +34,7 @@ def exec_query(prepare = true) end def sql - @sql ||= ERB.new(File.read(file_path)).result(binding) + @sql ||= prepare_query(false) end def pretty_sql @@ -46,7 +46,7 @@ def quote(value) end def prepared_for_logs - sql.gsub(/(\n|\s)+/, ' ') + @sql_for_logs ||= prepare_query(true) end def partial(partial_name, partial_options = {}) @@ -75,6 +75,12 @@ def initialize private + def prepare_query(for_logs) + query_template = File.read(file_path) + query_template = query_template.gsub(/(\n|\s)+/, ' ') if for_logs + ERB.new(query_template).result(binding) + end + def split_to_path_and_name(file) if file.is_a?(Symbol) ['', file.to_s] diff --git a/spec/sql_query_spec.rb b/spec/sql_query_spec.rb index 6248c48..390153b 100644 --- a/spec/sql_query_spec.rb +++ b/spec/sql_query_spec.rb @@ -201,6 +201,16 @@ class Model < ActiveRecord::Base expect(query.prepared_for_logs) .to eq("SELECT * FROM players WHERE email = 'e@mail.dev' ") end + + context 'when embedded params have multiple whitespaces' do + let(:options) { { email: ' e@mail.dev ' } } + let(:query) { described_class.new(file_name, options) } + + it 'returns string without multiple whitespaces except embedded params' do + expect(query.prepared_for_logs) + .to eq("SELECT * FROM players WHERE email = ' e@mail.dev ' ") + end + end end describe '.config' do