This is the most liked video Alchemist Camp has ever published on YouTube. I hope you enjoy!
(Source code available for premium members)
Today we're taking on a somewhat larger task. We're building out some CMS-like features for a Phoenix app. The goal is to make it possible to publish multiple types of content—starting with blog articles, screencast episodes and resource links. Furthermore, we want to handle these types of content in such a way that they can be tagged with topics and liked by users in a unified way. Finally, we want to set up some properties common to all types of content, like whether or not it has been published and whether or not they are restricted to logged-in users.
Basically, the content-related portion of our schema will look like this:
Generating our schemas and contexts (3:46)
The first thing to generate is a Context for content and a schema for our entities (which will act as a "parent" table for articles, episodes and resources). After that, we'll use the full html generators for articles, episodes and resources and edit their generated schemas to add a
belongs_to :entity, Entity and edit their migrations to add unique indexes for their slugs. We'll also generate a schema for topics, which will consist of just the text label of the topic.
Don't forget to add the routes the generators say to for each of the html generators. Running
mix will fail otherwise. After this it should be possible to create an episode via the generated crud templates. It work and then we drop in a pre-styled front-end template and CSS (since that's not the focus of this episode).
Tying together the associated schemas (18:00)
The episode we just created will break in our new show template. That's because the show template copied in rightfully assumes that every episode belongs to an entity, but our generated CRUD interface couldn't enforce or handle that. So let's poke around and how to do this from
iex. After working out exactly how to create entities and then associate them with episodes as we create them, we can put the exact same logic into our content context that our controllers are already using.
Implementing tagging and likes (23:14)
Tagging content with topics is considerably more complex, and the work falls into the following phases:
- Create a join table between Topics and Entities
- Create a many-to-many relationship between topics and entities
- Enforce the uniqueness of each topic/entity pair in the join table (i.e. "no duplicate tags")
- Create helpers in our Content context to make it easy to add or remove tags to an entity
- Extend the helpers so that tags can be added to any struct that contains an entity
- Implement similar helpers for removing tags
- Update our
get_<content>! helpers to preload tags
- Update our templates to display topics
Likes work the same way as above except that they relate entities with users rather than with topics.
Work to be done
In the follow-up episode we'll improve our forms to allow mass editing of tags on a given piece of content, and unified views of all content with a particular tag will come after that.