Sha256: 152dddcbf17f50abae6c725e04729fb3addbcd34d6a23d27e183f0c5b9615469

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

class RefactorFollowingsRelation < ActiveRecord::Migration
  def change
    create_table :inkwell_followings do |t|
      t.integer :follower_id
      t.integer :followed_id

      t.timestamps
    end
    add_column ::Inkwell::Engine::config.user_table, :follower_count, :integer, :default => 0
    add_column ::Inkwell::Engine::config.user_table, :following_count, :integer, :default => 0

    user_class = Object.const_get ::Inkwell::Engine::config.user_table.to_s.singularize.capitalize
    user_class.all.each do |user|
      followings_ids = ActiveSupport::JSON.decode user.followings_ids
      followers_ids = ActiveSupport::JSON.decode user.followers_ids
      user.following_count = followings_ids.size
      user.follower_count = followers_ids.size
      user.save
      followings_ids.each do |followed_id|
        ::Inkwell::Following.create :follower_id => user.id, :followed_id => followed_id
      end
    end

    remove_column ::Inkwell::Engine::config.user_table, :followers_ids
    remove_column ::Inkwell::Engine::config.user_table, :followings_ids
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
inkwell-2.0.0 db/migrate/20130212130888_refactor_followings_relation.rb
inkwell-1.5.2 db/migrate/20130212130888_refactor_followings_relation.rb
inkwell-1.5.1 db/migrate/20130212130888_refactor_followings_relation.rb
inkwell-1.4.1 db/migrate/20130212130888_refactor_followings_relation.rb