I'm new to RoR and am trying to use the public_activity gem with an existing application that already has an Activity model and activities table.
Is it possible to somehow configure or trick public_activity to use a different table & model without modifying the entire source code of public_activity?
I was thinking that the PublicActivity::Activity would just point to a different model class?
The answer is simple.
Let's assume you would like the gem to use the events table instead of it's default of using activities.
After following the public_activity install instructions and generating the migration, open up the migration file it generated and change the table name to :events
Open File: db/migrate/xxxxxxxxx_create_activities.rb
and modify it like so...
class CreateEvents < ActiveRecord::Migration
# Create table
def self.up
create_table :events 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 :events, [:trackable_id, :trackable_type]
add_index :events, [:owner_id, :owner_type]
add_index :events, [:recipient_id, :recipient_type]
end
# Drop table
def self.down
drop_table :events
end
end
Then run rake db:migrate.
Then, create a file config/initializers/public_activity.rb
and add the line:
PublicActivity::ORM::ActiveRecord::Activity.table_name = 'events'
And proceed with utilizing the public_activity gem as described in the documentation and all is well in the hood.
A special thanks to the developers of public_activity for assistance with this solution in GitHub issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With