-
Notifications
You must be signed in to change notification settings - Fork 0
Broadcasters
Eric Draut edited this page Jan 4, 2016
·
2 revisions
In your model
class User < ActiveRecord::Base
include ForeignOffice::Broadcaster
attr_accessor :load_friend
has_many :friendships
def serialize
self.attributes.merge friend_count: self.friendships.count,
load_friend: self.load_friend
end
end
In a service object
class AddFriend
def init(initiator,new_friend)
@initiator, @new_friend = initiator, new_friend
end
def call
@friendship = Friendship.create(initiator: @initiator, friend: @new_friend)
@recipient.load_friendship = @friendship
@recipient.broadcast_change
@new_friend.load_friendship = @friendship
@new_friend.broadcast_change
@friendship
end
end
In your view
<span <%= listener_attrs(@user, :friend_count)> > <%= @user.friend_count %> </span>
<section id="friendship_list">
<%= render partial: '/friendships/show_wrapper', collection: @user.friendships %>
</section>
<%= ajax_link '', '#', {data: listener_hash(@user, :load_friend, trigger: true)}, '#friendship_list' %>