acts_as_filterable 0.2.0 → 0.3.0
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.
- data/.gitignore +3 -0
- data/README.rdoc +40 -10
- data/Rakefile +2 -5
- data/VERSION.yml +2 -1
- data/acts_as_filterable.gemspec +10 -11
- data/lib/acts_as_filterable.rb +5 -2
- data/lib/acts_as_filterable/base.rb +48 -16
- data/setup.rb +1585 -0
- data/test/acts_as_filterable_integration_test.rb +72 -98
- data/test/filter_test.rb +95 -123
- data/test/test_helper.rb +4 -7
- metadata +38 -28
|
@@ -1,106 +1,80 @@
|
|
|
1
1
|
require "test_helper"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
cd.phone_number = "2223334444"
|
|
10
|
-
cd.discount = 0.25
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
should "add an #apply_filters instance method" do
|
|
15
|
-
@model.send(:apply_filters).nil?.should_not be(true)
|
|
3
|
+
describe ActsAsFilterable::ActiveRecordExt::Base do
|
|
4
|
+
before do
|
|
5
|
+
@model = ContactDetail.new do |cd|
|
|
6
|
+
cd.name = "joe smith"
|
|
7
|
+
cd.phone_number = "2223334444"
|
|
8
|
+
cd.discount = 0.25
|
|
16
9
|
end
|
|
10
|
+
end
|
|
17
11
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
should "add a macro to filter non-numeric values from string fields" do
|
|
23
|
-
ContactDetail.respond_to?(:filter_for_digits).should be(true)
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
should "add a macro to filter values to lowercase from string fields" do
|
|
27
|
-
ContactDetail.respond_to?(:filter_for_lowercase).should be(true)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
should "be savable with valid data" do
|
|
31
|
-
@model.save.should be(true)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
context "with formatted phone number data" do
|
|
35
|
-
setup do
|
|
36
|
-
@model.phone_number = "(222) 333-4444"
|
|
37
|
-
@model.valid?
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
should "strip all formatting" do
|
|
41
|
-
@model.phone_number.should be("2223334444")
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
should "return a coercable numeric value" do
|
|
45
|
-
@model.phone_number.to_f.should be(2223334444)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
context "with a nil attribute value" do
|
|
51
|
-
setup do
|
|
52
|
-
@model.phone_number = nil
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
should "not raise any errors due to a nil attribute value" do
|
|
56
|
-
lambda { @model.valid? }.should_not raise_error
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
should "not attempt to change the attribute value" do
|
|
60
|
-
@model.valid?
|
|
61
|
-
@model.phone_number.nil?.should be(true)
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
context "with non-string attributes" do
|
|
66
|
-
setup do
|
|
67
|
-
ContactDetail.filter_for_digits :discount
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
should "not raise any errors due to a non-string attribute value" do
|
|
71
|
-
lambda { @model.valid? }.should_not raise_error
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
should "not attempt to change the attribute value" do
|
|
75
|
-
@model.valid?
|
|
76
|
-
@model.discount.should be(0.25)
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
context "with an attribute value that contains no values to be stripped" do
|
|
82
|
-
setup do
|
|
83
|
-
@model.phone_number = "2223334444"
|
|
84
|
-
@model.valid?
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
should "not change the attribute value" do
|
|
88
|
-
@model.phone_number.should be("2223334444")
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
context "that has filtered attribute names that are identical to another filtered model" do
|
|
93
|
-
|
|
94
|
-
should "hold seperate collections of filtered_attributes" do
|
|
95
|
-
User.filtered_attributes.should_not === ContactDetail.filtered_attributes
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
should "not add attributes to other models errantly" do
|
|
99
|
-
ContactDetail.filtered_attributes[:digits].should_not include(:handle)
|
|
100
|
-
end
|
|
12
|
+
it "should add an #apply_filters instance method" do
|
|
13
|
+
@model.send(:apply_filters).wont_equal(nil)
|
|
14
|
+
end
|
|
101
15
|
|
|
102
|
-
|
|
103
|
-
|
|
16
|
+
it "should know about the types of filters that will be applied to the attributes" do
|
|
17
|
+
ContactDetail.must_respond_to(:filtered_attributes)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should add a macro to filter non-numeric values from string fields" do
|
|
21
|
+
ContactDetail.must_respond_to(:filter_for_digits)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "should add a macro to filter values to lowercase from string fields" do
|
|
25
|
+
ContactDetail.must_respond_to(:filter_for_lowercase)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "should be savable with valid data" do
|
|
29
|
+
@model.save.must_equal(true)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "should strip all formatting" do
|
|
33
|
+
@model.phone_number = "(222) 333-4444"
|
|
34
|
+
@model.valid?
|
|
35
|
+
@model.phone_number.must_equal("2223334444")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "should return a coercable numeric value" do
|
|
39
|
+
@model.phone_number = "(222) 333-4444"
|
|
40
|
+
@model.valid?
|
|
41
|
+
@model.phone_number.to_i.must_equal(2223334444)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "should not raise any errors due to a nil attribute value" do
|
|
45
|
+
@model.phone_number = nil
|
|
46
|
+
@model.valid?.must_equal(true)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "should not attempt to change the attribute value" do
|
|
50
|
+
@model.phone_number = nil
|
|
51
|
+
@model.valid?
|
|
52
|
+
@model.phone_number.must_equal(nil)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "should not raise formatting errors" do
|
|
56
|
+
ContactDetail.filter_for_digits :discount
|
|
57
|
+
@model.valid?.must_equal(true)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "should not attempt to change the attribute value" do
|
|
61
|
+
ContactDetail.filter_for_digits :discount
|
|
62
|
+
@model.valid?
|
|
63
|
+
@model.discount.must_equal(0.25)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "should not change the attribute value" do
|
|
67
|
+
@model.phone_number = "2223334444"
|
|
68
|
+
@model.valid?
|
|
69
|
+
@model.phone_number.must_equal("2223334444")
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "should hold seperate collections of filtered_attributes" do
|
|
73
|
+
User.filtered_attributes.wont_be_same_as ContactDetail.filtered_attributes
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "should not add attributes to other models errantly" do
|
|
77
|
+
ContactDetail.filtered_attributes[:digits].flatten.wont_include([:handle].flatten)
|
|
104
78
|
end
|
|
105
79
|
|
|
106
80
|
end
|
data/test/filter_test.rb
CHANGED
|
@@ -1,129 +1,101 @@
|
|
|
1
1
|
require "test_helper"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
should "not create extra string objects when no values are to be replaced" do
|
|
97
|
-
assert_identity_after_filter @filter, "43"
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
context "whitesapce filter, it" do
|
|
102
|
-
setup do
|
|
103
|
-
@filter = ActsAsFilterable::Filters[:whitespace]
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
should "replace all un-neccessary whitespace" do
|
|
107
|
-
value = "\t hai! this is neat\n\nok? \t"
|
|
108
|
-
@filter.call(value)
|
|
109
|
-
value.should be("hai! this is neat ok?")
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
should "trim the ends of the string" do
|
|
113
|
-
value = " this "
|
|
114
|
-
@filter.call(value)
|
|
115
|
-
value.should be("this")
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
should "not create extra string objects when replacing values" do
|
|
119
|
-
assert_identity_after_filter @filter, "TRANSLATE"
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
should "not create extra string objects when no values are to be replaced" do
|
|
123
|
-
assert_identity_after_filter @filter, "43"
|
|
124
|
-
end
|
|
125
|
-
end
|
|
3
|
+
def assert_identity_after_filter(filter, value)
|
|
4
|
+
identity = value.object_id
|
|
5
|
+
filter.call(value)
|
|
6
|
+
identity.must_equal value.object_id
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
describe ActsAsFilterable::Filters do
|
|
10
|
+
before do
|
|
11
|
+
@filter = ActsAsFilterable::Filters[:digits]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "should default filters that don't exist to an empty array" do
|
|
15
|
+
ActsAsFilterable::Filters[:test].empty?.must_equal(true)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "should contain some filters initially" do
|
|
19
|
+
ActsAsFilterable::Filters[:numeric].nil?.wont_equal(true)
|
|
20
|
+
ActsAsFilterable::Filters[:lowercase].nil?.wont_equal(true)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "should freeze the macro collection so it cannot be mutated" do
|
|
24
|
+
proc { ActsAsFilterable::Filters.store(:test, /./) }.must_raise TypeError, RuntimeError
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "should strip any non-digit values from the string" do
|
|
28
|
+
value = "45tr.,2"
|
|
29
|
+
ActsAsFilterable::Filters[:digits].call(value)
|
|
30
|
+
value.must_equal("452")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "should not lose digit values" do
|
|
34
|
+
value = "432099132"
|
|
35
|
+
ActsAsFilterable::Filters[:digits].call(value)
|
|
36
|
+
value.must_equal("432099132")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "should return a coercable numerica value" do
|
|
40
|
+
value = "4"
|
|
41
|
+
ActsAsFilterable::Filters[:digits].call(value)
|
|
42
|
+
value.to_i.must_equal(4)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "should not create extra string objects when replacing values" do
|
|
46
|
+
assert_identity_after_filter ActsAsFilterable::Filters[:digits], "54tr"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "should not create extra string objects when no values are to be replaced" do
|
|
50
|
+
assert_identity_after_filter ActsAsFilterable::Filters[:digits], "54"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "should lowercase all alpha values" do
|
|
54
|
+
value = "FAIl STRING"
|
|
55
|
+
ActsAsFilterable::Filters[:lowercase].call(value)
|
|
56
|
+
value.must_equal("fail string")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "should not create extra string objects when replacing values" do
|
|
60
|
+
assert_identity_after_filter ActsAsFilterable::Filters[:lowercase], "TRANSLATE"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "should not create extra string objects when no values are to be replaced" do
|
|
64
|
+
assert_identity_after_filter ActsAsFilterable::Filters[:lowercase], "43"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "should uppercase all alpha values" do
|
|
68
|
+
value = "lowercase string"
|
|
69
|
+
ActsAsFilterable::Filters[:uppercase].call(value)
|
|
70
|
+
value.must_equal("LOWERCASE STRING")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it "should not create extra string objects when replacing values" do
|
|
74
|
+
assert_identity_after_filter ActsAsFilterable::Filters[:uppercase], "translate"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "should not create extra string objects when no values are to be replaced" do
|
|
78
|
+
assert_identity_after_filter ActsAsFilterable::Filters[:uppercase], "43"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "should replace all un-neccessary whitespace" do
|
|
82
|
+
value = "\t hai! this is neat\n\nok? \t"
|
|
83
|
+
ActsAsFilterable::Filters[:whitespace].call(value)
|
|
84
|
+
value.must_equal("hai! this is neat ok?")
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "should trim the ends of the string" do
|
|
88
|
+
value = " this "
|
|
89
|
+
ActsAsFilterable::Filters[:whitespace].call(value)
|
|
90
|
+
value.must_equal("this")
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it "should not create extra string objects when replacing values" do
|
|
94
|
+
assert_identity_after_filter ActsAsFilterable::Filters[:whitespace], "TRANSLATE"
|
|
95
|
+
end
|
|
126
96
|
|
|
97
|
+
it "should not create extra string objects when no values are to be replaced" do
|
|
98
|
+
assert_identity_after_filter ActsAsFilterable::Filters[:whitespace], "43"
|
|
127
99
|
end
|
|
128
100
|
|
|
129
101
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
require "
|
|
2
|
-
require "
|
|
3
|
-
require "shoulda"
|
|
4
|
-
require "matchy"
|
|
1
|
+
require "minitest/spec"
|
|
2
|
+
require "active_record"
|
|
5
3
|
|
|
6
4
|
require "acts_as_filterable"
|
|
7
5
|
|
|
@@ -15,7 +13,7 @@ ActiveRecord::Schema.define do
|
|
|
15
13
|
t.string :fax_number
|
|
16
14
|
t.float :discount
|
|
17
15
|
end
|
|
18
|
-
|
|
16
|
+
|
|
19
17
|
create_table :users, :force => true do |t|
|
|
20
18
|
t.string :handle
|
|
21
19
|
t.string :phone_number
|
|
@@ -31,5 +29,4 @@ class User < ActiveRecord::Base
|
|
|
31
29
|
filter_for_lowercase :handle
|
|
32
30
|
end
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
end
|
|
32
|
+
MiniTest::Unit.autorun
|
metadata
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: acts_as_filterable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 0
|
|
7
|
+
- 3
|
|
8
|
+
- 0
|
|
9
|
+
version: 0.3.0
|
|
5
10
|
platform: ruby
|
|
6
11
|
authors:
|
|
7
12
|
- Rob Ares
|
|
@@ -9,49 +14,51 @@ autorequire:
|
|
|
9
14
|
bindir: bin
|
|
10
15
|
cert_chain: []
|
|
11
16
|
|
|
12
|
-
date:
|
|
17
|
+
date: 2010-03-07 00:00:00 -05:00
|
|
13
18
|
default_executable:
|
|
14
19
|
dependencies:
|
|
15
20
|
- !ruby/object:Gem::Dependency
|
|
16
21
|
name: activerecord
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
prerelease: false
|
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
20
24
|
requirements:
|
|
21
25
|
- - ">="
|
|
22
26
|
- !ruby/object:Gem::Version
|
|
27
|
+
segments:
|
|
28
|
+
- 1
|
|
29
|
+
- 15
|
|
30
|
+
- 0
|
|
23
31
|
version: 1.15.0
|
|
24
|
-
|
|
32
|
+
type: :runtime
|
|
33
|
+
version_requirements: *id001
|
|
25
34
|
- !ruby/object:Gem::Dependency
|
|
26
35
|
name: activesupport
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
prerelease: false
|
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
30
38
|
requirements:
|
|
31
39
|
- - ">="
|
|
32
40
|
- !ruby/object:Gem::Version
|
|
41
|
+
segments:
|
|
42
|
+
- 1
|
|
43
|
+
- 4
|
|
44
|
+
- 4
|
|
33
45
|
version: 1.4.4
|
|
34
|
-
|
|
46
|
+
type: :runtime
|
|
47
|
+
version_requirements: *id002
|
|
35
48
|
- !ruby/object:Gem::Dependency
|
|
36
|
-
name:
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
49
|
+
name: minitest
|
|
50
|
+
prerelease: false
|
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
|
40
52
|
requirements:
|
|
41
53
|
- - ">="
|
|
42
54
|
- !ruby/object:Gem::Version
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
-
|
|
46
|
-
|
|
55
|
+
segments:
|
|
56
|
+
- 1
|
|
57
|
+
- 5
|
|
58
|
+
- 0
|
|
59
|
+
version: 1.5.0
|
|
47
60
|
type: :development
|
|
48
|
-
|
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
-
requirements:
|
|
51
|
-
- - ">="
|
|
52
|
-
- !ruby/object:Gem::Version
|
|
53
|
-
version: "0"
|
|
54
|
-
version:
|
|
61
|
+
version_requirements: *id003
|
|
55
62
|
description:
|
|
56
63
|
email: rob.ares@gmail.com
|
|
57
64
|
executables: []
|
|
@@ -73,6 +80,7 @@ files:
|
|
|
73
80
|
- lib/acts_as_filterable.rb
|
|
74
81
|
- lib/acts_as_filterable/base.rb
|
|
75
82
|
- rails/init.rb
|
|
83
|
+
- setup.rb
|
|
76
84
|
- test/acts_as_filterable_integration_test.rb
|
|
77
85
|
- test/filter_test.rb
|
|
78
86
|
- test/test_helper.rb
|
|
@@ -89,18 +97,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
89
97
|
requirements:
|
|
90
98
|
- - ">="
|
|
91
99
|
- !ruby/object:Gem::Version
|
|
100
|
+
segments:
|
|
101
|
+
- 0
|
|
92
102
|
version: "0"
|
|
93
|
-
version:
|
|
94
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
104
|
requirements:
|
|
96
105
|
- - ">="
|
|
97
106
|
- !ruby/object:Gem::Version
|
|
107
|
+
segments:
|
|
108
|
+
- 0
|
|
98
109
|
version: "0"
|
|
99
|
-
version:
|
|
100
110
|
requirements: []
|
|
101
111
|
|
|
102
112
|
rubyforge_project:
|
|
103
|
-
rubygems_version: 1.3.
|
|
113
|
+
rubygems_version: 1.3.6
|
|
104
114
|
signing_key:
|
|
105
115
|
specification_version: 3
|
|
106
116
|
summary: Filter attributes and stuff.
|