cirandas.net

ref: master

db/migrate/20110203160153_rename_images_path_on_tracked_actions.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class RenameImagesPathOnTrackedActions < ActiveRecord::Migration

  def self.up
    select_all("SELECT id, verb, params FROM action_tracker WHERE verb IN ('new_friendship', 'join_community', 'leave_community')").each do |tracker|
      if tracker['verb'] == 'new_friendship'
        param_name = 'friend_profile_custom_icon'
      else
        param_name = 'resource_profile_custom_icon'
      end

      params = YAML.load(tracker['params'])
      paths = []
      params[param_name].each do |image_path|
        paths << self.rename_path(image_path) unless image_path.nil?
      end
      params[param_name] = paths

      execute(ApplicationRecord.sanitize_sql(["UPDATE action_tracker SET params = ? WHERE id  = ?", params.to_yaml, tracker['id']]))
    end
  end

  def self.down
    say('Nothing to undo')
  end

  class << self
    def rename_path(old_path)
      if old_path =~ /^\/images\/0/
        old_path.gsub(/^\/images\//, "/image_uploads/")
      else
        old_path
      end
    end
  end

end