Latest / CyberCode Academy / Course 37 - Building Web Apps with Ruby On Rails | Episode 16:Templates and Partials for Modular Rails APIs
Transcript
- 0:00Speaker 1: Think about the last time you opened a massive rails
- 0:03Speaker 1: application and just tried to change a single seemingly harmless
- 0:07Speaker 1: field in a JSON API response.
- 0:09Speaker 2: Oh boy, Yeah, that's a classic scenario.
- 0:11Speaker 1: Right, did you like update a model and suddenly break
- 0:15Speaker 1: three entirely unrelated endpoints almost certainly? Or did you find yourself,
- 0:19Speaker 1: you know, digging through six levels of nested super calls
- 0:22Speaker 1: in a pure Ruby hash just trying to figure out
- 0:25Speaker 1: where a random commo went wrong. Yeah, pulling your hair
- 0:27Speaker 1: out exactly, because today we are finally putting an end
- 0:31Speaker 1: to that madness.
- 0:33Speaker 2: It really is a right of passage for back end developers.
- 0:36Speaker 2: I mean, you start out thinking API responses are simple, right,
- 0:39Speaker 2: the front end asks for data, you give them data.
- 0:41Speaker 1: Sure, it sounds easy on paper exactly.
- 0:44Speaker 2: But the reality of formatting that data, especially once you
- 0:46Speaker 2: start juggling deep relationships, renaming fields, and applying complex business rules,
- 0:52Speaker 2: it often turns your code base into this fragile, terrifying
- 0:55Speaker 2: house of cards.
- 0:56Speaker 1: It really does, and that is exactly what we are
- 0:59Speaker 1: unpacking in this deep dive. We're actually pulling directly from
- 1:02Speaker 1: your own collected technoes and development documentation on API building.
- 1:07Speaker 2: Right, which are super detailed by the way.
- 1:08Speaker 1: Yeah, they're great, and our mission today is to entirely
- 1:12Speaker 1: upgrade your mental model. We want to take you from
- 1:14Speaker 1: that agonizing, messy phase of raw Jason generation and move
- 1:19Speaker 1: you into a clean, highly modular architecture using the j builder.
- 1:23Speaker 2: Gem, which is such a game changer, it really is.
- 1:26Speaker 1: By the end of this you should be able to
- 1:28Speaker 1: handle massive, intricate data structures without losing a single hour
- 1:33Speaker 1: of sleep.
- 1:33Speaker 2: To get there, though, we need to logically trace the
- 1:36Speaker 2: architectural journey of an API request. We have to look
- 1:40Speaker 2: at the tools you are probably using right now, understand
- 1:43Speaker 2: precisely why they collapse under pressure, and introduce a step
- 1:46Speaker 2: by step strategy to violently separate your business logic from
- 1:50Speaker 2: your presentation logic.
- 1:51Speaker 1: All right, let's start at the scene of the crime. Then,
- 1:53Speaker 1: when a developer starts building a Rails API, what is
- 1:56Speaker 1: the default tool they usually reach for, and so why
- 2:00Speaker 1: does it eventually betray them?
- 2:01Speaker 2: Well, the usual suspect is a built in method called
- 2:04Speaker 2: as Jason. Right, if you've spent any time in Rails,
- 2:07Speaker 2: it's almost guaranteed you've used it and look to be
- 2:10Speaker 2: completely fair, as Jason is perfectly fine when you were
- 2:15Speaker 2: at the starting.
- 2:15Speaker 1: Line, like for prototypes and stuff.
- 2:17Speaker 2: Exactly, if you have a bare bones user model, just
- 2:21Speaker 2: a name and an email address, and you want to
- 2:23Speaker 2: spit that out to the client, it does a job,
- 2:26Speaker 2: but it is a massive trap a track, yeah, because
- 2:29Speaker 2: the second your application grows up, it becomes this totally
- 2:32Speaker 2: unmanageable bottleneck.
- 2:33Speaker 1: Okay, let's dig into the mechanics of that. Why does
- 2:36Speaker 1: it break down so badly? What's the fundamental flaw in
- 2:39Speaker 1: the Asdjason approach.
- 2:41Speaker 2: It comes down to a really severe violation of architectural boundaries. Basically,
- 2:46Speaker 2: Adjason forces your presentation logic into your model layer.
- 2:49Speaker 1: Oh wow, yeah, that's not good, right.
- 2:52Speaker 2: Think about what a model is actually supposed to do
- 2:54Speaker 2: in a robust architecture. The model is essentially the bouncer
- 2:57Speaker 2: of your database.
- 2:58Speaker 1: The bouncer I like that.
- 2:59Speaker 2: Yeah, it's data integrity, validations, business rules, state transitions. It
- 3:05Speaker 2: should have absolutely no idea and really no opinion about
- 3:09Speaker 2: how a specific front end client wants that data formatted
- 3:13Speaker 2: on a screen.
- 3:14Speaker 1: Okay, let's unpack this. If the model is strictly a
- 3:16Speaker 1: bouncer checking data integrity, it has no business knowing what
- 3:20Speaker 1: color the VIP risk bands.
- 3:21Speaker 2: Are precisely when you start telling the model how to
- 3:25Speaker 2: format its own date strings or how to rename a
- 3:28Speaker 2: specific field, just because the mobile app team asks for it,
- 3:32Speaker 2: you're polluting the code base.
- 3:33Speaker 1: You're totally mixing responsibilities. Relying on ash Jason for a
- 3:36Speaker 1: scaling app sounds like keeping all your cooking ingredients, your
- 3:39Speaker 1: dirty dishes, and your kitchen trash in the exact same drawer.
- 3:42Speaker 2: That is a visceral but highly accurate analogy.
- 3:45Speaker 1: Eventually, you can't cook a simple meal without digging through
- 3:48Speaker 1: garbage to find a spatula exactly.
- 3:51Speaker 2: And let's look at a real world scenario of that
- 3:53Speaker 2: drawer just completely overflowing. Yeah, the complexity problem goes critical
- 3:57Speaker 2: the exact moment you introduce associations, right, get nested. Yeah,
- 4:01Speaker 2: when you need multi level nesting, filtering or derived data
- 4:05Speaker 2: Asjuson basically forces you to manipulate huge, cumbersome hash representations
- 4:11Speaker 2: of your models in pure Ruby.
- 4:13Speaker 1: I've actually seen the aftermath of this. A team is
- 4:16Speaker 1: trying to track down a bug where an API is
- 4:19Speaker 1: leaking sensitive data and they find out it's because someone
- 4:23Speaker 1: forgot to filter and association deep inside some pure Ruby hash.
- 4:27Speaker 2: Yes, exactly. Let's say you have a project model and
- 4:30Speaker 2: you want to pull in its associated milestones, and as
- 4:33Speaker 2: you send, you end up using the accept or include options,
- 4:37Speaker 2: which forces you to write nested super method calls. You're
- 4:41Speaker 2: calling super passing it a hash which contains another hash
- 4:44Speaker 2: for the milestones, which dictates an array of fields you
- 4:46Speaker 2: want to accept. That sounds exhausting it is. Now imagine
- 4:50Speaker 2: those milestones have further associations, like the specific users assigned
- 4:53Speaker 2: to them. You nest again. Suddenly your model code looks
- 4:56Speaker 2: like a staircase of curly braces trailing off the right
- 4:59Speaker 2: side of your screen.
- 5:00Speaker 1: It's completely unreadable. But my instinct as a developer, rather
- 5:03Speaker 1: than learning a totally new tool, would just be to
- 5:06Speaker 1: write a massive helper module.
- 5:08Speaker 2: Oh, the classic helper module.
- 5:10Speaker 1: Trap, right, Like, why can't I just build a custom
- 5:13Speaker 1: Ruby object or a service class that strips out all
- 5:16Speaker 1: the nested data right before it hits the controller? Why
- 5:19Speaker 1: shouldn't I just do that?
- 5:20Speaker 2: Well, because you are still just wrangling pure Ruby hashes,
- 5:25Speaker 2: which means you are essentially reinventing a presentation layer from scratch,
- 5:29Speaker 2: but doing it poorly.
- 5:30Speaker 1: Ah, gotcha.
- 5:31Speaker 2: A custom helper module might get the logic out of
- 5:34Speaker 2: the model. Sure, but it doesn't solve the core problem.
- 5:37Speaker 2: Ruby hashes just aren't designed to elegantly map complex, highly
- 5:40Speaker 2: nested presentation requirements.
- 5:42Speaker 1: So you're just shifting the mess to a different file exactly.
- 5:46Speaker 2: You'll still end up writing hundreds of lines of boilerplate
- 5:49Speaker 2: code just to check if keys exist, rename them, and
- 5:52Speaker 2: format nested arrays. We really don't want to reinvent the
- 5:55Speaker 2: wheel here. If the model needs to be strictly focused
- 5:58Speaker 2: on business logic, we need a dedicated presentation layer.
- 6:01Speaker 1: And in the rails world, we actually already have a
- 6:03Speaker 1: place for that. When we render HTML, it's the views
- 6:06Speaker 1: directory exactly.
- 6:08Speaker 2: Just like how your HTML rendering logic lives in the
- 6:10Speaker 2: views directory, your JSON generation logic should live there too,
- 6:14Speaker 2: And this is the exact problem the Jabilder gem was
- 6:16Speaker 2: designed to solve. J Builder provides true modularity by pulling
- 6:21Speaker 2: all of that formatting chaots out of your models, out
- 6:23Speaker 2: of your custom helpers, and putting it into dedicated view templates.
- 6:27Speaker 1: So these templates just sit right alongside where you would
- 6:29Speaker 1: normally drop an HTML file, and they use the file
- 6:32Speaker 1: extension dot json dot JBuilder. You've got it, so if
- 6:36Speaker 1: my controller has an index action exposing an array of projects,
- 6:39Speaker 1: I literally just create an index dot Yson dot JBuilder.
- 6:42Speaker 2: Template spawn on and to do this smoothly, Jibilder uses
- 6:46Speaker 2: a DSL, a domain specific language. It is a specialized
- 6:51Speaker 2: syntax tailor made strictly for common Jason operations like nesting, renaming,
- 6:56Speaker 2: and filtering.
- 6:57Speaker 1: Wait wait, let me stop you right there. Wait, so
- 6:58Speaker 1: we are solving our complexity problem by introducing a whole
- 7:01Speaker 1: new domain specific language. Doesn't learning a new syntax just
- 7:05Speaker 1: add more mental overhead? I already know Ruby, I know
- 7:08Speaker 1: how to iterate over a raise and build hashes. Why
- 7:10Speaker 1: do I want to take the time to learn JBuilder syntax.
- 7:13Speaker 2: It's a completely fair question. But what's fascinating here is
- 7:16Speaker 2: that the exact opposite is true. Yeah, because this DSL
- 7:20Speaker 2: is hyper specialized for this one specific task, it dramatically
- 7:24Speaker 2: reduces your mental overhead. When you use pure Ruvi hashes
- 7:28Speaker 2: to build json, you are fighting the language. You're forcing
- 7:32Speaker 2: it to do something it wasn't strictly designed to do. Elegantly,
- 7:36Speaker 2: j builder actually uses Ruby's dynamic nature to its advantage.
- 7:39Speaker 1: Okay, so what is the actual mechanism making it easier.
- 7:42Speaker 2: Well, j builder gives you an object. Yeah, and whatever
- 7:45Speaker 2: you call a method on that object, even if it's
- 7:46Speaker 2: a completely made up word. The DSL catches it instantly,
- 7:50Speaker 2: translates it into a JSON key, and assigns whatever value
- 7:54Speaker 2: you put next to it. Oh wow, Yeah, you aren't
- 7:55Speaker 2: writing tedious hash syntax with arrows and symbols. You are
- 7:59Speaker 2: literally sketchy. The structural shape of your final Jason it
- 8:02Speaker 2: structurally mirrors the output.
- 8:04Speaker 1: Okay, I want to put this to the test. Let's
- 8:05Speaker 1: step away from the abstract and walk through a chaotic
- 8:09Speaker 1: real world requirement. Let's say we are building out a
- 8:13Speaker 1: project tracker application. The front end team just dropped a
- 8:16Speaker 1: massive Gyra ticket on your desk. They want a project's
- 8:20Speaker 1: list endpoint. But they have a ton of highly specific,
- 8:24Speaker 1: annoying demands for how the Jason.
- 8:26Speaker 2: Needs to look as they always do.
- 8:28Speaker 1: Right, So how does jabilder absorb all these specific demands
- 8:31Speaker 1: without us ever touching the model? Walk me through the
- 8:34Speaker 1: mental visualization. Where do we even start?
- 8:37Speaker 2: We start in the controller, and you were going to
- 8:39Speaker 2: love this because the controller basically does almost nothing. It
- 8:42Speaker 2: just becomes a simple.
- 8:43Speaker 1: Traffic cutch, just waving cars through exactly.
- 8:46Speaker 2: Instead of calling complex render methods or wrangling helper classes,
- 8:49Speaker 2: the controller simply grabs the projects away from the database
- 8:53Speaker 2: and passes it to the format dot json block. There
- 8:56Speaker 2: is zero rendering logic here. The controller just points the
- 8:59Speaker 2: data to the view and steps entirely.
- 9:00Speaker 1: Out of the way, so the controller keeps its hands
- 9:02Speaker 1: perfectly clean. What happens when the view takes over We
- 9:06Speaker 1: move into our index dot json dot JBuilder file.
- 9:09Speaker 2: Right right inside this template. Because we still have access
- 9:13Speaker 2: to standard Ruby, we aren't restricted by some rigid configuration file.
- 9:18Speaker 2: We can just run a normal loop over our projects
- 9:20Speaker 2: array nice, and for every single project we interact with
- 9:24Speaker 2: that magical g builder object, which is simply called json,
- 9:28Speaker 2: we just start attaching our requested fields directly to it.
- 9:30Speaker 1: All right, let's hit the first demand from the front
- 9:32Speaker 1: end team. In our database, the field is simply called title,
- 9:36Speaker 1: but the frontend developers have a conflict on their end,
- 9:39Speaker 1: so they specifically need this key to be named project
- 9:42Speaker 1: underscore title in the API response.
- 9:45Speaker 2: Okay, So in a pure Ruby ash or inside as json,
- 9:48Speaker 2: this means intercepting the data, deleting the old key, and
- 9:51Speaker 2: mapping the value to a new key. It's super tedious.
- 9:54Speaker 1: Yeah, lots of boilerplate.
- 9:56Speaker 2: But in JBuilder you simply dictate the shape. You're a
- 9:58Speaker 2: json dot project under score title and then give it
- 10:01Speaker 2: the value project dot title. That's it. That's it. The
- 10:04Speaker 2: DSL intercepts project underscore title, creates the Jason key and
- 10:08Speaker 2: assigns the data it reads entirely naturally.
- 10:11Speaker 1: Okay, Next demand date formatting in the database created underscore
- 10:17Speaker 1: at start underscore date and end to underscore date are
- 10:20Speaker 1: standard date time strings, but the front end client is
- 10:24Speaker 1: doing some complex local time zone math, so they demand
- 10:28Speaker 1: all dates arrive as Unix timestamps.
- 10:30Speaker 2: This is a perfect example of keeping boundaries strict. The
- 10:33Speaker 2: model shouldn't care about the front ken's preference for Unix
- 10:37Speaker 2: over isostrings, right, The bouncer doesn't care exactly. The model
- 10:41Speaker 2: just knows the exact moment the project was created. So
- 10:44Speaker 2: we keep this translation strictly in the view template. As
- 10:46Speaker 2: we attach the created underscore at field to our Jason object,
- 10:50Speaker 2: we simply chain standard rubion too it calling dot to
- 10:53Speaker 2: underscore I to convert the DateTime object to an integer.
- 10:56Speaker 1: Oh, which gives us our Unix timestamp bingo.
- 10:59Speaker 2: The hands of the translation seamlessly.
- 11:01Speaker 1: Let's escalate the difficulty. Now, the front end wants nesting,
- 11:04Speaker 1: but they want it heavily filtered. They want us to
- 11:06Speaker 1: pull in the associated milestones for each project, but you
- 11:09Speaker 1: know if we send the entire milestone object, the payload
- 11:12Speaker 1: will be massive. They only want the milestones title, a
- 11:15Speaker 1: boolean showing if it's done and it's created underscore at timestamp.
- 11:20Speaker 2: Remember that staircase of curly braces we talked about earlier. Vividly,
- 11:23Speaker 2: that is totally gone with j Builder. You just open
- 11:27Speaker 2: a block for the Milestones Association. Inside that nested block,
- 11:31Speaker 2: you explicitly list the exact fields you want. You state title, done,
- 11:36Speaker 2: and map created underscore ad to its timestamp.
- 11:39Speaker 1: Just listing them out.
- 11:40Speaker 2: Yeah, and because you are opening a block, the code
- 11:44Speaker 2: visually indense. The shape of your code directly mirrors the
- 11:47Speaker 2: nested array structure of the final Jason output. It is
- 11:51Speaker 2: incredibly easy to read. That's so clean, And we can
- 11:53Speaker 2: do the exact same thing for the viewers association just
- 11:56Speaker 2: opening a block and pulling out just their name and ID.
- 11:59Speaker 1: Here's where it gets really interesting. It genuinely feels like
- 12:02Speaker 1: jabuilder is acting as a highly efficient translator at the
- 12:05Speaker 1: United Nations.
- 12:06Speaker 2: I like that comparison.
- 12:07Speaker 1: Think about it. The database and the models they speak
- 12:10Speaker 1: raw data, pure unfiltered facts. The client application, on the
- 12:14Speaker 1: other hand, speaks a very specific, demanding dialect, very demanding,
- 12:18Speaker 1: and Jabuilder sits right in the middle in that soundproof
- 12:22Speaker 1: translation booth, renaming titles, converting dates, dropping unnecessary fields, and
- 12:27Speaker 1: doing it all flawlessly without ever bothering the actual diplomats
- 12:31Speaker 1: the models who are just trying to execute their business logic.
- 12:34Speaker 2: That is spot on. It's an isolated translation layer and
- 12:38Speaker 2: it handles translations that are even more abstract than that.
- 12:41Speaker 2: Let's look at drive data. Okay, what happens when the
- 12:44Speaker 2: front end demands a brand new, nested JSON object that
- 12:48Speaker 2: does not even exist in your database schema? Right?
- 12:51Speaker 1: For instance, your technoes outline a scenario where the front
- 12:54Speaker 1: end has a reusable UI component called an owner card.
- 12:57Speaker 1: It needs an object called owner containing a name and
- 13:00Speaker 1: an ID. But our database only has a single relational
- 13:03Speaker 1: field called created underscore.
- 13:04Speaker 2: By Yeah, so if you're relying on the model for this,
- 13:07Speaker 2: you would have to write some dummy method to fake
- 13:10Speaker 2: an owner object just for the API. It's incredibly messing.
- 13:14Speaker 2: But in JBuilder you just materialize it. You declare json
- 13:17Speaker 2: dot owner and open a block. Inside that block, you
- 13:20Speaker 2: map the name and ID straight from the created underscore
- 13:23Speaker 2: by relationship. You have literally synthesized a brand new nested
- 13:27Speaker 2: structure out of thin air, perfectly satisfying the client's component,
- 13:31Speaker 2: and the database schema remains completely unaware.
- 13:34Speaker 1: Let's tackle one more requirement dynamic fields. Sometimes you need
- 13:38Speaker 1: a boolean or a calculated value that isn't stored anywhere
- 13:41Speaker 1: at all. Right, the notes show the creation of a
- 13:44Speaker 1: custom field called is underscore single underscore day underscore project.
- 13:48Speaker 1: The front end just wants a simple true or false flag.
- 13:51Speaker 2: This is where we see the perfect synergy between model
- 13:54Speaker 2: and view. In the jabuilder template, we use a specific
- 13:57Speaker 2: method called json dot set exclamation mark to explicitly create
- 14:01Speaker 2: a key name is underscore single underscore day underscore project.
- 14:05Speaker 1: Okay, but where does the math happen? Oh?
- 14:08Speaker 2: For the value, the template actually reaches back to the
- 14:10Speaker 2: model and calls a method named single underscore day underscore project.
- 14:14Speaker 1: Ah. I see the boundary there. The model holds the
- 14:17Speaker 1: logic for actually calculating if the start date and end
- 14:20Speaker 1: date match. That is, pure business logic.
- 14:23Speaker 2: Exactly, the model calculates the truth, but the j builder
- 14:26Speaker 2: template holds the logic for what that truth should be
- 14:29Speaker 2: named in the payload and where it should sit in
- 14:32Speaker 2: the Jason structure. That is the ultimate separation of concerns.
- 14:36Speaker 1: So the dust settles, the Gira ticket is complete. The
- 14:40Speaker 1: API response we send back is highly complex. It features
- 14:43Speaker 1: renamed keys, unix conversions, deeply nested and filtered associations, custom
- 14:48Speaker 1: derived objects, and dynamic calculations a lot. Yet the j
- 14:52Speaker 1: builder template reads like a clean structural map. And more importantly,
- 14:56Speaker 1: if we open up our project model, it is completely pristine.
- 14:59Speaker 1: It only contain validations and data relationships.
- 15:02Speaker 2: It is night and day compared to wrangling hashes. You
- 15:05Speaker 2: can actually read your codebase.
- 15:06Speaker 1: Again, we have this beautifully clean template for our project's list.
- 15:09Speaker 1: But let's zoom out and look at the life span
- 15:11Speaker 1: of this application, because you know, code rarely just sits there.
- 15:15Speaker 2: At scales, unfortunately for us.
- 15:16Speaker 1: Yes, what happens six months down the line when the
- 15:19Speaker 1: product team decides they want a brand new user dashboard endpoint,
- 15:23Speaker 1: and on that dashboard they want to list all of
- 15:26Speaker 1: the user's upcoming milestones using the exact same Jason structure
- 15:30Speaker 1: we just built for the project's list. We obviously don't
- 15:33Speaker 1: want to write that milestone formatting logic a second time.
- 15:36Speaker 2: You absolutely do not. The temptation here is to just
- 15:40Speaker 2: copy the milestone block from your project's template and paste
- 15:44Speaker 2: it into your new dashboard template. I mean it's fast,
- 15:47Speaker 2: it is fast, but doing that introduces massive technical debt.
- 15:51Speaker 2: You are creating a maintenance trap because another six months
- 15:54Speaker 2: from now, the front end team is going to come
- 15:56Speaker 2: back and say, hey, we need you to add a
- 15:58Speaker 2: do underscore date field to every mile stone across the
- 16:01Speaker 2: entire app.
- 16:01Speaker 1: And if you copypasted that code, you are now playing
- 16:04Speaker 1: a terrifying game of find and replace.
- 16:06Speaker 2: Yep, you have to.
- 16:07Speaker 1: Hunt down every single template where a milestone might be rendered,
- 16:10Speaker 1: update it manually, and basically pray you didn't miss one
- 16:13Speaker 1: nested somewhere obscure.
- 16:14Speaker 2: And you inevitably will miss one, causing a bug in production.
- 16:18Speaker 2: So to stay dry. To ensure we don't repeat yourself here,
- 16:22Speaker 2: JBuilder gives us a tool to handle this repetition elegantly.
- 16:26Speaker 2: It's a feature called partials.
- 16:27Speaker 1: Okay, partials.
- 16:28Speaker 2: If you are familiar with building HTML views, it is
- 16:31Speaker 2: the exact same concept applied to Jason. Partials are centralized
- 16:35Speaker 2: reusable blocks of logic placed logically under your views directory,
- 16:40Speaker 2: categorized by resource.
- 16:41Speaker 1: Let's break down the mechanics of this. Because syntax rules
- 16:44Speaker 1: are strict here, how do we actually extract our milestone
- 16:47Speaker 1: logic into a partial.
- 16:49Speaker 2: First we create a new file, and the naming convention
- 16:52Speaker 2: is critical here. If we're creating a partial for a milestone,
- 16:55Speaker 2: the file name absolutely must begin with an underscore.
- 16:58Speaker 1: So just underscore milestone dot json dot.
- 17:01Speaker 2: J Builder exactly. That leading underscore is a direct signal
- 17:05Speaker 2: to the Rails framework. It says, do not try to
- 17:07Speaker 2: render this as a full web page or a primary
- 17:09Speaker 2: API response. This is a modular, reusable chunk of code. Gotcha.
- 17:12Speaker 2: Inside that specific file, you just write your standard JBuilder logic,
- 17:16Speaker 2: just the fields you want for a single milestone, like
- 17:18Speaker 2: the title, the status flag, and the Unix timestamp.
- 17:21Speaker 1: Okay, so we've isolated the logic into this underscored file.
- 17:24Speaker 1: How do we actually inject it back into our main
- 17:27Speaker 1: project template or our new dashboard template.
- 17:30Speaker 2: Inside your main template whenever you need a milestone, you
- 17:33Speaker 2: use the json dot partial exclamation mark method. You pass
- 17:37Speaker 2: it two distinct pieces of information. First, you give it
- 17:40Speaker 2: the filepath to the partial. But here's the trick you
- 17:43Speaker 2: have to remember. When you reference the path in your code,
- 17:47Speaker 2: you drop the underscore. You simply write quotes milestones slash
- 17:51Speaker 2: milestone end quotes.
- 17:52Speaker 1: Okay, no underscore in the past right.
- 17:54Speaker 2: Second, you pass it the actual model object or the
- 17:57Speaker 2: collection of objects that you want the partial to iterate
- 18:00Speaker 2: over and render.
- 18:02Speaker 1: So what does this all mean for the poor developer
- 18:04Speaker 1: who has to maintain this app two years from now.
- 18:07Speaker 2: By doing this, we completely shift the burden of maintenance.
- 18:10Speaker 2: He is no longer a frantic search through a dozen files.
- 18:13Speaker 2: It becomes a centralized architecture.
- 18:15Speaker 1: Exactly when that front end team eventually demands the do
- 18:19Speaker 1: underscore date field, you don't peric You just calmly open
- 18:23Speaker 1: that single underscore milestone j builder file. You add the
- 18:28Speaker 1: one line of code for the new date, and instantly,
- 18:30Speaker 1: every single API endpoint across your entire application that will
- 18:34Speaker 1: turns a milestone is updated simultaneously.
- 18:36Speaker 2: It is absolutely bulletproof.
- 18:37Speaker 1: It really gives you the confidence to scale without fearing
- 18:40Speaker 1: your own code base. All right, let's take a moment
- 18:43Speaker 1: to look back at the architectural journey we've just taken.
- 18:46Speaker 1: We started by identifying the trap of the ash Jason method,
- 18:49Speaker 1: realizing that keeping presentation logic like renaming fields or formatting
- 18:53Speaker 1: dates inside the model leads to unreadable brittle code.
- 18:57Speaker 2: Right. We then introduce j Builder, moving our presentation layer
- 19:01Speaker 2: into dedicated views. We explored how it specialized domain specific
- 19:05Speaker 2: language intercepts our methods to dynamically sketch the shape of
- 19:08Speaker 2: our Jason elegantly handling complex nesting, derived objects, and dynamic
- 19:12Speaker 2: fields without ever polluting our pure business logic.
- 19:16Speaker 1: And finally, we bulletproofed that new architecture against future future
- 19:20Speaker 1: requests by extracting reusable blocks of logic into partials, ensuring
- 19:24Speaker 1: our application remains perfectly dry and deeply maintainable. Now, before
- 19:29Speaker 1: we wrap this up, I have a quick review exercise
- 19:32Speaker 1: for you the listener, to reinforce what we've just covered.
- 19:35Speaker 1: Oh nice, Think back to our discussion on scaling up
- 19:37Speaker 1: and avoiding poppy pasting. Let's say you have a custom
- 19:40Speaker 1: block of Jason data for a viewer object. You realize
- 19:44Speaker 1: this exact viewer structure needs to appear in five entirely
- 19:47Speaker 1: different API responses across your app what specific j bolder
- 19:52Speaker 1: feature should you use to centralize this logic, and what
- 19:54Speaker 1: is the mandatory naming role for the file you create.
- 19:57Speaker 1: I'll give you a second to mentally walk through the steps.
- 20:00Speaker 2: Okay, Well, if you concluded that you need to extract
- 20:02Speaker 2: that logic into a partial and that the file name
- 20:04Speaker 2: absolutely must begin with an underscore, then you have successfully
- 20:08Speaker 2: upgraded your mental model spot on.
- 20:10Speaker 1: You are now fully equipped to build clean, modular APIs.
- 20:14Speaker 2: And as you go back to your code, I want
- 20:16Speaker 2: to leave you with one final thought. Tim all over,
- 20:19Speaker 2: separating your presentation logic from your business logic isn't just
- 20:22Speaker 2: an exercise in making your files look neat and tidy today.
- 20:26Speaker 2: It is essentially an exercise in predicting the future. I
- 20:30Speaker 2: want you to critically evaluate your current projects right now.
- 20:34Speaker 2: Where else in your back end are you letting to
- 20:36Speaker 2: how it looks dictate how it works? Because the stricter
- 20:41Speaker 2: you keep those architectural boundaries, the more resilient your applications
- 20:45Speaker 2: and frankly, your sanity will be when the scale inevitably increases.
- 20:49Speaker 1: That's a great point.
- 20:50Speaker 2: For instance, think about the very next challenge you are
- 20:52Speaker 2: going to face with these massive data payloads pagination. If
- 20:55Speaker 2: your architecture is a tangled as Jason mess, adding complex
- 20:59Speaker 2: pagination is an absolute nightmare. But if your controller is
- 21:02Speaker 2: clean and your views are modular, implementing pagination becomes a breeze.
- 21:06Speaker 2: Keep those boundaries sharp.
- 21:08Speaker 1: Protect the bouncer, and organize the kitchen drawer. Thank you
- 21:11Speaker 1: for joining us on this deep dive into API architecture.
- 21:14Speaker 1: Go clean up those endpoints and we will catch you
- 21:16Speaker 1: next time.