forked from bryanlarsen/agility-gitorial-patches
-
Notifications
You must be signed in to change notification settings - Fork 1
/
16-customizing-views-1.patch
58 lines (39 loc) · 3.99 KB
/
16-customizing-views-1.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
16-customizing-views-1.patch
From: Ignacio Huerta <[email protected]>
# Customising views
It's pretty surprising how far you can get without even touching the view layer. That's the way we like to work with Hobo - get the models and controllers right and the view will probably get close to what you want. From there you can override just those parts of the view that you need to.
We do that using the DRYML template language which is part of Hobo. DRYML is tag based -- it allows you to define and use your own tags right alongside the regular HTML tags. Tags are like helpers, but a lot more powerful. DRYML is quite unlike other tag-based template languages, thanks to features like the implicit context and nestable parameters. DRYML is also an extension of ERB so you can still use the ERB syntax you're familiar with from Rails.
DRYML is probably the single best part of Hobo. It's very good at high-level re-use because it allows you to make very focussed changes if a given piece of pre-packaged HTML is not *quite* what you want.
A full coverage of DRYML is well beyond the scope of this tutorial. Instead we're going to take a few specific examples of changes we'd like to make to Agility, and see how they're done.
## Add assigned users to the tasks
At the moment, the only way to see who's assigned to a task is to click the task's edit link. Not good. Let's add a list of the assigned users to each task when we're looking at a story.
DRYML has a feature called *polymorphic tags*. These are tags that are defined differently for different types of object. Rapid makes use of this feature with a system of "cards". The tasks that are displayed on the story page are rendered by the `<card>`. You can define custom cards for particular models. Furthermore, if you call `<base-card>` you can define your card by tweaking the default, rather than starting from scratch. This is what DRYML is all about. It's like a smart-bomb, capable of taking out little bits of unwanted HTML with pin-point strikes and no collateral damage.
The file `app/views/taglibs/front_site.dryml` is a place to put tag definitions that will be available throughout the site. Add this definition to that file:
SHOW_PATCH
OK there's a lot of new concepts thrown at you at once there :-)
First off, refresh the story page. You'll see that in the cards for each task there is now a list of assigned users. The users are clickable - they link to each users home page (which doesn't have much on it at the moment).
The `<extend>` tag is used to extend any tag that's already defined. The body of `<extend>` is our new definition. It's very common to want to base the new definition on the old one, for example, we often want to insert a bit of extra content as we've done here. We can do that by calling the "old" definition, which is available as `<old-card>`. We've passed the `<append-body:>` parameter to `<old-card>` which, in a startling twist, is used to append content to the body of the card. Some points to note:
* The `<repeat>` tag provides a `join` attribute which we use to insert the commas
* The link is created with a simple empty `<a/>`. It links to the 'current context' which, in this case, is the user.
* The `:users` in `<repeat:users>` switches the context. It selects the `users` association of the task.
* DRYML has a multi-purpose `<else>` tag. When used with repeat, it provides a default for the case when the collection is empty.
---
app/views/taglibs/front_site.dryml | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/app/views/taglibs/front_site.dryml b/app/views/taglibs/front_site.dryml
index f16084d..7f6da4c 100644
--- a/app/views/taglibs/front_site.dryml
+++ b/app/views/taglibs/front_site.dryml
@@ -15,3 +15,13 @@
<include src="taglibs/auto/rapid/forms"/>
<include src="application"/>
+
+<extend tag="card" for="Task">
+ <old-card merge>
+ <append-body:>
+ <div class="users">
+ Assigned users: <repeat:users join=", "><a/></repeat><else>None</else>
+ </div>
+ </append-body:>
+ </old-card>
+</extend>