Skip to content

Commit

Permalink
Issue #15 - Kept multiple whitespace in embedded params using gsub on…
Browse files Browse the repository at this point in the history
…ly on query template. Updated travis.yml to Ruby 2.6.0 to avoid bundler install error.
  • Loading branch information
Alessandro Barbieri committed Jan 20, 2023
1 parent be2020a commit 698cea1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ gemfile:
- gemfiles/5.0.gemfile

rvm:
- '2.3.3'
- '2.6.0'

addons:
postgresql: '9.4'
Expand Down
10 changes: 8 additions & 2 deletions lib/sql_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {})
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions spec/sql_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ class Model < ActiveRecord::Base
expect(query.prepared_for_logs)
.to eq("SELECT * FROM players WHERE email = '[email protected]' ")
end

context 'when embedded params have multiple whitespaces' do
let(:options) { { email: ' [email protected] ' } }
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 = ' [email protected] ' ")
end
end
end

describe '.config' do
Expand Down

0 comments on commit 698cea1

Please sign in to comment.