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.
384 lines
10 KiB
384 lines
10 KiB
11 years ago
|
#-- copyright
|
||
5 years ago
|
# OpenProject is an open source project management software.
|
||
|
# Copyright (C) 2012-2020 the OpenProject GmbH
|
||
11 years ago
|
#
|
||
5 years ago
|
# 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-2017 Jean-Philippe Lang
|
||
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
11 years ago
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
5 years ago
|
# as published by the Free Software Foundation; either version 2
|
||
|
# of the License, or (at your option) any later version.
|
||
11 years ago
|
#
|
||
|
# 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.
|
||
5 years ago
|
#
|
||
|
# See docs/COPYRIGHT.rdoc for more details.
|
||
11 years ago
|
#++
|
||
|
|
||
14 years ago
|
class Report::Operator
|
||
|
include Report::QueryUtils
|
||
|
include Report::Validation
|
||
14 years ago
|
extend Forwardable
|
||
15 years ago
|
|
||
|
#############################################################################################
|
||
|
# Wrapped so we can place this at the top of the file.
|
||
|
def self.define_operators # :nodoc:
|
||
|
# Defaults
|
||
|
defaults do
|
||
14 years ago
|
def_delegators :'singleton_class', :forced?, :force!, :forced
|
||
14 years ago
|
|
||
15 years ago
|
def sql_operator
|
||
|
name
|
||
|
end
|
||
|
|
||
|
def where_clause
|
||
|
"%s %s '%s'"
|
||
|
end
|
||
|
|
||
|
def modify(query, field, *values)
|
||
|
query.where [where_clause, field, sql_operator, *values]
|
||
|
query
|
||
|
end
|
||
|
|
||
|
def label
|
||
12 years ago
|
@label ||= self.class.name
|
||
15 years ago
|
end
|
||
|
end
|
||
|
|
||
|
# Operators from Redmine
|
||
10 years ago
|
new '>t-', label: :label_less_than_ago do
|
||
15 years ago
|
include DateRange
|
||
|
def modify(query, field, value)
|
||
|
super query, field, -value.to_i, 0
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new 'w', arity: 0, label: :label_this_week do
|
||
15 years ago
|
def modify(query, field, offset = nil)
|
||
14 years ago
|
offset ||= 0
|
||
|
first_day = begin
|
||
|
Integer I18n.t(:general_first_day_of_week)
|
||
|
rescue ArgumentError
|
||
|
1 # assume mondays
|
||
|
end
|
||
|
|
||
14 years ago
|
from = Time.now.at_beginning_of_week + ((first_day % 7) - 1).days
|
||
15 years ago
|
from -= offset.days
|
||
|
'<>d'.to_operator.modify query, field, from, from + 7.days
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new 't+', label: :label_in do
|
||
15 years ago
|
include DateRange
|
||
|
def modify(query, field, *values)
|
||
|
super query, field, values.first.to_i, values.first.to_i
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '<=', label: :label_less_or_equal
|
||
15 years ago
|
|
||
10 years ago
|
new '!', label: :label_not_equals do
|
||
15 years ago
|
def modify(query, field, *values)
|
||
14 years ago
|
where_clause = "(#{field} IS NULL"
|
||
14 years ago
|
where_clause += " OR #{field} NOT IN #{collection(*values)}" unless values.compact.empty?
|
||
10 years ago
|
where_clause += ')'
|
||
14 years ago
|
query.where where_clause
|
||
15 years ago
|
query
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new 't-', label: :label_ago do
|
||
15 years ago
|
include DateRange
|
||
|
def modify(query, field, *values)
|
||
|
super query, field, -values.first.to_i, -values.first.to_i
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '!~', arity: 1, label: :label_not_contains do
|
||
15 years ago
|
def modify(query, field, *values)
|
||
|
value = values.first || ''
|
||
15 years ago
|
query.where "LOWER(#{field}) NOT LIKE '%#{quote_string(value.to_s.downcase)}%'"
|
||
|
query
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '=', label: :label_equals do
|
||
15 years ago
|
def modify(query, field, *values)
|
||
14 years ago
|
case
|
||
|
when values.size == 1 && values.first.nil?
|
||
|
query.where "#{field} IS NULL"
|
||
|
when values.compact.empty?
|
||
10 years ago
|
query.where '1=0'
|
||
14 years ago
|
else
|
||
|
query.where "#{field} IN #{collection(*values)}"
|
||
|
end
|
||
15 years ago
|
query
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '~', arity: 1, label: :label_contains do
|
||
15 years ago
|
def modify(query, field, *values)
|
||
|
value = values.first || ''
|
||
15 years ago
|
query.where "LOWER(#{field}) LIKE '%#{quote_string(value.to_s.downcase)}%'"
|
||
|
query
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '<t+', label: :label_in_less_than do
|
||
15 years ago
|
include DateRange
|
||
|
def modify(query, field, value)
|
||
|
super query, field, 0, value.to_i
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new 't', label: :label_today do
|
||
15 years ago
|
include DateRange
|
||
15 years ago
|
def modify(query, field)
|
||
15 years ago
|
super query, field, 0, 0
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '>=', label: :label_greater_or_equal
|
||
15 years ago
|
|
||
10 years ago
|
new '!*', arity: 0, where_clause: '%s IS NULL', label: :label_none
|
||
15 years ago
|
|
||
10 years ago
|
new '<t-', label: :label_more_than_ago do
|
||
15 years ago
|
include DateRange
|
||
|
def modify(query, field, value)
|
||
|
super query, field, nil, -value.to_i
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '>t+', label: :label_in_more_than do
|
||
15 years ago
|
include DateRange
|
||
|
def modify(query, field, value)
|
||
|
super query, field, value.to_i, nil
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '*', arity: 0, where_clause: '%s IS NOT NULL', label: :label_all
|
||
15 years ago
|
|
||
|
# Our own operators
|
||
10 years ago
|
new '<', label: :label_less
|
||
|
new '>', label: :label_greater
|
||
15 years ago
|
|
||
10 years ago
|
new '=n', label: :label_equals do
|
||
15 years ago
|
def modify(query, field, value)
|
||
5 years ago
|
query.where "#{field} = #{parse_number_string(value)}"
|
||
15 years ago
|
query
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '0', label: :label_none, where_clause: '%s = 0'
|
||
|
new 'y', label: :label_yes, arity: 0, where_clause: '%s IS NOT NULL'
|
||
|
new 'n', label: :label_no, arity: 0, where_clause: '%s IS NULL'
|
||
15 years ago
|
|
||
10 years ago
|
new '<d', label: :label_less_or_equal, validate: :dates do
|
||
15 years ago
|
def modify(query, field, value)
|
||
14 years ago
|
return query if value.to_s.empty?
|
||
10 years ago
|
'<='.to_operator.modify query, field, quoted_date(value)
|
||
15 years ago
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '>d', label: :label_greater_or_equal, validate: :dates do
|
||
15 years ago
|
def modify(query, field, value)
|
||
14 years ago
|
return query if value.to_s.empty?
|
||
10 years ago
|
'>='.to_operator.modify query, field, quoted_date(value)
|
||
15 years ago
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '<>d', label: :label_between, validate: :dates do
|
||
15 years ago
|
def modify(query, field, from, to)
|
||
14 years ago
|
return query if from.to_s.empty? || to.to_s.empty?
|
||
15 years ago
|
query.where "#{field} BETWEEN '#{quoted_date from}' AND '#{quoted_date to}'"
|
||
|
query
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '=d', label: :label_date_on, validate: :dates do
|
||
15 years ago
|
def modify(query, field, value)
|
||
14 years ago
|
return query if value.to_s.empty?
|
||
10 years ago
|
'='.to_operator.modify query, field, quoted_date(value)
|
||
15 years ago
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '>=d', label: :label_days_ago, validate: :integers do
|
||
14 years ago
|
force! :integers
|
||
14 years ago
|
|
||
14 years ago
|
def modify(query, field, value)
|
||
|
now = Time.now
|
||
14 years ago
|
from = (now - value.to_i.days).beginning_of_day
|
||
14 years ago
|
'<>d'.to_operator.modify query, field, from, now
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '?=', label: :label_null_or_equal do
|
||
14 years ago
|
def modify(query, field, *values)
|
||
|
where_clause = "(#{field} IS NULL"
|
||
|
where_clause += " OR #{field} IN #{collection(*values)}" unless values.compact.empty?
|
||
10 years ago
|
where_clause += ')'
|
||
14 years ago
|
query.where where_clause
|
||
|
query
|
||
|
end
|
||
|
end
|
||
|
|
||
10 years ago
|
new '?!', label: :label_not_null_and_not_equal do
|
||
14 years ago
|
def modify(query, field, *values)
|
||
|
where_clause = "(#{field} IS NOT NULL"
|
||
|
where_clause += " AND #{field} NOT IN #{collection(*values)}" unless values.compact.empty?
|
||
10 years ago
|
where_clause += ')'
|
||
14 years ago
|
query.where where_clause
|
||
|
query
|
||
|
end
|
||
|
end
|
||
15 years ago
|
end
|
||
|
#############################################################################################
|
||
|
|
||
|
module CoreExt
|
||
|
::String.send :include, self
|
||
|
::Symbol.send :include, self
|
||
|
def to_operator
|
||
14 years ago
|
Report::Operator.find self
|
||
15 years ago
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
def self.force!(type)
|
||
|
@force = type
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
def self.forced?
|
||
|
!!@force
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
def self.forced
|
||
|
@force
|
||
14 years ago
|
end
|
||
|
|
||
15 years ago
|
def self.new(name, values = {}, &block)
|
||
|
all[name.to_s] ||= super
|
||
|
end
|
||
|
|
||
10 years ago
|
# TODO: this should be inheritable by subclasses
|
||
15 years ago
|
def self.all
|
||
14 years ago
|
@@all_operators ||= {}
|
||
15 years ago
|
end
|
||
|
|
||
|
def self.load
|
||
|
return if @done
|
||
|
@done = true
|
||
|
define_operators
|
||
|
end
|
||
|
|
||
|
def self.find(name)
|
||
14 years ago
|
all[name.to_s] or raise ArgumentError, "Operator #{name.inspect} not defined"
|
||
15 years ago
|
end
|
||
|
|
||
14 years ago
|
def self.exists?(name)
|
||
|
all.has_key?(name.to_s)
|
||
|
end
|
||
|
|
||
15 years ago
|
def self.defaults(&block)
|
||
|
class_eval &block
|
||
|
end
|
||
|
|
||
|
def self.default_operator
|
||
10 years ago
|
find '='
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def self.integer_operators
|
||
10 years ago
|
['<', '>', '<=', '>='].map(&:to_operator)
|
||
15 years ago
|
end
|
||
|
|
||
|
def self.null_operators
|
||
10 years ago
|
['*', '!*'].map(&:to_operator)
|
||
15 years ago
|
end
|
||
|
|
||
|
def self.string_operators
|
||
10 years ago
|
['!~', '~'].map(&:to_operator)
|
||
15 years ago
|
end
|
||
|
|
||
|
def self.time_operators
|
||
10 years ago
|
# ["t-", "t+", ">t-", "<t-", ">t+", "<t+"].map { |s| s.to_operator}
|
||
|
['t', 'w', '<>d', '>d', '<d', '=d', '>=d'].map(&:to_operator)
|
||
15 years ago
|
end
|
||
|
|
||
|
def self.default_operators
|
||
10 years ago
|
['=', '!'].map(&:to_operator)
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
attr_reader :name
|
||
|
|
||
|
def initialize(name, values = {}, &block)
|
||
|
@name = name.to_s
|
||
14 years ago
|
validation_methods = values.delete(:validate)
|
||
14 years ago
|
register_validations(validation_methods) unless validation_methods.nil?
|
||
15 years ago
|
values.each do |key, value|
|
||
14 years ago
|
singleton_class.class_eval { define_method(key) { value } }
|
||
15 years ago
|
end
|
||
14 years ago
|
singleton_class.class_eval(&block) if block
|
||
15 years ago
|
end
|
||
|
|
||
|
def to_operator
|
||
|
self
|
||
|
end
|
||
|
|
||
15 years ago
|
def to_s
|
||
|
name
|
||
|
end
|
||
|
|
||
15 years ago
|
def arity
|
||
|
@arity ||= begin
|
||
|
num = method(:modify).arity
|
||
15 years ago
|
# modify takes two more arguments before the values
|
||
15 years ago
|
num < 0 ? num + 2 : num - 2
|
||
|
end
|
||
|
end
|
||
|
|
||
15 years ago
|
def inspect
|
||
|
"#<#{self.class.name}:#{name.inspect}>"
|
||
|
end
|
||
|
|
||
|
def <=>(other)
|
||
10 years ago
|
name <=> other.name
|
||
15 years ago
|
end
|
||
|
|
||
14 years ago
|
## Creates an alias for a given operator.
|
||
|
def aka(alt_name, alt_label)
|
||
|
all = self.class.all
|
||
|
alt = alt_name.to_s
|
||
|
raise ArgumentError, "Can't alias operator with an existing one's name ( #{alt} )." if all.has_key?(alt)
|
||
|
op = all[name].clone
|
||
|
op.send(:rename_to, alt_name)
|
||
|
op.singleton_class.send(:define_method, 'label') { alt_label }
|
||
|
all[alt] = op
|
||
|
end
|
||
|
|
||
15 years ago
|
module DateRange
|
||
|
def modify(query, field, from, to)
|
||
14 years ago
|
query.where ["#{field} > '%s'", quoted_date((Date.yesterday + from).to_time.end_of_day)] if from
|
||
|
query.where ["#{field} <= '%s'", quoted_date((Date.today + to).to_time.end_of_day)] if to
|
||
15 years ago
|
query
|
||
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
private
|
||
|
|
||
|
def rename_to(new_name)
|
||
|
@name = new_name
|
||
|
end
|
||
|
|
||
15 years ago
|
# Done with class method definition, let's initialize the operators
|
||
|
load
|
||
14 years ago
|
end
|