kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.0 KiB
125 lines
3.0 KiB
8 years ago
|
#-- copyright
|
||
|
# OpenProject is a project management system.
|
||
|
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License version 3.
|
||
|
#
|
||
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
|
# as published by the Free Software Foundation; either version 2
|
||
|
# of the License, or (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
#
|
||
|
# See doc/COPYRIGHT.rdoc for more details.
|
||
|
#++
|
||
|
|
||
|
class Queries::BaseQuery
|
||
|
class << self
|
||
|
attr_accessor :model, :default_scope
|
||
|
end
|
||
|
|
||
|
include Queries::AvailableFilters
|
||
|
include Queries::AvailableOrders
|
||
|
include ActiveModel::Validations
|
||
|
|
||
|
validate :filters_valid,
|
||
|
:sortation_valid
|
||
|
|
||
|
def initialize
|
||
|
@scope = self.class.default_scope
|
||
|
@filters = []
|
||
|
@orders = []
|
||
|
end
|
||
|
|
||
|
def results
|
||
|
if valid?
|
||
|
filters.each do |filter|
|
||
|
self.scope = scope.merge(filter.scope)
|
||
|
end
|
||
|
|
||
|
orders.each do |order|
|
||
|
self.scope = scope.merge(order.scope)
|
||
|
end
|
||
|
else
|
||
|
empty_scope
|
||
|
end
|
||
|
|
||
|
scope
|
||
|
end
|
||
|
|
||
|
def where(attribute, operator, values)
|
||
|
filter = filter_for(attribute)
|
||
|
filter.operator = operator
|
||
|
filter.values = values
|
||
|
filter.context = context
|
||
|
|
||
|
filters << filter
|
||
|
|
||
|
self
|
||
|
end
|
||
|
|
||
|
def order(hash)
|
||
|
hash.each do |attribute, direction|
|
||
|
order = order_for(attribute)
|
||
|
order.direction = direction
|
||
|
orders << order
|
||
|
end
|
||
|
|
||
|
self
|
||
|
end
|
||
|
|
||
|
protected
|
||
|
|
||
|
attr_accessor :scope,
|
||
|
:filters,
|
||
|
:orders
|
||
|
|
||
|
def filters_valid
|
||
|
filters.each do |filter|
|
||
|
next if filter.valid?
|
||
|
|
||
|
add_error(:filters, filter.human_name, filter)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def sortation_valid
|
||
|
orders.each do |order|
|
||
|
next if order.valid?
|
||
|
|
||
|
add_error(:orders, order.class.key, order)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def add_error(local_attribute, attribute_name, object)
|
||
|
messages = object
|
||
|
.errors
|
||
|
.messages
|
||
|
.values
|
||
|
.flatten
|
||
|
.join(" #{I18n.t('support.array.sentence_connector')} ")
|
||
|
|
||
|
errors.add local_attribute, errors.full_message(attribute_name, messages)
|
||
|
end
|
||
|
|
||
|
def empty_scope
|
||
|
self.scope = self.class.model.where(Arel::Nodes::Equality.new(1, 0))
|
||
|
end
|
||
|
|
||
|
def context
|
||
|
nil
|
||
|
end
|
||
|
end
|