Class: Effective::ContentReplacer
- Inherits:
-
Object
- Object
- Effective::ContentReplacer
- Defined in:
- app/models/effective/content_replacer.rb
Instance Attribute Summary collapse
-
#places ⇒ Object
Returns the value of attribute places.
Instance Method Summary collapse
- #count(old_value) ⇒ Object (also: #find)
- #default_places ⇒ Object
-
#initialize(places = nil) ⇒ ContentReplacer
constructor
A new instance of ContentReplacer.
- #replace!(old_value, new_value) ⇒ Object
Constructor Details
#initialize(places = nil) ⇒ ContentReplacer
Returns a new instance of ContentReplacer.
8 9 10 |
# File 'app/models/effective/content_replacer.rb', line 8 def initialize(places = nil) @places = (places || default_places) end |
Instance Attribute Details
#places ⇒ Object
Returns the value of attribute places.
6 7 8 |
# File 'app/models/effective/content_replacer.rb', line 6 def places @places end |
Instance Method Details
#count(old_value) ⇒ Object Also known as: find
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/models/effective/content_replacer.rb', line 38 def count(old_value) raise("old_value cannot contain a \' character") if old_value.include?("'") total = 0 places.each do |table, columns| columns.each do |column| sql = "SELECT COUNT(*) FROM #{table} WHERE #{column} ILIKE '%#{old_value}%'" existing = ActiveRecord::Base.connection.execute(sql).first['count'].to_i total += existing puts "There are #{existing} occurrences of #{old_value} in #{table}.#{column}" end end total end |
#default_places ⇒ Object
12 13 14 |
# File 'app/models/effective/content_replacer.rb', line 12 def default_places { action_text_rich_texts: [:body] } end |
#replace!(old_value, new_value) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/effective/content_replacer.rb', line 16 def replace!(old_value, new_value) raise("old_value cannot contain a \' character") if old_value.include?("'") raise("new_value cannot contain a \' character") if new_value.include?("'") total = 0 places.each do |table, columns| columns.each do |column| sql = "SELECT COUNT(*) FROM #{table} WHERE #{column} ILIKE '%#{old_value}%'" existing = ActiveRecord::Base.connection.execute(sql).first['count'].to_i total += existing puts "Replacing #{existing} occurrences of #{old_value} with #{new_value} in #{table}.#{column}" sql = "UPDATE #{table} SET #{column} = REPLACE(#{column}, '#{old_value}', '#{new_value}') WHERE #{column} ILIKE '%#{old_value}%'" ActiveRecord::Base.connection.execute(sql) end end total end |