From 4d1e4216fe28bff58504a6f73f53416235f74811 Mon Sep 17 00:00:00 2001 From: Thomas Leishman Date: Wed, 22 Mar 2017 23:39:24 -0600 Subject: [PATCH] simplify queries --- lib/redmine_digest/digest.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/redmine_digest/digest.rb b/lib/redmine_digest/digest.rb index 5752bd6..abd6f65 100644 --- a/lib/redmine_digest/digest.rb +++ b/lib/redmine_digest/digest.rb @@ -178,10 +178,14 @@ def get_changed_issue_ids end def get_created_issue_ids - issues = Issue.where('issues.project_id in (?)', project_ids). - where('issues.created_on >= ? and issues.created_on < ?', time_from, time_to) - issues = issues.where('(issues.assigned_to_id = ? OR issues.author_id = ?)', user.id, user.id) if all_involved_only? - issues.uniq.pluck(:id) + Issue.where('issues.project_id in (?)', project_ids). + where('issues.created_on >= ? and issues.created_on < ?', time_from, time_to). + where(user_is_involved_in_issues). + uniq.pluck(:id) + end + + def user_is_involved_in_issues + ['issues.assigned_to_id = :user_id OR issues.author_id = :user_id', {user_id: user.id}] if all_involved_only? end def get_issues_scope(issue_ids) @@ -202,9 +206,9 @@ def get_journal_all_involved_scope Journal.joins(:issue). joins("LEFT JOIN journal_details ON journals.id = journal_details.journal_id AND property = 'attr' AND prop_key = 'assigned_to_id'"). joins("LEFT JOIN watchers ON watchers.watchable_type='Issue' AND watchers.watchable_id = issues.id"). - where('watchers.user_id = ? OR issues.author_id = ? OR issues.assigned_to_id = ? OR - journal_details.old_value = ? OR journal_details.value = ? OR journals.user_id = ?', - user.id, user.id, user.id, user.id, user.id, user.id) + where('watchers.user_id = :user_id OR issues.author_id = :user_id OR issues.assigned_to_id = :user_id OR + journal_details.old_value = :user_id OR journal_details.value = :user_id OR journals.user_id = :user_id', + {user_id: user.id}) end