Sha256: 1f4c408dcd83ff2a7cb567132b8071900995dcc839ffe579ea921d1f0a3890b2

Contents?: true

Size: 888 Bytes

Versions: 13

Compression:

Stored size: 888 Bytes

Contents

# frozen_string_literal: true

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

13 entries across 13 versions & 1 rubygems

Version Path
fat_free_crm-0.22.1 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.22.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.21.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.20.1 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.20.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.19.2 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.19.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.18.2 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.17.3 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.18.1 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.18.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.17.2 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.17.1 db/migrate/20120314080441_add_subscribed_users_to_entities.rb