I'm trying to render my notification feed activities but I can't seem to figure out how to properly loop through @activities
.
For background, I followed the recommended Sitepoint tutorial: https://www.sitepoint.com/super-easy-activity-feeds-with-stream/
My code is basically the same as that. Here's the @activities
object:
[2] pry(#<FeedsController>)> @activities
=> [{"activities"=>
[{"actor"=>
#<User id: 1, email: "andy@dev.to", created_at: "2017-06-08-21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
"foreign_id"=>"Follow:4",
"id"=>"861b2600-5063-11e7-8080-8000262131d5",
"object"=>
#<User id: 1, email: "andy@dev.to", created_at: "2017-06-08 21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
"origin"=>nil,
"target"=>nil,
"time"=>"2017-06-13T18:10:04.000000",
"to"=>["notification:1"],
"verb"=>"Follow"}],
"activity_count"=>1,
"actor_count"=>1,
"created_at"=>"2017-06-13T18:10:04.000000",
"group"=>"48946_2017-06-13",
"id"=>"861b2600-5063-11e7-8080-8000262131d5",
"is_read"=>false,
"is_seen"=>false,
"updated_at"=>"2017-06-13T18:10:04.000000",
"verb"=>"Follow"},
{"activities"=>
[{"actor"=>
#<User id: 1, email: "andy@dev.to", created_at: "2017-06-08 21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
"foreign_id"=>"Follow:1",
"id"=>"8b249a80-4fbb-11e7-8080-80002a2ebc3f",
"object"=>
#<User id: 1, email: "andy@dev.to", created_at: "2017-06-08 21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
"origin"=>nil,
"target"=>nil,
"time"=>"2017-06-12T22:07:37.000000",
"to"=>["notification:1"],
"verb"=>"Follow"},
{"actor"=>
#<User id: 2, email: "niko@dev.to", created_at: "2017-06-08 21:35:33", updated_at: "2017-06-08 22:01:50", name: "Niko">,
"foreign_id"=>"Follow:2",
"id"=>"40edea00-4fb8-11e7-8080-8000106d4b0d",
"object"=>
#<User id: 1, email: "andy@dev.to", created_at: "2017-06-08 21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
"origin"=>nil,
"target"=>nil,
"time"=>"2017-06-12T21:44:04.000000",
"to"=>["notification:1"],
"verb"=>"Follow"}],
"activity_count"=>2,
"actor_count"=>2,
"created_at"=>"2017-06-12T21:44:04.000000",
"group"=>"48946_2017-06-12",
"id"=>"8b249a80-4fbb-11e7-8080-80002a2ebc3f",
"is_read"=>false,
"is_seen"=>false,
"updated_at"=>"2017-06-12T22:07:37.000000",
"verb"=>"Follow"}]
Here's the main view (view/feeds/notification.html.erb):
<div class="page-header"><h1>Your notification feed</h1></div>
<% for activity in @activities %>
<%= render_activity activity %>
<% end %>
And my partial (views/aggregated_activity/_follow.html.erb):
<div class="well well-sm">
<p><small class="text-muted"><%= time_ago_in_words activity['time'] -%> ago</small></p>
<p><strong><%= activity['object'].name %></strong> and <strong><%= activity['actor'].name %></strong> are now friends</p>
</div>
I can access the right object if I changed the above partial to the following:
<div class="well well-sm">
<p><small class="text-muted"><%= time_ago_in_words activity['activities'][0]['time'] %> ago</small></p>
<p><strong><%= activity['activities'][0]['object'].name %></strong> and <strong><%= activity['activities'][0]['actor'].name %></strong> are now friends</p>
</div>
However that doesn't seem like the right way to go about it. It also seems like the only activities shown are the instances where I'm following myself, and not when I follow the second user (John). What's the best way to loop into this array of hashes, whose values are arrays?
I'll have to double check how current that sitepoint.com tutorial is, as we (Stream) didn't write it.
Back to your question, though: our notification feed, as you can see, ends up being grouped differently and you'd need a double loop for notification feeds:
<% for group in activities %>
<% for activity in group %>
<%= render_activity activity %>
As a side note, our stream_rails library, has some enrichment tools you can use to turn references in your activities back into full objects. By default it handles actor and object, which is what gives you access to things like activity['actor'].name
that you see when dumping the activity data above, but you can also reference other models for metadata items if you were to include those later.