DEV Community

Cover image for Rails 7 - Sneak Peek [part1]
fedeagripa
fedeagripa

Posted on

Rails 7 - Sneak Peek [part1]

New method

# replaces the common pattern of finding sibling objects without one self:
relation.excluding(post, another_post) 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

THOUGHTS??

Discussion (0)