Rails 7 - Sneak Peek [part1]
source link: https://dev.to/fedeagripa/rails-7-sneak-peek-part1-2j71
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
New method
# replaces the common pattern of finding sibling objects without one self:
relation.excluding(post, another_post)
Lets see how we can use it
A simple example for this shows can you simply filter other posts in your usually built onboarding project and it seems quite cool
class Post
def simliar_posts
user.posts.excluding(self)
end
end
Lets actually think if it useful in a common place
But wait a second... we have a really messy implementation of BROADCAST
in ActionCable where we need to filter our own sent messages everytime, and this new friend may look promising to use.
What if we implement a MULTICAST
, or even add a parameter to our usual broadcast_to
method in ActionCable making it look like this source:
def broadcast(message, exclude_self: true)
server.logger.debug { "[ActionCable] Broadcasting to #{broadcasting}: #{message.inspect.truncate(300)}" }
payload = { broadcasting: broadcasting, message: message, coder: coder }
ActiveSupport::Notifications.instrument("broadcast.action_cable", payload) do
encoded = coder ? coder.encode(message) : message
if exclude_self
server.pubsub.exclude(self).broadcast broadcasting, encoded
else
server.pubsub.broadcast broadcasting, encoded
end
end
end
THOUGHTS??
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK