From f6a0c90eb1eb797ccf4667cc940c0d4093d00ec3 Mon Sep 17 00:00:00 2001 From: Hagen Schink Date: Tue, 28 Jan 2014 17:53:42 +0100 Subject: [PATCH] Moves changed since condition to scope --- .../api/v2/planning_elements_controller.rb | 15 ++++++++++----- app/models/work_package.rb | 4 ++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v2/planning_elements_controller.rb b/app/controllers/api/v2/planning_elements_controller.rb index 28dd5ee28b..a7df986e71 100644 --- a/app/controllers/api/v2/planning_elements_controller.rb +++ b/app/controllers/api/v2/planning_elements_controller.rb @@ -38,6 +38,7 @@ module Api before_filter :find_project_by_project_id, :authorize, :except => [:index] + before_filter :parse_changed_since, only: [:index] before_filter :assign_planning_elements, :except => [:index, :update, :create] # Attention: find_all_projects_by_project_id needs to mimic all of the above @@ -207,13 +208,9 @@ module Api def current_work_packages(projects) work_packages = WorkPackage.for_projects(projects) + .changed_since(@since) .includes(:status, :project, :type) - if params[:changed_since] - since = Time.at(Float(params[:changed_since]).to_i) rescue render_400 - work_packages = work_packages.where(["updated_at >= ?", since]) - end - if params[:f] query = Query.new query.add_filters(params[:f], params[:op], params[:v]) @@ -283,6 +280,14 @@ module Api end + + private + + def parse_changed_since + if params[:changed_since] + @since = Time.at(Float(params[:changed_since]).to_i) rescue render_400 + end + end end end end diff --git a/app/models/work_package.rb b/app/models/work_package.rb index 10a42114fe..eb436e6e3c 100644 --- a/app/models/work_package.rb +++ b/app/models/work_package.rb @@ -78,6 +78,10 @@ class WorkPackage < ActiveRecord::Base {:conditions => {:project_id => projects}} } + scope :changed_since, lambda { |changed_since| + changed_since ? { conditions: ["updated_at >= ?", changed_since] } : nil + } + # >>> issues.rb >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> scope :open, :conditions => ["#{Status.table_name}.is_closed = ?", false], :include => :status