Sha256: 16781933d0c2aa7dac00fdad210087b63dd5119df0ebae03937635789db68ecd

Contents?: true

Size: 857 Bytes

Versions: 8

Compression:

Stored size: 857 Bytes

Contents

class AddSubscribedUsersToEntities < ActiveRecord::Migration[4.2]
  def change
    %w[accounts campaigns contacts leads opportunities tasks].each do |table|
      add_column table.to_sym, :subscribed_users, :text
      # Reset the column information of each model
      table.singularize.capitalize.constantize.reset_column_information
    end

    entity_subscribers = Hash.new(Set.new)

    # Add comment's user to the entity's Set
    Comment.all.each do |comment|
      entity_subscribers[[comment.commentable_type, comment.commentable_id]] += [comment.user_id]
    end

    # Run as one atomic action.
    ActiveRecord::Base.transaction do
      entity_subscribers.each do |entity, user_ids|
        connection.execute "UPDATE #{entity[0].tableize} SET subscribed_users = '#{user_ids.to_a.to_yaml}' WHERE id = #{entity[1]}"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fat_free_crm-0.15.2 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.16.4 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.15.1 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.16.3 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.16.2 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.16.1 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.16.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.15.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb