There's a lot of duplication of conditionals+logic in this file now, could we implement the conditionals once, store that scope in a variable and then set up the results variable (with search + pagination) and total variable (with count) after?