Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I customize the public_activity rails gem to use a different "table" or "model" for "activities"?

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?

like image 683
Joshua F. Rountree Avatar asked Feb 01 '26 05:02

Joshua F. Rountree


1 Answers

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.

like image 170
Joshua F. Rountree Avatar answered Feb 03 '26 19:02

Joshua F. Rountree



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!