Skip to main content

Join GoRails to continue learning

Subscribe to GoRails to get access to this episode and all other pro episodes, and new awesome content every month.

Subscribe Now
Only $19/month

Login to your account

56 Activity Feed From Scratch

Episode 70 · July 3, 2015

Learn how to build an activity feed from scratch and how to manage lots of flexible partials

Views


Resources

<% @events.each do |event| %>
  <% if lookup_context.template_exists?(event.action, "events/activities", true) %>
    <%= render partial: "events/activities/#{event.action}", locals: {event: event} %>
  <% end %>
<% end %>

Transcripts

Subscribe or login to view the transcript for this episode.

Discussion


Gravatar
Crven Kapa (10 XP) on

Could you post the source code to this?

Gravatar
Chris Oliver (167,500 XP) on

Absolutely! Just posted it here: https://github.com/excid3/g...


Gravatar
Kohl Kohlbrenner on

event.eventable is an instance of a class right

Gravatar
Chris Oliver (167,500 XP) on

Yep! That's correct. It's actually another model instance but just given the name of "eventable" because it could be of various types of models.


Gravatar
Marc Gayle (320 XP) on

Chris,
How do we handle deletion dependencies here? E.g. say I setup an event to be trigged on a `comment` creation, or when I upload an image. But what happens when I delete that comment or I delete that image. Ideally, I would like to be able to delete the event that is related to that specific recently deleted comment or image.

What's the best way to do this?

Gravatar
Chris Oliver (167,500 XP) on

This is a fantastic question. If you setup the reverse dependency and set the dependent destroy option, that will delete the events when you delete the comment.

class Comment
has_many :events, as: :eventable, dependent: :destroy
end
Gravatar
Marc Gayle (320 XP) on

Ahh perfect. So that covers most use-cases. How do I handle virtual attributes, e.g. 'votes'. So I am using acts_as_votable, which creates a bunch of virtual attributes to indicate when a user has `liked` a `Node`. When that node is deleted, all of those virtual attributes are deleted, so I would like to delete all of those events too.

Do I have to do this manually in an `after_delete :callback` on my `Node` model or is there some other way?

Gravatar
Chris Oliver (167,500 XP) on

Depending on how that's setup, you might patch the Vote model so that if you created events matched to the vote, you could delete those. You can't really set this up on virtual attributes because you need it attached directly to the records so when things get deleted you can remove the related records.

Gravatar
Marc Gayle (320 XP) on

Yeh....so I guess the solution just may be in the removal of the Node callback. Or just adding the `dependent: :destroy` on my Node model. Or maybe I just ignore it, given that it is just a like.


Gravatar
Francisco Quinones (7,370 XP) on

Love it keep it up Chris


Gravatar
Noel Hwande (730 XP) on

Hey Chris. Thanks for all the awesome tutorials. If I wanted to add a link to "Unfollow" and delete the event, what would that look like?


Gravatar
Daniel LeGrand (1,040 XP) on
It looks like the `lookup_context.template_exists?()` method now requires the prefixes to be an array, or an enumerable that responds to `.map()`, instead of just a string. This was changed as previous versions of Rails (< 3.1) accepted a string prefix. I'm not sure why it works in the episode, since you're using Rails 4; maybe it was a soft deprecation?

https://apidock.com/rails/ActionView/LookupContext/ViewPaths/template_exists%3f

Login or create an account to join the conversation.