refactor created at filter to use model scopes

This commit is contained in:
James Lopez 2017-07-07 18:31:50 +02:00
parent 5e66c6568b
commit cda7cbde03
7 changed files with 25 additions and 20 deletions

View File

@ -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

View File

@ -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)

View File

@ -14,7 +14,7 @@
# external: boolean
#
class UsersFinder
include Gitlab::Database::CreatedAtFilter
include CreatedAtFilter
attr_accessor :current_user, :params

View File

@ -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

View File

@ -10,6 +10,7 @@ class Issue < ActiveRecord::Base
include FasterCacheKeys
include RelativePositioning
include IgnorableColumn
include CreatedAtFilterable
ignore_column :position

View File

@ -12,6 +12,7 @@ class User < ActiveRecord::Base
include TokenAuthenticatable
include IgnorableColumn
include FeatureGate
include CreatedAtFilterable
DEFAULT_NOTIFICATION_LEVEL = :participating

View File

@ -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