Skip to content

Instantly share code, notes, and snippets.

@yannvery
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save yannvery/1697791e173202a8047f to your computer and use it in GitHub Desktop.

Select an option

Save yannvery/1697791e173202a8047f to your computer and use it in GitHub Desktop.
How to configure public_activity ( https://github.com/pokonski/public_activity ) with another table name

Use Public Activity Gem with another table name

##1. Generate migration We assume that your application already has an "activities" table, you need to generate a migration for public activity with a specific name ( public_activity )

rails g public_activity:migration public_activities

You need to open the generated file and modify it like this (replace activities with public_activities) :

# Migration responsible for creating a table with activities
class CreatePublicActivities < ActiveRecord::Migration
  # Create table
  def self.up
    create_table :public_activities do |t|
      t.belongs_to :trackable, :polymorphic => true
      t.belongs_to :owner, :polymorphic => true
      t.string  :key
      t.text    :parameters
      t.belongs_to :recipient, :polymorphic => true

      t.timestamps
    end

    add_index :public_activities, [:trackable_id, :trackable_type]
    add_index :public_activities, [:owner_id, :owner_type]
    add_index :public_activities, [:recipient_id, :recipient_type]
  end
  # Drop table
  def self.down
    drop_table :public_activities
  end
end

##2. Add an initializer You need to specify table name wich be used by public activity

# config/initializers/public_activity.rb
PublicActivity::Config.set do
  table_name "public_activities"
end

##3. Overwrite create_activity if needed If a tracked model uses a relation has_one / belongs_to named "activity", you need to overwrite create_activity method like this :

def create_activity(args)
  kkey = args
  if args.class == "Array"
    kkey = args[:key] if args[:key]
    parameters = args[:parameters] if args[:parameters]
    owner = args[:owner] if args[:owner]
    recipient = args[:recipient] if args[:recipient]
  end
  PublicActivity::Activity.create :key => kkey, :trackable => self, :parameters => parameters, :owner => owner
end

##4. Resources

@francois-blanchard
Copy link

thx ^^

don't forget to migrate after modify migration

rake db:migrate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment