0001-Support-multi-word-last-names-in-Principal-like-and-.patch

Gregor Schmidt, 2018-02-08 10:36

Download (3 KB)

View differences:

app/models/principal.rb
69 69
      where({})
70 70
    else
71 71
      pattern = "%#{q}%"
72
      sql = %w(login firstname lastname).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ")
72
      sql = "LOWER(#{table_name}.login) LIKE LOWER(:p)"
73 73
      sql << " OR #{table_name}.id IN (SELECT user_id FROM #{EmailAddress.table_name} WHERE LOWER(address) LIKE LOWER(:p))"
74 74
      params = {:p => pattern}
75
      if q =~ /^(.+)\s+(.+)$/
76
        a, b = "#{$1}%", "#{$2}%"
77
        sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))"
78
        sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))"
79
        params.merge!(:a => a, :b => b)
75

  
76
      tokens = q.split(/\s+/).reject(&:blank?).map { |token| "%#{token}%" }
77
      if tokens.present?
78
        sql << ' OR ('
79
        sql << tokens.map.with_index do |token, index|
80
          params.merge!(:"token_#{index}" => token)
81
          "(LOWER(#{table_name}.firstname) LIKE LOWER(:token_#{index}) OR LOWER(#{table_name}.lastname) LIKE LOWER(:token_#{index}))"
82
        end.join(' AND ')
83
        sql << ')'
80 84
      end
85

  
81 86
      where(sql, params)
82 87
    end
83 88
  }
test/unit/principal_test.rb
127 127
    assert_equal User.find(2), results.first
128 128
  end
129 129

  
130
  test "like scope should find lastname with spaces" do
131
    user = User.find(1)
132
    user.update_columns(:firstname => 'Leonardo', :lastname => 'da Vinci')
133

  
134
    results = Principal.like('Leonardo da Vinci')
135

  
136
    assert_equal 1, results.count
137
    assert_equal user, results.first
138
  end
139

  
130 140
  def test_like_scope_with_cyrillic_name
131 141
    user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис')
132 142
    results = Principal.like('Собо')
133
-