Permalink
Browse files

Use String#strip_heredoc for cleaner heredocs

  • Loading branch information...
1 parent 4c04674 commit 21e4b5897b0a14a7d429b4ccc1629ca1ba43304b @nertzy nertzy committed Apr 2, 2013
View
1 lib/pg_search.rb
@@ -1,6 +1,7 @@
require "active_record"
require "active_support/concern"
require "active_support/core_ext/module/attribute_accessors"
+require "active_support/core_ext/string/strip"
module PgSearch
autoload :Configuration, "pg_search/configuration"
View
2 lib/pg_search/features/tsearch.rb
@@ -7,7 +7,7 @@ def initialize(*args)
super
if options[:prefix] && model.connection.send(:postgresql_version) < 80400
- raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.gsub /^\s*/, '')
+ raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.strip_heredoc)
Sorry, {:using => {:tsearch => {:prefix => true}}} only works in PostgreSQL 8.4 and above.")
MESSAGE
end
View
22 lib/pg_search/multisearch/rebuilder.rb
@@ -28,17 +28,17 @@ def connection
model.connection
end
- REBUILD_SQL_TEMPLATE = <<-SQL
-INSERT INTO :documents_table (searchable_type, searchable_id, content, created_at, updated_at)
- SELECT :model_name AS searchable_type,
- :model_table.id AS searchable_id,
- (
- :content_expressions
- ) AS content,
- :current_time AS created_at,
- :current_time AS updated_at
- FROM :model_table
-SQL
+ REBUILD_SQL_TEMPLATE = <<-SQL.strip_heredoc
+ INSERT INTO :documents_table (searchable_type, searchable_id, content, created_at, updated_at)
+ SELECT :model_name AS searchable_type,
+ :model_table.id AS searchable_id,
+ (
+ :content_expressions
+ ) AS content,
+ :current_time AS created_at,
+ :current_time AS updated_at
+ FROM :model_table
+ SQL
def rebuild_sql
replacements.inject(REBUILD_SQL_TEMPLATE) do |sql, key|
View
2 lib/pg_search/normalizer.rb
@@ -8,7 +8,7 @@ def add_normalization(sql_expression)
return sql_expression unless config.ignore.include?(:accents)
if config.postgresql_version < 90000
- raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.gsub /^\s*/, '')
+ raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.strip_heredoc)
Sorry, {:ignoring => :accents} only works in PostgreSQL 9.0 and above.
#{config.inspect}
MESSAGE
View
6 lib/pg_search/tasks.rb
@@ -5,9 +5,9 @@
namespace :multisearch do
desc "Rebuild PgSearch multisearch records for a given model"
task :rebuild, [:model,:schema] => :environment do |task, args|
- raise ArgumentError, <<-MESSAGE unless args.model
-You must pass a model as an argument.
-Example: rake pg_search:multisearch:rebuild[BlogPost]
+ raise ArgumentError, <<-MESSAGE.strip_heredoc unless args.model
+ You must pass a model as an argument.
+ Example: rake pg_search:multisearch:rebuild[BlogPost]
MESSAGE
model_class = args.model.classify.constantize
connection = PgSearch::Document.connection
View
2 spec/integration/pg_search_spec.rb
@@ -623,7 +623,7 @@
let!(:unexpected) { ModelWithTsvector.create!(:content => 'longcat is looooooooong') }
before do
- ActiveRecord::Base.connection.execute <<-SQL
+ ActiveRecord::Base.connection.execute <<-SQL.strip_heredoc
UPDATE #{ModelWithTsvector.table_name}
SET content_tsvector = to_tsvector('english'::regconfig, "#{ModelWithTsvector.table_name}"."content")
SQL
View
20 spec/lib/pg_search/multisearch/rebuilder_spec.rb
@@ -84,16 +84,16 @@ def rebuild_pg_search_documents
time = DateTime.parse("2001-01-01")
rebuilder = PgSearch::Multisearch::Rebuilder.new(Model, lambda { time } )
- expected_sql = <<-SQL
-INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
- SELECT 'Model' AS searchable_type,
- #{Model.quoted_table_name}.id AS searchable_id,
- (
- coalesce(#{Model.quoted_table_name}.name::text, '')
- ) AS content,
- '2001-01-01 00:00:00' AS created_at,
- '2001-01-01 00:00:00' AS updated_at
- FROM #{Model.quoted_table_name}
+ expected_sql = <<-SQL.strip_heredoc
+ INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
+ SELECT 'Model' AS searchable_type,
+ #{Model.quoted_table_name}.id AS searchable_id,
+ (
+ coalesce(#{Model.quoted_table_name}.name::text, '')
+ ) AS content,
+ '2001-01-01 00:00:00' AS created_at,
+ '2001-01-01 00:00:00' AS updated_at
+ FROM #{Model.quoted_table_name}
SQL
executed_sql = []
View
48 spec/lib/pg_search/multisearch_spec.rb
@@ -31,7 +31,7 @@
describe "cleaning up search documents for this model" do
before do
- connection.execute <<-SQL
+ connection.execute <<-SQL.strip_heredoc
INSERT INTO pg_search_documents
(searchable_type, searchable_id, content, created_at, updated_at)
VALUES
@@ -74,7 +74,7 @@
context "when the model implements .rebuild_pg_search_documents" do
before do
def model.rebuild_pg_search_documents
- connection.execute <<-SQL
+ connection.execute <<-SQL.strip_heredoc
INSERT INTO pg_search_documents
(searchable_type, searchable_id, content, created_at, updated_at)
VALUES
@@ -119,17 +119,17 @@ def model.rebuild_pg_search_documents
end
it "should generate the proper SQL code" do
- expected_sql = <<-SQL
-INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content, created_at, updated_at)
- SELECT #{connection.quote(model.name)} AS searchable_type,
- #{model.quoted_table_name}.id AS searchable_id,
- (
- coalesce(#{model.quoted_table_name}.title::text, '')
- ) AS content,
- #{connection.quote(connection.quoted_date(now))} AS created_at,
- #{connection.quote(connection.quoted_date(now))} AS updated_at
- FROM #{model.quoted_table_name}
- SQL
+ expected_sql = <<-SQL.strip_heredoc
+ INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content, created_at, updated_at)
+ SELECT #{connection.quote(model.name)} AS searchable_type,
+ #{model.quoted_table_name}.id AS searchable_id,
+ (
+ coalesce(#{model.quoted_table_name}.title::text, '')
+ ) AS content,
+ #{connection.quote(connection.quoted_date(now))} AS created_at,
+ #{connection.quote(connection.quoted_date(now))} AS updated_at
+ FROM #{model.quoted_table_name}
+ SQL
statements = []
connection.stub(:execute) { |sql| statements << sql }
@@ -146,17 +146,17 @@ def model.rebuild_pg_search_documents
end
it "should generate the proper SQL code" do
- expected_sql = <<-SQL
-INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content, created_at, updated_at)
- SELECT #{connection.quote(model.name)} AS searchable_type,
- #{model.quoted_table_name}.id AS searchable_id,
- (
- coalesce(#{model.quoted_table_name}.title::text, '') || ' ' || coalesce(#{model.quoted_table_name}.content::text, '')
- ) AS content,
- #{connection.quote(connection.quoted_date(now))} AS created_at,
- #{connection.quote(connection.quoted_date(now))} AS updated_at
- FROM #{model.quoted_table_name}
-SQL
+ expected_sql = <<-SQL.strip_heredoc
+ INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content, created_at, updated_at)
+ SELECT #{connection.quote(model.name)} AS searchable_type,
+ #{model.quoted_table_name}.id AS searchable_id,
+ (
+ coalesce(#{model.quoted_table_name}.title::text, '') || ' ' || coalesce(#{model.quoted_table_name}.content::text, '')
+ ) AS content,
+ #{connection.quote(connection.quoted_date(now))} AS created_at,
+ #{connection.quote(connection.quoted_date(now))} AS updated_at
+ FROM #{model.quoted_table_name}
+ SQL
statements = []
connection.stub(:execute) { |sql| statements << sql }

0 comments on commit 21e4b58

Please sign in to comment.