Skip to content

Commit

Permalink
fix(form): use parent documents jexl context for table row documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Metzener authored and anehx committed Dec 27, 2019
1 parent 9a54c85 commit aedf903
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion addon/components/cf-field/input/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default Component.extend({

const newDocument = getOwner(this)
.factoryFor("caluma-model:document")
.create({ raw: parseDocument(raw) });
.create({ raw: parseDocument(raw), parentDocument: this.field.document });

this.setProperties({
documentToEdit: newDocument,
Expand Down
5 changes: 4 additions & 1 deletion addon/lib/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export default Base.extend({
existing ||
getOwner(this)
.factoryFor("caluma-model:document")
.create({ raw: parseDocument(document) })
.create({
raw: parseDocument(document),
parentDocument: this.field.document
})
);
});
}
Expand Down
12 changes: 9 additions & 3 deletions addon/lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,15 @@ export default Base.extend({
* @property {Object} jexlContext
* @accessor
*/
jexlContext: computed("document.rootForm.slug", function() {
return { form: this.rootForm.slug };
}),
jexlContext: computed(
"document.rootForm.slug",
"parentDocument.jexlContext",
function() {
if (this.parentDocument) return this.parentDocument.jexlContext;

return { form: this.rootForm.slug };
}
),

/**
* Find an answer for a given question slug
Expand Down
5 changes: 3 additions & 2 deletions addon/lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ export default Base.extend({
__typename: answerType,
question: { slug: this.raw.question.slug },
[camelize(answerType.replace(/Answer$/, "Value"))]: null
}
},
field: this
});
} else {
answer =
this.calumaStore.find(`Answer:${decodeId(this.raw.answer.id)}`) ||
AnswerFactory.create({ raw: this.raw.answer });
AnswerFactory.create({ raw: this.raw.answer, field: this });
}

this.set("answer", answer);
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/lib/answer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ module("Unit | Library | answer", function(hooks) {
});

test("it generates documents for table answers", async function(assert) {
assert.expect(2);
assert.expect(3);

const answer = this.owner.factoryFor("caluma-model:answer").create({
field: {
document: {
jexlContext: {
form: "parent-form"
}
}
},
raw: {
__typename: "TableAnswer",
tableValue: [
Expand Down Expand Up @@ -86,5 +93,11 @@ module("Unit | Library | answer", function(hooks) {

assert.equal(answer.value[0].pk, "Document:xxxx-xxxx");
assert.deepEqual(answer.serializedValue, ["xxxx-xxxx"]);

assert.deepEqual(
answer.value[0].jexlContext,
{ form: "parent-form" },
"JEXL context of the table rows do not match the parent documents context"
);
});
});

0 comments on commit aedf903

Please sign in to comment.