how can clean bit? want rid of unnecessary code repeated a.question == f.question
comparisons use on each case
statement:
def notifications_lookup(filters, answers) filters.flat_map |f| answers.select |a| case a.question.question_type when "image" a.question == f.question && a.answer_image.image_id == f.answer.to_i when "single" a.question == f.question && a.choice_answer.choice_id == f.answer.to_i when "list" a.question == f.question && a.choice_answer.choice_id == f.answer.to_i when "multiple" a.question == f.question && !(f.answer.split(",").map(&:to_i) & a.answer_multiple.choices.map(&:id)).empty? when "rating" results = verify_condition(f, a) a.question == f.question && results.any? else a.question == f.question end end end end def verify_condition(filter, a) a.answer_raitings.map |r| r.raiting == filter.raiting && filter.answer.split(",").map(&:to_i).include?(r.response) end end
remember ruby lets these things in several simple passes. there's no need in 1 go:
def notifications_lookup(filters, answers) filters.flat_map |f| answers.select |a| a.question == f.question end.select |a| case a.question.question_type when "image" a.answer_image.image_id == f.answer.to_i when "single", "list" a.choice_answer.choice_id == f.answer.to_i when "multiple" !(f.answer.split(",").map(&:to_i) & a.answer_multiple.choices.map(&:id)).empty? when "rating" verify_condition(f, a).any? else true end end end end
another quick fix combine 2 clauses had same code.
you might make easier writing ==
method on answer model can comparison you.
Comments
Post a Comment