Reputation: 203
I have a posts model that I am tracking using the PublicActivity gem as seen below
class Post < ActiveRecord::Base
include PublicActivity::Model
tracked except: :destroy, owner: ->(controller, model) { controller && controller.current_user }
Here's the link to more info about the gem: https://github.com/pokonski/public_activity
In the Post table, one of the attributes that I have is "discussion".
create_table "posts", :force => true do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "discussion", :default => false
end
If discussion == true, I want to display a different kind of text message in the PublicActivity stream.
Right now, I'm using this in view for ALL posts
<li>
<% if activity.trackable_type == "Post" %>
<%= link_to activity.owner.name, activity.owner if activity.owner %> posted new content <span class="timestamp"> <%= time_ago_in_words(activity.created_at) %> ago.
</span>
<% else %>
<%= link_to activity.owner.name, activity.owner if activity.owner %> made a comment on a post<span class="timestamp"> <%= time_ago_in_words(activity.created_at) %> ago.
</span>
<% end %>
</li>
Here's the controller for the view above
class ActivitiesController < ApplicationController
def index
@activities = PublicActivity::Activity.order("created_at desc")
end
end
I want to include a new type of view message where it says "user created a new discussion" which is based on the boolean value of the attribute inside the post table. Is this possible?
Upvotes: 0
Views: 1238
Reputation: 2520
activity.trackable
will give the model that is being tracked. it is a polymorphic association.
Lets say the user has created a new post and discussion is set to true.
In that case activity.trackable
will give you the Post model object and all its methods will work as normal, so you activity.trackable.discussion
will give you true/false
, based on the example i said, it will return true
in your view you can do something like this
<% if activity.trackable.discussion %>
User created a discussion
<% else %>
User created a post
<% end %>
Hope that helps.
Upvotes: 2