Commit 3a7f48306c355e761fee7514f5cbf1a3638e8269
1 parent
be57a405
Exists in
send_email_to_admins
and in
5 other branches
Transforms filter methods into scopes
Showing
2 changed files
with
25 additions
and
19 deletions
Show diff stats
app/controllers/my_profile/tasks_controller.rb
| ... | ... | @@ -108,30 +108,21 @@ class TasksController < MyProfileController |
| 108 | 108 | |
| 109 | 109 | protected |
| 110 | 110 | |
| 111 | - def filter_by_closed_date(filter, tasks) | |
| 112 | - filter[:closed_from] = Date.parse(filter[:closed_from]) unless filter[:closed_from].blank? | |
| 113 | - filter[:closed_until] = Date.parse(filter[:closed_until]) unless filter[:closed_until].blank? | |
| 114 | - | |
| 115 | - tasks = tasks.where('tasks.end_date >= ?', filter[:closed_from].beginning_of_day) unless filter[:closed_from].blank? | |
| 116 | - tasks = tasks.where('tasks.end_date <= ?', filter[:closed_until].end_of_day) unless filter[:closed_until].blank? | |
| 117 | - tasks | |
| 118 | - end | |
| 111 | + def filter_tasks(filter, tasks) | |
| 112 | + tasks = tasks.eager_load(:requestor, :closed_by) | |
| 113 | + tasks = tasks.of(filter[:type].presence) | |
| 114 | + tasks = tasks.where(:status => filter[:status]) unless filter[:status].blank? | |
| 119 | 115 | |
| 120 | - def filter_by_creation_date(filter, tasks) | |
| 121 | 116 | filter[:created_from] = Date.parse(filter[:created_from]) unless filter[:created_from].blank? |
| 122 | 117 | filter[:created_until] = Date.parse(filter[:created_until]) unless filter[:created_until].blank? |
| 118 | + filter[:closed_from] = Date.parse(filter[:closed_from]) unless filter[:closed_from].blank? | |
| 119 | + filter[:closed_until] = Date.parse(filter[:closed_until]) unless filter[:closed_until].blank? | |
| 123 | 120 | |
| 124 | - tasks = tasks.where('tasks.created_at >= ?', filter[:created_from].beginning_of_day) unless filter[:created_from].blank? | |
| 125 | - tasks = tasks.where('tasks.created_at <= ?', filter[:created_until].end_of_day) unless filter[:created_until].blank? | |
| 126 | - tasks | |
| 127 | - end | |
| 121 | + tasks = tasks.from_creation_date filter[:created_from] unless filter[:created_from].blank? | |
| 122 | + tasks = tasks.until_creation_date filter[:created_until] unless filter[:created_until].blank? | |
| 128 | 123 | |
| 129 | - def filter_tasks(filter, tasks) | |
| 130 | - tasks = tasks.eager_load(:requestor, :closed_by) | |
| 131 | - tasks = tasks.of(filter[:type].presence) | |
| 132 | - tasks = tasks.where(:status => filter[:status]) unless filter[:status].blank? | |
| 133 | - tasks = filter_by_creation_date(filter, tasks) | |
| 134 | - tasks = filter_by_closed_date(filter, tasks) | |
| 124 | + tasks = tasks.from_closed_date filter[:closed_from] unless filter[:closed_from].blank? | |
| 125 | + tasks = tasks.until_closed_date filter[:closed_until] unless filter[:closed_until].blank? | |
| 135 | 126 | |
| 136 | 127 | tasks = tasks.where('profiles.name LIKE ?', filter[:requestor]) unless filter[:requestor].blank? |
| 137 | 128 | tasks = tasks.where('closed_bies_tasks.name LIKE ?', filter[:closed_by]) unless filter[:closed_by].blank? | ... | ... |
app/models/task.rb
| ... | ... | @@ -324,6 +324,21 @@ class Task < ActiveRecord::Base |
| 324 | 324 | where [environment_condition, profile_condition].compact.join(' OR ') |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | + scope :from_closed_date, -> closed_from { | |
| 328 | + where('tasks.end_date >= ?', closed_from.beginning_of_day) unless closed_from.blank? | |
| 329 | + } | |
| 330 | + | |
| 331 | + scope :until_closed_date, -> closed_until { | |
| 332 | + where('tasks.end_date <= ?', closed_until.end_of_day) unless closed_until.blank? | |
| 333 | + } | |
| 334 | + | |
| 335 | + scope :from_creation_date, -> created_from { | |
| 336 | + where('tasks.created_at >= ?', created_from.beginning_of_day) unless created_from.blank? | |
| 337 | + } | |
| 338 | + | |
| 339 | + scope :until_creation_date, -> created_until { | |
| 340 | + where('tasks.created_at <= ?', created_until.end_of_day) unless created_until.blank? | |
| 341 | + } | |
| 327 | 342 | |
| 328 | 343 | def self.pending_types_for(profile) |
| 329 | 344 | Task.to(profile).pending.select('distinct type').map { |t| [t.class.name, t.title] } | ... | ... |