Fix populating and in eachAlive helper
Filter all alive members before populating and handlebars
fields in eachAlive helper. This fixes an issue where eachAlive would
actually populate with a member who is not alive
@@ -35,16 +35,19 @@ impl HelperDef for EachAliveHelper {
);
let rendered = match (value.value().is_truthy(), value.value()) {
(true, &Json::Array(ref list)) => {
- let len = list.len();
+ let alive_members: Vec<Json> = list.iter()
+ .filter_map(|m| {
+ m.as_object().and_then(|m| if m.contains_key("alive") &&
+ m["alive"].as_bool().unwrap()
+ {
+ Some(to_json(&m))
+ } else {
+ None
+ })
+ })
+ .collect();
+ let len = alive_members.len();
for i in 0..len {
- let member = list[i].as_object().ok_or_else(|| {
- RenderError::new(format!(
- "Param value is not a valid census \
- member. Parameter content is: {:?}",
- list[i]
- ))
- })?;
- if member.contains_key("alive") && member["alive"].as_bool().unwrap() {
let mut local_rc = rc.derive();
local_rc.set_local_var("@first".to_string(), to_json(&(i == 0usize)));
local_rc.set_local_var("@last".to_string(), to_json(&(i == len - 1)));
@@ -52,7 +55,7 @@ impl HelperDef for EachAliveHelper {
if let Some(block_param) = h.block_param() {
let mut map = BTreeMap::new();
- map.insert(block_param.to_string(), to_json(&list[i]));
+ map.insert(block_param.to_string(), to_json(&alive_members[i]));
local_rc.push_block_context(&map);
}
@@ -62,7 +65,6 @@ impl HelperDef for EachAliveHelper {
local_rc.pop_block_context();
}
}
- }
Ok(())
}
(true, &Json::Object(ref obj)) => {
