AE
Ash Elixir•2y ago
rohan

Composing filter expressions

I'm trying to figure out how to reuse/compose (eg. apply a not) a complex expression that I use in filters. I have a complex filter expression that looks like this:
read :get_processed_for_visit do

filter expr(
not is_nil(visit.processing_started_at) and
summary_processed_at(is_nil(false)) and
summary_processed_at > visit.processing_started_at
)
end
read :get_processed_for_visit do

filter expr(
not is_nil(visit.processing_started_at) and
summary_processed_at(is_nil(false)) and
summary_processed_at > visit.processing_started_at
)
end
and then another one that is:
read :get_unprocessed do
filter expr(
not (not is_nil(visit.processing_started_at) and
summary_processed_at(is_nil(false)) and
summary_processed_at > visit.processing_started_at)
)
end
read :get_unprocessed do
filter expr(
not (not is_nil(visit.processing_started_at) and
summary_processed_at(is_nil(false)) and
summary_processed_at > visit.processing_started_at)
)
end
(ie. it's just the not of the previous one) Is there a way to extract out the expression so I can do something like: filter expr(processed) and filter expr(not processed) I don't think I can use a calculation because it's using visit which is a relationship
8 Replies
ZachDaniel
ZachDaniel•2y ago
you can add an aggregate, and then use that in the calculation
aggregates do
first :visit_processing_started_at, :visit, :processing_started_at
end

calculations do
calculate :processed, expr(not is_nil(visit_processing_started_at) and ...)
end
aggregates do
first :visit_processing_started_at, :visit, :processing_started_at
end

calculations do
calculate :processed, expr(not is_nil(visit_processing_started_at) and ...)
end
rohan
rohanOP•2y ago
ohh that's clever haha
ZachDaniel
ZachDaniel•2y ago
another note: summary_processed_at(is_nil(false)) is probably not what you want i.e not is_nil(summary_processed_at)
rohan
rohanOP•2y ago
sorry little confused - is there a difference between those two or just aesthetically the latter is preferred i was a little confused by the is_nil stuff bc in the docs it said it's used like: x is_nil true
ZachDaniel
ZachDaniel•2y ago
hmm....gimme a sec 🙂 I believe that is incorrect you can use it when using the data structure version of filters, i.e x: [is_nil: true] huh looks like that does work I think I need to undo that because then you cant call functions w/ the result of is_nil(true) yeah, okay thats a bug 🙂 I'm fixing it now. It might break people's apps but there isn't really a way around it
rohan
rohanOP•2y ago
haha ok
\ ឵឵឵
If I'm right in thinking that the limitation on using attributes of related resources in calculations is primarily about the potential for there to be multiple related resources, could this be supported for has_one and belongs_to relationships?
ZachDaniel
ZachDaniel•2y ago
yes, eventually 🙂 I think there is an issue for that actually

Did you find this page helpful?