Many of my schemas can be considered to have a Feed
, so I followed the many-to-many recommendation for polymorphic associations.
I want to be able to list feed items, create posts of different types, delete posts of different types from a single interface. My initial thought was to make a Feed
protocol, so I’ve been going down this path:
defmodule Hydroplane.Feeds do
alias Hydroplane.Feeds.Feed
def list_feed_items(feedable) do
Feed.list_posts(feedable) ++ Feed.list_polls(feedable)
end
def create_post(feedable, author, attrs) do
Feed.create_post(feedable, author, attrs)
end
end
And the Feed protocol is here:
defprotocol Hydroplane.Feeds.Feed do
def list_posts(feedable)
def list_polls(feedable)
def create_post(feedable, author, attrs)
end
defimpl Hydroplane.Feeds.Feed, for: Hydroplane.Initiatives.Initiative do
import Ecto.Query
alias Ecto.Multi
alias Hydroplane.Repo
alias Hydroplane.Initiatives.Initiative
alias Hydroplane.Feeds.{Post, Poll}
def list_posts(initiative) do
query =
from(p in Post,
join: ip in "initiative_posts",
on: p.id == ip.post_id,
join: i in Initiative,
on: i.id == ip.initiative_id,
where: i.id == ^initiative.id
)
query
|> Repo.all()
|> Repo.preload(:author)
end
def list_polls(initiative) do
query =
from(p in Poll,
join: ip in "initiative_polls",
on: p.id == ip.poll_id,
join: i in Initiative,
on: i.id == ip.initiative_id,
where: i.id == ^initiative.id
)
query
|> Repo.all()
|> Repo.preload(:author)
end
def create_post(initiative, author, attrs) do
result =
Multi.new()
|> Multi.insert(:post, Post.changeset(%Post{author: author}, attrs))
|> Multi.insert(:association, fn %{post: post} -> end)
|> Repo.transaction()
# Stuff like this is where it can start to get really messy
end
end
The absolute JANK that I’m creating, basically having to fully implement Feed
for each feedable item makes me think this is the wrong approach. It seems crazy to implement everything differently when the only thing that changes is how I interact with the join table.
In Ruby the obvious way to solve this would be through metaprogramming, is that the proper route here? What’s a better way to do this? I’m sure there are several.