From cda7cbde03ca5908144df2d37f32e4142de73174 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Fri, 7 Jul 2017 18:31:50 +0200 Subject: [PATCH] refactor created at filter to use model scopes --- app/finders/created_at_filter.rb | 8 ++++++++ app/finders/issuable_finder.rb | 4 ++-- app/finders/users_finder.rb | 2 +- app/models/concerns/created_at_filterable.rb | 12 ++++++++++++ app/models/issue.rb | 1 + app/models/user.rb | 1 + lib/gitlab/database/created_at_filter.rb | 17 ----------------- 7 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 app/finders/created_at_filter.rb create mode 100644 app/models/concerns/created_at_filterable.rb delete mode 100644 lib/gitlab/database/created_at_filter.rb diff --git a/app/finders/created_at_filter.rb b/app/finders/created_at_filter.rb new file mode 100644 index 00000000000..ac9ac77732c --- /dev/null +++ b/app/finders/created_at_filter.rb @@ -0,0 +1,8 @@ +module CreatedAtFilter + def by_created_at(items) + items = items.created_before(params[:created_before]) if params[:created_before].present? + items = items.created_after(params[:created_after]) if params[:created_after].present? + + items + end +end diff --git a/app/finders/issuable_finder.rb b/app/finders/issuable_finder.rb index 39427d83669..6e37c10166d 100644 --- a/app/finders/issuable_finder.rb +++ b/app/finders/issuable_finder.rb @@ -19,7 +19,7 @@ # iids: integer[] # class IssuableFinder - include Gitlab::Database::CreatedAtFilter + include CreatedAtFilter NONE = '0'.freeze IRRELEVANT_PARAMS_FOR_CACHE_KEY = %i[utf8 sort page].freeze @@ -34,6 +34,7 @@ class IssuableFinder def execute items = init_collection items = by_scope(items) + items = by_created_at(items) items = by_state(items) items = by_group(items) items = by_search(items) @@ -44,7 +45,6 @@ class IssuableFinder items = by_iids(items) items = by_milestone(items) items = by_label(items) - items = by_created_at(items) # Filtering by project HAS TO be the last because we use the project IDs yielded by the issuable query thus far items = by_project(items) diff --git a/app/finders/users_finder.rb b/app/finders/users_finder.rb index c6bfc380c1c..33f7ae90598 100644 --- a/app/finders/users_finder.rb +++ b/app/finders/users_finder.rb @@ -14,7 +14,7 @@ # external: boolean # class UsersFinder - include Gitlab::Database::CreatedAtFilter + include CreatedAtFilter attr_accessor :current_user, :params diff --git a/app/models/concerns/created_at_filterable.rb b/app/models/concerns/created_at_filterable.rb new file mode 100644 index 00000000000..e8a3e41203d --- /dev/null +++ b/app/models/concerns/created_at_filterable.rb @@ -0,0 +1,12 @@ +module CreatedAtFilterable + extend ActiveSupport::Concern + + included do + scope :created_before, ->(date) { where(scoped_table[:created_at].lteq(date)) } + scope :created_after, ->(date) { where(scoped_table[:created_at].gteq(date)) } + + def self.scoped_table + arel_table.alias(table_name) + end + end +end diff --git a/app/models/issue.rb b/app/models/issue.rb index 01f985823e1..6b686d4e113 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -10,6 +10,7 @@ class Issue < ActiveRecord::Base include FasterCacheKeys include RelativePositioning include IgnorableColumn + include CreatedAtFilterable ignore_column :position diff --git a/app/models/user.rb b/app/models/user.rb index 4411a06d429..4b01c2f19f0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,6 +12,7 @@ class User < ActiveRecord::Base include TokenAuthenticatable include IgnorableColumn include FeatureGate + include CreatedAtFilterable DEFAULT_NOTIFICATION_LEVEL = :participating diff --git a/lib/gitlab/database/created_at_filter.rb b/lib/gitlab/database/created_at_filter.rb deleted file mode 100644 index 3fa2770c65f..00000000000 --- a/lib/gitlab/database/created_at_filter.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Gitlab - module Database - module CreatedAtFilter - def by_created_at(items) - if params[:created_after].present? - items = items.where(items.klass.arel_table[:created_at].gteq(params[:created_after])) - end - - if params[:created_before].present? - items = items.where(items.klass.arel_table[:created_at].lteq(params[:created_before])) - end - - items - end - end - end -end