username_suggester 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -19,18 +19,20 @@ module UsernameSuggester
|
|
|
19
19
|
# <tt>:last_name_attribute</tt>:: The attribute which stores the last name. Default is <tt>:last_name</tt>
|
|
20
20
|
# <tt>:num_suggestions</tt>:: Maximum suggestions generated. Default is <tt>10</tt>
|
|
21
21
|
# <tt>:validate</tt>: An Proc object which takes in an username and return true if this is an validate username
|
|
22
|
+
# <tt>:exclusion</tt>: An array of strings that should not be suggested
|
|
22
23
|
#
|
|
23
24
|
def suggestions_for(attribute = :username, options = {})
|
|
24
25
|
first_name_attribute = options[:first_name_attribute] || :first_name
|
|
25
26
|
last_name_attribute = options[:last_name_attribute] || :last_name
|
|
26
27
|
num_suggestions = options[:num_suggestions] || 10
|
|
28
|
+
exclusion = options[:exclusion] || []
|
|
27
29
|
|
|
28
30
|
send :define_method, "#{attribute}_suggestions".to_sym do
|
|
29
31
|
suggester = Suggester.new(send(first_name_attribute), send(last_name_attribute), options)
|
|
30
32
|
name_combinations_with_regex = suggester.name_combinations.map { |s| "^#{s}[0-9]*$" }
|
|
31
33
|
sql_conditions = Array.new(name_combinations_with_regex.size, "#{attribute} RLIKE ?").join(" OR ")
|
|
32
|
-
unavailable_choices = self.class.all(:select => attribute,
|
|
33
|
-
:conditions => [sql_conditions].concat(name_combinations_with_regex)).map{ |c| c.send(attribute) }
|
|
34
|
+
unavailable_choices = exclusion.concat(self.class.all(:select => attribute,
|
|
35
|
+
:conditions => [sql_conditions].concat(name_combinations_with_regex)).map{ |c| c.send(attribute) })
|
|
34
36
|
suggester.suggest(num_suggestions, unavailable_choices)
|
|
35
37
|
end
|
|
36
38
|
end
|
|
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
|
2
2
|
|
|
3
3
|
class User < ActiveRecord::Base
|
|
4
4
|
attr_accessible :first_name, :last_name, :username
|
|
5
|
-
suggestions_for :username
|
|
5
|
+
suggestions_for :username, :exclusion => ['luk']
|
|
6
6
|
end
|
|
7
7
|
|
|
8
8
|
describe UsernameSuggester::UsernameSuggestions do
|
|
@@ -28,4 +28,13 @@ describe UsernameSuggester::UsernameSuggestions do
|
|
|
28
28
|
suggestions.should_not include "jerry"
|
|
29
29
|
suggestions.should include "jerry11"
|
|
30
30
|
end
|
|
31
|
+
|
|
32
|
+
it "should not suggest usernames in the exclusion list" do
|
|
33
|
+
UsernameSuggester::Suggester.send(:define_method, :rand) { 1 }
|
|
34
|
+
|
|
35
|
+
suggestions = @user.username_suggestions
|
|
36
|
+
suggestions.should_not be_blank
|
|
37
|
+
suggestions.should_not include "luk"
|
|
38
|
+
end
|
|
39
|
+
|
|
31
40
|
end
|
data/username_suggester.gemspec
CHANGED
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: username_suggester
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 19
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 2
|
|
9
|
-
-
|
|
10
|
-
version: 0.2.
|
|
9
|
+
- 2
|
|
10
|
+
version: 0.2.2
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- jerryluk
|