cirandas.net

ref: master

db/migrate/20120228202739_adapt_create_articles_activity.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
class AdaptCreateArticlesActivity < ActiveRecord::Migration

  # Removing 'create_article' activities that grouped articles.
  # Creating new activities only to recent articles (not grouping)
  def self.up
    select_all("SELECT id FROM action_tracker WHERE verb = 'create_article'").each do |tracker|
      activity = ActionTracker::Record.find_by(id: tracker['id'])
      if activity
        activity.destroy
      end
    end

      select_all("SELECT id FROM articles").each do |art|
      article = Article.find(art['id'])
      if article && article.created_at >= 8.days.ago && article.author && article.author.kind_of?(Person)
        article.create_activity
      end
    end
  end

  def self.down
    say "this migration can't be reverted"
  end
end