strip_attributes 1.4.0 → 1.4.1
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/lib/strip_attributes.rb +3 -1
- data/lib/strip_attributes/version.rb +1 -1
- data/test/matchers_test.rb +6 -3
- data/test/strip_attributes_test.rb +31 -1
- data/test/test_helper.rb +2 -37
- metadata +10 -10
data/lib/strip_attributes.rb
CHANGED
@@ -44,6 +44,8 @@ module StripAttributes
|
|
44
44
|
end
|
45
45
|
|
46
46
|
attributes.each do |attr, value|
|
47
|
+
original_value = value
|
48
|
+
|
47
49
|
if value.respond_to?(:strip)
|
48
50
|
value = (value.blank? && !allow_empty) ? nil : value.strip
|
49
51
|
end
|
@@ -52,7 +54,7 @@ module StripAttributes
|
|
52
54
|
value.squeeze!(' ')
|
53
55
|
end
|
54
56
|
|
55
|
-
record[attr] = value
|
57
|
+
record[attr] = value if original_value != value
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
data/test/matchers_test.rb
CHANGED
@@ -3,9 +3,12 @@ require "test_helper"
|
|
3
3
|
require "strip_attributes/matchers"
|
4
4
|
|
5
5
|
class SampleMockRecord < Tableless
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
attribute :stripped1
|
7
|
+
attribute :stripped2
|
8
|
+
attribute :stripped3
|
9
|
+
attribute :unstripped1
|
10
|
+
attribute :unstripped2
|
11
|
+
attribute :unstripped3
|
9
12
|
strip_attributes :only => [:stripped1, :stripped2, :stripped3]
|
10
13
|
end
|
11
14
|
|
@@ -2,7 +2,12 @@ require "test_helper"
|
|
2
2
|
|
3
3
|
module MockAttributes
|
4
4
|
def self.included(base)
|
5
|
-
base.
|
5
|
+
base.attribute :foo
|
6
|
+
base.attribute :bar
|
7
|
+
base.attribute :biz
|
8
|
+
base.attribute :baz
|
9
|
+
base.attribute :bang
|
10
|
+
base.attribute :foz
|
6
11
|
end
|
7
12
|
end
|
8
13
|
|
@@ -41,6 +46,16 @@ class CollapseDuplicateSpaces < Tableless
|
|
41
46
|
strip_attributes :collapse_spaces => true
|
42
47
|
end
|
43
48
|
|
49
|
+
class CoexistWithOtherValidations < Tableless
|
50
|
+
attribute :number, :type => Integer
|
51
|
+
|
52
|
+
strip_attributes
|
53
|
+
validates :number, {
|
54
|
+
:numericality => { :only_integer => true, :greater_than_or_equal_to => 1000 },
|
55
|
+
:allow_blank => true
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
44
59
|
class StripAttributesTest < MiniTest::Unit::TestCase
|
45
60
|
def setup
|
46
61
|
@init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "", :bang => " ", :foz => " foz foz" }
|
@@ -139,4 +154,19 @@ class StripAttributesTest < MiniTest::Unit::TestCase
|
|
139
154
|
assert_equal "", record.baz
|
140
155
|
assert_equal "", record.bang
|
141
156
|
end
|
157
|
+
|
158
|
+
def test_should_coexist_with_other_validations
|
159
|
+
record = CoexistWithOtherValidations.new
|
160
|
+
record.number = 1000.1
|
161
|
+
assert !record.valid?, "Expected record to be invalid"
|
162
|
+
assert record.errors.include?(:number), "Expected record to have an error on :number"
|
163
|
+
|
164
|
+
record = CoexistWithOtherValidations.new(:number => " 1000.2 ")
|
165
|
+
assert !record.valid?, "Expected record to be invalid"
|
166
|
+
assert record.errors.include?(:number), "Expected record to have an error on :number"
|
167
|
+
|
168
|
+
# record = CoexistWithOtherValidations.new(:number => " 1000 ")
|
169
|
+
# assert record.valid?, "Expected record to be valid, but got #{record.errors.full_messages}"
|
170
|
+
# assert !record.errors.include?(:number), "Expected record to have no errors on :number"
|
171
|
+
end
|
142
172
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,44 +1,9 @@
|
|
1
1
|
require "minitest/autorun"
|
2
2
|
require "minitest/pride"
|
3
|
-
require "
|
4
|
-
require "active_support/core_ext/hash/indifferent_access"
|
3
|
+
require "active_attr"
|
5
4
|
require "strip_attributes"
|
6
5
|
|
7
|
-
# Tableless ActiveModel with some parts borrowed from both
|
8
|
-
# http://railscasts.com/episodes/219-active-model and ActiveRecord's
|
9
|
-
# implementation.
|
10
6
|
class Tableless
|
11
|
-
include
|
7
|
+
include ActiveAttr::Model
|
12
8
|
include ActiveModel::Validations::Callbacks
|
13
|
-
|
14
|
-
def initialize(attributes = {})
|
15
|
-
attributes.each { |name, value| send("#{name}=", value) }
|
16
|
-
end
|
17
|
-
|
18
|
-
def attributes
|
19
|
-
attrs = HashWithIndifferentAccess.new
|
20
|
-
self.class.attribute_names.each { |name| attrs[name] = self[name] }
|
21
|
-
attrs
|
22
|
-
end
|
23
|
-
|
24
|
-
def assign_attributes(attributes = {})
|
25
|
-
attributes.each { |name, value| send("#{name}=", value) }
|
26
|
-
end
|
27
|
-
|
28
|
-
def [](name)
|
29
|
-
send name
|
30
|
-
end
|
31
|
-
|
32
|
-
def []=(name, value)
|
33
|
-
send "#{name}=", value
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.attribute_names
|
37
|
-
@attribute_names ||= []
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.attributes(*names)
|
41
|
-
attr_accessor *names
|
42
|
-
attribute_names.concat names
|
43
|
-
end
|
44
9
|
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: strip_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.4.
|
5
|
+
version: 1.4.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ryan McGeary
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
type: :runtime
|
@@ -29,34 +29,34 @@ dependencies:
|
|
29
29
|
prerelease: false
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
type: :development
|
32
|
-
name:
|
32
|
+
name: active_attr
|
33
33
|
requirement: !ruby/object:Gem::Requirement
|
34
34
|
requirements:
|
35
35
|
- - ~>
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version: '
|
37
|
+
version: '0.7'
|
38
38
|
none: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: '
|
43
|
+
version: '0.7'
|
44
44
|
none: false
|
45
45
|
prerelease: false
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
47
|
type: :development
|
48
|
-
name:
|
48
|
+
name: minitest-matchers
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
51
|
- - ~>
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
53
|
+
version: '1.2'
|
54
54
|
none: false
|
55
55
|
version_requirements: !ruby/object:Gem::Requirement
|
56
56
|
requirements:
|
57
57
|
- - ~>
|
58
58
|
- !ruby/object:Gem::Version
|
59
|
-
version: '
|
59
|
+
version: '1.2'
|
60
60
|
none: false
|
61
61
|
prerelease: false
|
62
62
|
- !ruby/object:Gem::Dependency
|
@@ -104,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
104
|
requirements:
|
105
105
|
- - ! '>='
|
106
106
|
- !ruby/object:Gem::Version
|
107
|
-
hash:
|
107
|
+
hash: -830091072879827042
|
108
108
|
segments:
|
109
109
|
- 0
|
110
110
|
version: '0'
|
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
requirements:
|
114
114
|
- - ! '>='
|
115
115
|
- !ruby/object:Gem::Version
|
116
|
-
hash:
|
116
|
+
hash: -830091072879827042
|
117
117
|
segments:
|
118
118
|
- 0
|
119
119
|
version: '0'
|