Sha256: d3aed8aff48220d56b7508871f22fa3a7c7025a0a258e6ab6d04e360035b99a7

Contents?: true

Size: 852 Bytes

Versions: 21

Compression:

Stored size: 852 Bytes

Contents

class AddSubscribedUsersToEntities < ActiveRecord::Migration
  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

21 entries across 21 versions & 2 rubygems

Version Path
fat_free_crm-0.14.2 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.14.1 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.15.0.beta.2 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.15.0.beta db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.14.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
reduced_fat_crm-0.15.0.beta db/migrate/20120314080441_add_subscribed_users_to_entities.rb
reduced_fat_crm-0.14.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.13.6 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.13.5 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.13.4 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.13.3 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.13.2 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.12.3 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.12.2 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.13.1 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.12.1 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.13.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.12.0 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.11.4 db/migrate/20120314080441_add_subscribed_users_to_entities.rb
fat_free_crm-0.11.3 db/migrate/20120314080441_add_subscribed_users_to_entities.rb