##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
- Public activity gem : https://github.com/pokonski/public_activity
- [How to] Change table name for activities : https://github.com/pokonski/public_activity/wiki/%5BHow-to%5D-Change-table-name-for-activities
- Read the code to config : https://github.com/pokonski/public_activity/blob/master/lib/public_activity/config.rb
- Same issue : public-activity/public_activity#87
thx ^^
don't forget to migrate after modify migration