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

Ajax

开发平台:

Others

  1. # BareMigration doesn't handle inheritance yet.  Therefore, we need
  2. # to mimic it manually using conditions on find().  Since this script
  3. # doesn't need to set type or id, there's no need to move either of
  4. # the protected fields out of the way like we do in #20-23.
  5. # BareMigration ALSO doesn't handle associations... yet.
  6. # For now, that means your keywords won't be automatically converted
  7. # to tags.  That might not be a bad thing...
  8. class Bare24Article < ActiveRecord::Base
  9.   include BareMigration
  10.   include TypoGuid
  11.   set_table_name :contents
  12.   has_and_belongs_to_many :tags,
  13.       :class_name => 'Bare24Tag', :foreign_key => 'article_id',
  14.       :join_table => 'articles_tags', :association_foreign_key => 'tag_id'
  15.   def keywords_to_tags
  16.     Bare24Article.transaction do
  17.       tags.clear
  18.       keywords.to_s.split.uniq.each do |tagword|
  19.         tags << Bare24Tag.get(tagword)
  20.       end
  21.     end
  22.   end
  23. end
  24. class Bare24Tag < ActiveRecord::Base
  25.   include BareMigration
  26.   has_and_belongs_to_many :articles,
  27.       :class_name => 'Bare24Article', :foreign_key => 'tag_id',
  28.       :join_table => 'articles_tags', :association_foreign_key => 'articles_id'
  29. end
  30. class CleanupContents < ActiveRecord::Migration
  31.   def self.up
  32.     STDERR.puts "Updating all articles"
  33.     # This is needed when migrating from 2.5.x, because we skip GUID
  34.     # generation and tagging during earlier migrations.
  35.     Bare24Article.transaction do
  36.       Bare24Article.find(:all, :conditions => "type = 'Article'").each do |a|
  37.         a.create_guid
  38. #       a.keywords_to_tags      (not needed?)
  39.         a.save!
  40.       end
  41.     end
  42.   end
  43.   def self.down
  44.     # nothing
  45.   end
  46. end