046_fixup_forthcoming_publications.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:1k
源码类别:

Ajax

开发平台:

Others

  1. require 'bare_migration'
  2. class FixupForthcomingPublications < ActiveRecord::Migration
  3.   class Trigger < ActiveRecord::Base
  4.     belongs_to :pending_item, :polymorphic => true
  5.   end
  6.   class Content < ActiveRecord::Base
  7.   end
  8.   class Article < Content
  9.   end
  10.   def self.up
  11.     return if $schema_generator
  12.     Article.transaction do
  13.       Trigger.transaction do
  14.         Article.find(:all, :conditions => ['published = ? AND published_at > ?',
  15.                                            true, Time.now]).each do |art|
  16.           Trigger.create!(:pending_item   => art,
  17.                           :due_at         => art.published_at,
  18.                           :trigger_method => 'publish!')
  19.           art.update_attribute(:published, false)
  20.         end
  21.       end
  22.     end
  23.   end
  24.   def self.down
  25.     return if $schema_generator
  26.     Article.transaction do
  27.       Trigger.transaction do
  28.         Trigger.find(:all,
  29.                      :conditions => "pending_item_type = 'Article' AND trigger_method = 'publish!'").each do |t|
  30.           t.pending_item.update_attribute(:published, :true)
  31.           t.destroy
  32.         end
  33.       end
  34.     end
  35.   end
  36. end