Hi all. I have three Spine models (Artist, Album, Song), and I have an app that allows you to select which type (of the three) to display, and makes a list of records from the selected model. So far so good.
I wanted to filter the records displayed, and I made a filter() method in each model. Works great.
Now I want to filter by multiple things, and I can't figure out how to do it without creating a bunch of filter methods and calling them with a huge if/then list. My filters are artist (matches id), album (matches id), genres (an array of ids), and query (a catchall that matches name). The cumbersome and non-DRY way to do it would be:
if (artist && album && genres && query) {
model.matchAll()
} else if (artist && album && genres) {
model.matchArtAlbGen()
} else if (artist && album && query) {
model.matchArtAlbQu()
} else if (artist && genres && query) {
...and so on, iterating through each possible match and calling the relevant filtering method. This is complicated, unreadable, non-DRY, and gives me a visceral feeling of disgust when I contemplate coding it, and I'm certain there must be a better way. Got any ideas? Is it possible to pass the results of a filter into another filter? (I tried filtering in my controller, but I need to do sorting on the results afterwards, and I can't call model.sort() on a variable I create in the controller unless I create it with model.all() or model.select().)
Like I said, there must be some better way to do it, but I'm stumped.