Add optional "ignore_case" parameter to posts:remap rake task

This commit is contained in:
Gerhard Schlager 2018-08-23 14:49:00 +02:00
parent 7a91df3248
commit 409ee66839
2 changed files with 24 additions and 14 deletions

View File

@ -106,7 +106,7 @@ class Post < ActiveRecord::Base
when 'string' when 'string'
where('raw ILIKE ?', "%#{pattern}%") where('raw ILIKE ?', "%#{pattern}%")
when 'regex' when 'regex'
where('raw ~ ?', "(?n)#{pattern}") where('raw ~* ?', "(?n)#{pattern}")
end end
} }

View File

@ -133,15 +133,17 @@ task 'posts:normalize_code' => :environment do
puts "#{i} posts normalized!" puts "#{i} posts normalized!"
end end
def remap_posts(find, type, replace = "") def remap_posts(find, type, ignore_case, replace = "")
ignore_case = ignore_case == 'true'
i = 0 i = 0
Post.raw_match(find, type).find_each do |p| Post.raw_match(find, type).find_each do |p|
new_raw = regex = case type
case type when 'string' then Regexp.new(Regexp.escape(find), ignore_case)
when 'string' then p.raw.gsub(/#{Regexp.escape(find)}/, replace) when 'regex' then Regexp.new(find, ignore_case)
when 'regex' then p.raw.gsub(/#{find}/, replace) end
end
new_raw = p.raw.gsub(regex, replace)
if new_raw != p.raw if new_raw != p.raw
begin begin
@ -158,13 +160,14 @@ def remap_posts(find, type, replace = "")
end end
desc 'Remap all posts matching specific string' desc 'Remap all posts matching specific string'
task 'posts:remap', [:find, :replace, :type] => [:environment] do |_, args| task 'posts:remap', [:find, :replace, :type, :ignore_case] => [:environment] do |_, args|
require 'highline/import' require 'highline/import'
args.with_defaults(type: 'string') args.with_defaults(type: 'string', ignore_case: 'false')
find = args[:find] find = args[:find]
replace = args[:replace] replace = args[:replace]
type = args[:type]&.downcase type = args[:type]&.downcase
ignore_case = args[:ignore_case]&.downcase
if !find if !find
puts "ERROR: Expecting rake posts:remap['find','replace']" puts "ERROR: Expecting rake posts:remap['find','replace']"
@ -173,7 +176,10 @@ task 'posts:remap', [:find, :replace, :type] => [:environment] do |_, args|
puts "ERROR: Expecting rake posts:remap['find','replace']. Want to delete a word/string instead? Try rake posts:delete_word['word-to-delete']" puts "ERROR: Expecting rake posts:remap['find','replace']. Want to delete a word/string instead? Try rake posts:delete_word['word-to-delete']"
exit 1 exit 1
elsif type != 'string' && type != 'regex' elsif type != 'string' && type != 'regex'
puts "ERROR: Expecting rake posts:delete_word[pattern, type] where type is string or regex" puts "ERROR: Expecting rake posts:remap['find','replace',type] where type is string or regex"
exit 1
elsif ignore_case != 'true' && ignore_case != 'false'
puts "ERROR: Expecting rake posts:remap['find','replace',type,ignore_case] where ignore_case is true or false"
exit 1 exit 1
else else
confirm_replace = ask("Are you sure you want to replace all #{type} occurrences of '#{find}' with '#{replace}'? (Y/n)") confirm_replace = ask("Are you sure you want to replace all #{type} occurrences of '#{find}' with '#{replace}'? (Y/n)")
@ -181,17 +187,18 @@ task 'posts:remap', [:find, :replace, :type] => [:environment] do |_, args|
end end
puts "Remapping" puts "Remapping"
total = remap_posts(find, type, replace) total = remap_posts(find, type, ignore_case, replace)
puts "", "#{total} posts remapped!", "" puts "", "#{total} posts remapped!", ""
end end
desc 'Delete occurrence of a word/string' desc 'Delete occurrence of a word/string'
task 'posts:delete_word', [:find, :type] => [:environment] do |_, args| task 'posts:delete_word', [:find, :type, :ignore_case] => [:environment] do |_, args|
require 'highline/import' require 'highline/import'
args.with_defaults(type: 'string') args.with_defaults(type: 'string', ignore_case: 'false')
find = args[:find] find = args[:find]
type = args[:type]&.downcase type = args[:type]&.downcase
ignore_case = args[:ignore_case]&.downcase
if !find if !find
puts "ERROR: Expecting rake posts:delete_word['word-to-delete']" puts "ERROR: Expecting rake posts:delete_word['word-to-delete']"
@ -199,13 +206,16 @@ task 'posts:delete_word', [:find, :type] => [:environment] do |_, args|
elsif type != 'string' && type != 'regex' elsif type != 'string' && type != 'regex'
puts "ERROR: Expecting rake posts:delete_word[pattern, type] where type is string or regex" puts "ERROR: Expecting rake posts:delete_word[pattern, type] where type is string or regex"
exit 1 exit 1
elsif ignore_case != 'true' && ignore_case != 'false'
puts "ERROR: Expecting rake posts:delete_word[pattern, type,ignore_case] where ignore_case is true or false"
exit 1
else else
confirm_delete = ask("Are you sure you want to remove all #{type} occurrences of '#{find}'? (Y/n)") confirm_delete = ask("Are you sure you want to remove all #{type} occurrences of '#{find}'? (Y/n)")
exit 1 unless (confirm_delete == "" || confirm_delete.downcase == 'y') exit 1 unless (confirm_delete == "" || confirm_delete.downcase == 'y')
end end
puts "Processing" puts "Processing"
total = remap_posts(find, type) total = remap_posts(find, type, ignore_case)
puts "", "#{total} posts updated!", "" puts "", "#{total} posts updated!", ""
end end