Project

General

Profile

Defect #8371 » short_filters_regexps.patch

Etienne Massip, 2011-05-20 15:23

View differences:

app/models/query.rb (working copy)
93 93
  validates_presence_of :name, :on => :save
94 94
  validates_length_of :name, :maximum => 255
95 95

  
96
  @@operators = { "="   => :label_equals,
97
                  "!"   => :label_not_equals,
98
                  "o"   => :label_open_issues,
99
                  "c"   => :label_closed_issues,
100
                  "!*"  => :label_none,
101
                  "*"   => :label_all,
102
                  ">="  => :label_greater_or_equal,
103
                  "<="  => :label_less_or_equal,
104
                  "<t+" => :label_in_less_than,
105
                  ">t+" => :label_in_more_than,
106
                  "t+"  => :label_in,
107
                  "t"   => :label_today,
108
                  "w"   => :label_this_week,
109
                  ">t-" => :label_less_than_ago,
110
                  "<t-" => :label_more_than_ago,
111
                  "t-"  => :label_ago,
112
                  "~"   => :label_contains,
113
                  "!~"  => :label_not_contains }
96
  @@operators = { "="   => { :label => :label_equals, :operands_count => 1 },
97
                  "!"   => { :label => :label_not_equals, :operands_count => 1 },
98
                  "o"   => { :label => :label_open_issues, :operands_count => 0 },
99
                  "c"   => { :label => :label_closed_issues, :operands_count => 0 },
100
                  "!*"  => { :label => :label_none, :operands_count => 0 },
101
                  "*"   => { :label => :label_all, :operands_count => 0 },
102
                  ">="  => { :label => :label_greater_or_equal, :operands_count => 1 },
103
                  "<="  => { :label => :label_less_or_equal, :operands_count => 1 },
104
                  "<t+" => { :label => :label_in_less_than, :operands_count => 1 },
105
                  ">t+" => { :label => :label_in_more_than, :operands_count => 1 },
106
                  "t+"  => { :label => :label_in, :operands_count => 2 },
107
                  "t"   => { :label => :label_today, :operands_count => 0 },
108
                  "w"   => { :label => :label_this_week, :operands_count => 0 },
109
                  ">t-" => { :label => :label_less_than_ago, :operands_count => 1 },
110
                  "<t-" => { :label => :label_more_than_ago, :operands_count => 1 },
111
                  "t-"  => { :label => :label_ago, :operands_count => 1 },
112
                  "~"   => { :label => :label_contains, :operands_count => 1 },
113
                  "!~"  => { :label => :label_not_contains, :operands_count => 1} }
114 114

  
115 115
  cattr_reader :operators
116 116

  
......
266 266
  end
267 267

  
268 268
  def add_short_filter(field, expression)
269
    return unless expression
270
    parms = expression.scan(/^(o|c|!\*|!|\*)?(.*)$/).first
271
    add_filter field, (parms[0] || "="), [parms[1] || ""]
269
    return unless expression && available_filters.has_key?(field)
270
    m = nil
271
    short_filter_res(field).detect { |re| m = re.match(expression) }
272
    if m
273
      add_filter field, m[1], (m.size > 2 ? m[2..-1] : [''])
274
    else
275
      add_filter field, '=', [expression]
276
    end
277
  end
278

  
279
  def short_filter_res(field)
280
    @fields_short_filter_res = {} if @fields_short_filter_res.nil?
281
    @fields_short_filter_res_by_type = {} if @fields_short_filter_res_by_type.nil?
282

  
283
    @fields_short_filter_res[field] ||= begin
284

  
285
      field_type = available_filters[field][:type]
286
      @fields_short_filter_res_by_type[field_type] ||= @@operators_by_filter_type[field_type].collect do |operator|
287
        /^#{'(' + Regexp.escape(operator) + ')' + (['(.+)'] * @@operators[operator][:operands_count]).join(',')}$/i
288
      end
289
    end
272 290
  end
273 291

  
274 292
  # Add multiple filters using +add_filter+
app/helpers/queries_helper.rb (working copy)
18 18
module QueriesHelper
19 19
  
20 20
  def operators_for_select(filter_type)
21
    Query.operators_by_filter_type[filter_type].collect {|o| [l(Query.operators[o]), o]}
21
    Query.operators_by_filter_type[filter_type].collect {|o| [l(Query.operators[o][:label]), o]}
22 22
  end
23 23
  
24 24
  def column_header(column)
(2-2/3)