ruby-on-railsrubypublic-activity

Rails 4 public_activity with two types of owner


I am using public_activity gem in my app and to track the owner who created the record to track the event model, I am using devise and I have two types of users, individual and company , my code is this to track the owner

class Event < ActiveRecord::Base
  include PublicActivity::Model
  tracked owner: ->(controller, model) { controller && controller.current_individual }

end

Here the problem is I want to track the owner as current_company if the company is signed in , but public_activity gem doesnt provide multiple owner types

I want to track that if the current_individual is signed in then the owner type and id would be of individual and if company is signed in then according to that.

what i am doing is

class Event < ActiveRecord::Base
  include PublicActivity::Model
  tracked owner: ->(controller, model) { controller &&  controller.individual_signed_in? ? controller.current_individual : controller.current_company  }
end

but i am getting owner id and type when individual is logged in but not getting owner type and id when company is logged in


Solution

  • Actually owner is polymorphic as can be seen here:

    create_table :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
    

    So it's not a problem to assign owner of different type.

    If you want to assign company as owner when company's user is signed-in and a person, when personal account is signed in, you should just make appropriate changes in your code:

    tracked owner: ->(controller, model) { controller.current_user.organization? ? controller.current_user.organization : controller.current_user.person }