Currently, when writing a calculation, the third argument is the expression.
For example:
calculate :full_name, :string, expr(first_name <> " " <> last_name)
This looks clean and simple, but when the calculation is a little bit more complex, it starts to get a little bit messy:
calculate :total_feedbacks_in_class_this_month,
:integer,
expr(
count(feedbacks,
query: [
filter:
expr(
class_id == ^arg(:class_id) and
inserted_at >= fragment("date_trunc('month', now())") and
inserted_at <
fragment("date_trunc('month', now()) + interval '1 month'")
)
]
)
) do
argument :class_id, :string, allow_nil?: false
end
I was wondering if we could add an alternative way to write the calculations where we can not write the third argument and alternatively add it inside the calculate
code block.
Something like this:
calculate :total_feedbacks_in_class_this_month, :integer do
expression expr(
count(feedbacks,
query: [
filter:
expr(
class_id == ^arg(:class_id) and
inserted_at >= fragment("date_trunc('month', now())") and
inserted_at < fragment("date_trunc('month', now()) + interval '1 month'")
)
]
)
)
argument :class_id, :string, allow_nil?: false
end
At least for me, this seems easier to read.