has_handle_fallback 0.0.13 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/has_handle_fallback.rb +24 -8
- data/test/test_has_handle_fallback.rb +37 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/lib/has_handle_fallback.rb
CHANGED
@@ -24,16 +24,21 @@ module HasHandleFallback
|
|
24
24
|
end
|
25
25
|
|
26
26
|
module ActiveRecordBaseMethods
|
27
|
-
def has_handle_fallback(
|
27
|
+
def has_handle_fallback(*args)
|
28
28
|
include InstanceMethods
|
29
29
|
extend ClassMethods
|
30
|
+
|
31
|
+
fallback_column = args.first.respond_to?(:[]) ? nil : args.shift.to_s
|
32
|
+
options = args.shift || {}
|
30
33
|
|
31
34
|
class_eval do
|
32
35
|
cattr_accessor :has_handle_fallback_options
|
33
36
|
self.has_handle_fallback_options = {}
|
34
37
|
has_handle_fallback_options[:required] = options.delete(:required) || false
|
35
|
-
has_handle_fallback_options[:fallback_column] = fallback_column
|
38
|
+
has_handle_fallback_options[:fallback_column] = fallback_column
|
36
39
|
has_handle_fallback_options[:handle_column] = options.delete(:handle_column) || 'handle'
|
40
|
+
has_handle_fallback_options[:validates_format] =
|
41
|
+
options.include?(:validates_format) ? options.delete(:validates_format) : true
|
37
42
|
|
38
43
|
validate :handle_is_valid
|
39
44
|
end
|
@@ -54,12 +59,21 @@ module HasHandleFallback
|
|
54
59
|
end
|
55
60
|
|
56
61
|
module InstanceMethods
|
62
|
+
def fallback_handle_is_valid?
|
63
|
+
fallback_column = self.class.has_handle_fallback_options[:fallback_column]
|
64
|
+
# only check if we need to actually generate a fallback
|
65
|
+
if handle.blank? and fallback_column.present?
|
66
|
+
handle_fallback =~ HasHandleFallback::REGEXP and HasHandleFallback::LENGTH_RANGE.include?(handle_fallback.length)
|
67
|
+
else
|
68
|
+
true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
57
72
|
def handle_is_valid
|
58
73
|
raw = read_attribute self.class.has_handle_fallback_options[:handle_column]
|
59
|
-
cooked = handle_fallback
|
60
74
|
|
61
75
|
# inline check to make sure the handle_fallback method works
|
62
|
-
unless
|
76
|
+
unless fallback_handle_is_valid?
|
63
77
|
raise "Dear Developer: your handle_fallback method is not generating valid handles (generated '#{handle_fallback}' for '#{raw}')"
|
64
78
|
end
|
65
79
|
|
@@ -77,7 +91,7 @@ module HasHandleFallback
|
|
77
91
|
end
|
78
92
|
|
79
93
|
# validates_format_of :handle, :with => HasHandleFallback::REGEXP, :allow_nil => true
|
80
|
-
|
94
|
+
if has_handle_fallback_options[:validates_format] and raw !~ HasHandleFallback::REGEXP
|
81
95
|
errors.add self.class.has_handle_fallback_options[:handle_column], "contains invalid characters"
|
82
96
|
end
|
83
97
|
|
@@ -106,9 +120,11 @@ module HasHandleFallback
|
|
106
120
|
end
|
107
121
|
|
108
122
|
def handle_fallback
|
109
|
-
|
110
|
-
fallback =
|
111
|
-
|
123
|
+
column = self.class.has_handle_fallback_options[:fallback_column]
|
124
|
+
if column and fallback = read_attribute(column)
|
125
|
+
fallback = fallback.split('@').first if fallback.to_s.include? '@'
|
126
|
+
HasHandleFallback.str2handle fallback
|
127
|
+
end
|
112
128
|
end
|
113
129
|
|
114
130
|
def to_param
|
@@ -10,6 +10,16 @@ ActiveRecord::Schema.define(:version => 20090819143429) do
|
|
10
10
|
t.string :name
|
11
11
|
t.string :moniker
|
12
12
|
end
|
13
|
+
|
14
|
+
create_table 'dogs', :force => true do |t|
|
15
|
+
t.string :email
|
16
|
+
t.string :handle
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table 'ferrets', :force => true do |t|
|
20
|
+
t.string :email
|
21
|
+
t.string :handle
|
22
|
+
end
|
13
23
|
end
|
14
24
|
|
15
25
|
class Person < ActiveRecord::Base
|
@@ -20,6 +30,15 @@ class Cat < ActiveRecord::Base
|
|
20
30
|
has_handle_fallback :name, :handle_column => 'moniker', :required => true
|
21
31
|
end
|
22
32
|
|
33
|
+
class Dog < ActiveRecord::Base
|
34
|
+
has_handle_fallback :email, :validates_format => false
|
35
|
+
validates_format_of :handle, :with => /Astro/
|
36
|
+
end
|
37
|
+
|
38
|
+
class Ferret < ActiveRecord::Base
|
39
|
+
has_handle_fallback :validates_format => false
|
40
|
+
end
|
41
|
+
|
23
42
|
class TestHasHandleFallback < Test::Unit::TestCase
|
24
43
|
def setup
|
25
44
|
Person.delete_all
|
@@ -45,6 +64,11 @@ class TestHasHandleFallback < Test::Unit::TestCase
|
|
45
64
|
pierre = Cat.new :name => 'Pierre Bourdieu'
|
46
65
|
assert_equal false, pierre.valid?
|
47
66
|
end
|
67
|
+
|
68
|
+
def test_nil_handle_and_email
|
69
|
+
abe = Person.new :email => nil, :handle => nil
|
70
|
+
assert_nil abe.handle
|
71
|
+
end
|
48
72
|
|
49
73
|
def test_has_validations
|
50
74
|
assert_equal true, Person.new(:email => 'pierre.bourdieu@example.com', :handle => 'Pierre-Bourdieu_99').valid?
|
@@ -110,4 +134,17 @@ class TestHasHandleFallback < Test::Unit::TestCase
|
|
110
134
|
a = Person.create!(:email => 'pierre.bourdieu@example.com', :handle => handle.upcase)
|
111
135
|
assert_equal a, Person[handle.downcase]
|
112
136
|
end
|
137
|
+
|
138
|
+
def test_disables_validation_with_option
|
139
|
+
d = Dog.new :handle => 'Astro Jetson'
|
140
|
+
assert d.valid?
|
141
|
+
|
142
|
+
d = Dog.new :handle => 'Scooby Doo'
|
143
|
+
assert !d.valid?
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_no_fallback_column
|
147
|
+
friedrich = Ferret.new :handle => nil, :email => 'friedrich@example.com'
|
148
|
+
assert_nil friedrich.handle
|
149
|
+
end
|
113
150
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_handle_fallback
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.13
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Seamus Abshere
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-06-
|
18
|
+
date: 2010-06-29 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|