auto_strip_attributes 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -7,3 +7,5 @@ pkg/*
7
7
  .project
8
8
  .idea
9
9
  .redcar
10
+
11
+ help
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.8.7 # (current default)
3
+ - 1.9.2
4
+ gemfile:
5
+ - gemfiles/Gemfile.rails-3.1.x
6
+ - gemfiles/Gemfile.rails-3.0.x
data/README.md CHANGED
@@ -29,18 +29,34 @@ class User < ActiveRecord::Base
29
29
  end
30
30
  ```
31
31
 
32
+ # Requirements
33
+
34
+ Gem has been tested with ruby 1.8.7, 1.9.2 and Rails 3.x. Although it should also work with previous versions of rails.
35
+
36
+ http://travis-ci.org/#!/holli/auto_strip_attributes
37
+
32
38
  # Support
33
39
 
34
40
  Submit suggestions or feature requests as a GitHub Issue or Pull Request. Remember to update tests. Tests are quite extensive.
35
41
 
42
+ # Other approaches
43
+
44
+ This gem works by addin before_validation hook and setting attributes with self[attribute]=stripped_value. See: https://github.com/holli/auto_strip_attributes/blob/master/lib/auto_strip_attributes.rb
45
+
46
+ Other approaches could include calling attribute= from before_validation. This would end up calling possible custom setters twice. Might not be desired effect (e.g. if setter does some logging).
47
+
48
+ Method chaining attribute= can be also used. But then stripping would be omitted if there is some code that calls model[attribute]= directly. This could happen easily when using hashes in some places.
49
+
36
50
  ## Similar gems
37
51
 
38
52
  There are many similar gems. Most of those don't have :squish or :nullify options. Those gems
39
- might have some extra methods whereas this gem is kept as simple as possible.
53
+ might have some extra methods whereas this gem is kept as simple as possible. These gems have a bit
54
+ different approaches. See discussion in previous chapter.
40
55
 
41
56
  - https://github.com/phatworx/acts_as_strip
42
57
  - https://github.com/rmm5t/strip_attributes
43
58
  - https://github.com/thomasfedb/attr_cleaner
59
+ - https://github.com/mdeering/attribute_normalizer (Bit hardcore approach, more features and more complex)
44
60
 
45
61
  # Licence
46
62
 
@@ -19,10 +19,11 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
 
22
- s.add_runtime_dependency "activemodel", "~> 3.0"
22
+ s.add_runtime_dependency "activerecord", ">= 3.0"
23
23
 
24
- s.add_development_dependency "activerecord", "~> 3.0"
25
- s.add_development_dependency "minitest", "~> 2.5.0"
26
- s.add_development_dependency 'ruby-debug'
24
+ s.add_development_dependency "activerecord", ">= 3.0"
25
+ s.add_development_dependency "minitest", "2.6.0"
26
+ s.add_development_dependency "mocha", "~> 0.9.12"
27
+ #s.add_development_dependency 'ruby-debug'
27
28
  s.add_development_dependency 'rake'
28
29
  end
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # This is for testing with rails 3.0
4
+
5
+ gem 'rake'
6
+ gem "activerecord", "~> 3.0.0"
7
+ gem "minitest", "2.6.0"
8
+ gem "mocha", "~> 0.9.12"
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # This is for testing with rails 3.0
4
+
5
+ gem 'rake'
6
+ gem "activerecord", "~> 3.0.0"
7
+ gem "minitest", "2.6.0"
8
+ gem "mocha", "~> 0.9.12"
@@ -33,5 +33,5 @@ module AutoStripAttributes
33
33
  end
34
34
 
35
35
  ActiveRecord::Base.send(:extend, AutoStripAttributes) if defined? ActiveRecord
36
- #ActiveModel::Validations::HelperMethods.send(:include, AutoStripAttributes)
36
+ #ActiveModel::Validations::HelperMethods.send(:include, AutoStripAttributes) if defined? ActiveRecord
37
37
 
@@ -1,4 +1,4 @@
1
1
  module AutoStripAttributes
2
2
  #VERSION = "0.0.1"
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
@@ -1,8 +1,28 @@
1
- require "test/test_helper"
1
+ require 'minitest/autorun'
2
+ require "active_record"
3
+ require "auto_strip_attributes"
4
+ #require 'ruby-debug'
5
+ require 'mocha'
6
+
7
+
8
+ #require "test/test_helper"
2
9
  # bundle exec ruby test/auto_strip_attributes_test.rb -v --name /test_name/
3
10
 
4
11
  #class AutoStripAttributesTest < Test::Unit::TestCase
5
12
  #class AutoStripAttributesTest < MiniTest::Unit::TestCase
13
+
14
+ class MockRecordParent
15
+ include ActiveModel::Validations
16
+ include ActiveModel::Validations::Callbacks
17
+ extend AutoStripAttributes
18
+
19
+ # Overriding @record[key]=val , that's only found in activerecord, not in ActiveModel
20
+ def []=(key, val)
21
+ # send("#{key}=", val) # We dont want to call setter again
22
+ instance_variable_set(:"@#{key}", val)
23
+ end
24
+ end
25
+
6
26
  describe AutoStripAttributes do
7
27
 
8
28
  def setup
@@ -14,49 +34,57 @@ describe AutoStripAttributes do
14
34
  end
15
35
 
16
36
  describe "Basic attribute with default options" do
17
- class MockRecordBasic < ActiveRecord::Base
18
- column :foo, :string
37
+ class MockRecordBasic < MockRecordParent
38
+ attr_accessor :foo
19
39
  auto_strip_attributes :foo
20
40
  end
21
- before do
22
- @record = MockRecordBasic.new()
23
- end
24
41
 
25
42
  it "should be ok for normal strings" do
43
+ @record = MockRecordBasic.new()
26
44
  @record.foo = " aaa \t"
27
45
  @record.valid?
28
46
  @record.foo.must_equal "aaa"
29
47
  end
30
48
 
31
49
  it "should set empty strings to nil" do
50
+ @record = MockRecordBasic.new()
32
51
  @record.foo = " "
33
52
  @record.valid?
34
53
  @record.foo.must_be_nil
35
54
  end
36
55
 
37
56
  it "should call strip method to attribute if possible" do
38
- str_mock = MiniTest::Mock.new()
39
- str_mock.expect :'nil?', false
40
- str_mock.expect :strip, (@stripped_str="stripped_str_here")
57
+ @record = MockRecordBasic.new()
58
+ str_mock = " strippable_str "
59
+ str_mock.expects(:strip).returns(@stripped_str="stripped_str_here")
41
60
  @record.foo = str_mock
42
61
  @record.valid?
43
- str_mock.verify
62
+ assert true
44
63
  @record.foo.must_be_same_as @stripped_str
64
+
65
+ #str_mock.expect :'nil?', false
66
+ #str_mock.expect :strip, (@stripped_str="stripped_str_here")
67
+ #@record.foo = str_mock
68
+ #@record.valid?
69
+ #str_mock.verify
70
+ #@record.foo.must_be_same_as @stripped_str
45
71
  end
46
72
 
47
73
  it "should not call strip method for non strippable attributes" do
74
+ @record = MockRecordBasic.new()
48
75
  str_mock = MiniTest::Mock.new() # answers false to str_mock.respond_to?(:strip)
49
- str_mock.expect :'nil?', false
50
76
  @record.foo = str_mock
51
77
  @record.valid?
52
- str_mock.verify
53
78
  assert @record.foo === str_mock
79
+ str_mock.verify # "Should not call anything on mock when respond_to is false"
54
80
  end
55
81
  end
56
82
 
57
83
  describe "Attribute with nullify option" do
58
- class MockRecordWithNullify < ActiveRecord::Base
59
- column :foo, :string
84
+ #class MockRecordWithNullify < ActiveRecord::Base
85
+ class MockRecordWithNullify < MockRecordParent
86
+ #column :foo, :string
87
+ attr_accessor :foo
60
88
  auto_strip_attributes :foo, :nullify => false
61
89
  end
62
90
 
@@ -69,8 +97,9 @@ describe AutoStripAttributes do
69
97
  end
70
98
 
71
99
  describe "Attribute with squish option" do
72
- class MockRecordWithSqueeze < ActiveRecord::Base
73
- column :foo, :string
100
+ class MockRecordWithSqueeze < MockRecordParent #< ActiveRecord::Base
101
+ #column :foo, :string
102
+ attr_accessor :foo
74
103
  auto_strip_attributes :foo, :squish => true
75
104
  end
76
105
 
@@ -90,11 +119,12 @@ describe AutoStripAttributes do
90
119
  end
91
120
 
92
121
  describe "Multible attributes with multiple options" do
93
- class MockRecordWithMultipleAttributes < ActiveRecord::Base
94
- column :foo, :string
95
- column :bar, :string
96
- column :biz, :string
97
- column :bang, :integer
122
+ class MockRecordWithMultipleAttributes < MockRecordParent #< ActiveRecord::Base
123
+ #column :foo, :string
124
+ #column :bar, :string
125
+ #column :biz, :string
126
+ #column :bang, :integer
127
+ attr_accessor :foo, :bar, :biz, :bang
98
128
  auto_strip_attributes :foo, :bar
99
129
  auto_strip_attributes :biz, {:nullify => false, :squish => true}
100
130
  end
@@ -112,8 +142,9 @@ describe AutoStripAttributes do
112
142
  end
113
143
 
114
144
  describe "Attribute with custom setter" do
115
- class MockRecordWithCustomSetter < ActiveRecord::Base
116
- column :foo, :string
145
+ class MockRecordWithCustomSetter < MockRecordParent # < ActiveRecord::Base
146
+ #column :foo, :string
147
+ attr_accessor :foo
117
148
  auto_strip_attributes :foo
118
149
 
119
150
  def foo=(val)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_strip_attributes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Olli Huotari
@@ -15,71 +15,73 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-26 00:00:00 Z
18
+ date: 2011-09-21 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
+ name: activerecord
21
22
  prerelease: false
22
- type: :runtime
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ~>
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  hash: 7
29
29
  segments:
30
30
  - 3
31
31
  - 0
32
32
  version: "3.0"
33
+ type: :runtime
33
34
  version_requirements: *id001
34
- name: activemodel
35
35
  - !ruby/object:Gem::Dependency
36
+ name: activerecord
36
37
  prerelease: false
37
- type: :development
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
- - - ~>
41
+ - - ">="
42
42
  - !ruby/object:Gem::Version
43
43
  hash: 7
44
44
  segments:
45
45
  - 3
46
46
  - 0
47
47
  version: "3.0"
48
+ type: :development
48
49
  version_requirements: *id002
49
- name: activerecord
50
50
  - !ruby/object:Gem::Dependency
51
+ name: minitest
51
52
  prerelease: false
52
- type: :development
53
53
  requirement: &id003 !ruby/object:Gem::Requirement
54
54
  none: false
55
55
  requirements:
56
- - - ~>
56
+ - - "="
57
57
  - !ruby/object:Gem::Version
58
- hash: 27
58
+ hash: 23
59
59
  segments:
60
60
  - 2
61
- - 5
61
+ - 6
62
62
  - 0
63
- version: 2.5.0
63
+ version: 2.6.0
64
+ type: :development
64
65
  version_requirements: *id003
65
- name: minitest
66
66
  - !ruby/object:Gem::Dependency
67
+ name: mocha
67
68
  prerelease: false
68
- type: :development
69
69
  requirement: &id004 !ruby/object:Gem::Requirement
70
70
  none: false
71
71
  requirements:
72
- - - ">="
72
+ - - ~>
73
73
  - !ruby/object:Gem::Version
74
- hash: 3
74
+ hash: 35
75
75
  segments:
76
76
  - 0
77
- version: "0"
77
+ - 9
78
+ - 12
79
+ version: 0.9.12
80
+ type: :development
78
81
  version_requirements: *id004
79
- name: ruby-debug
80
82
  - !ruby/object:Gem::Dependency
83
+ name: rake
81
84
  prerelease: false
82
- type: :development
83
85
  requirement: &id005 !ruby/object:Gem::Requirement
84
86
  none: false
85
87
  requirements:
@@ -89,8 +91,8 @@ dependencies:
89
91
  segments:
90
92
  - 0
91
93
  version: "0"
94
+ type: :development
92
95
  version_requirements: *id005
93
- name: rake
94
96
  description: AutoStripAttributes helps to remove unnecessary whitespaces from ActiveRecord or ActiveModel attributes. It's good for removing accidental spaces from user inputs. It works by adding a before_validation hook to the record. It has option to set empty strings to nil or to remove extra spaces inside the string.
95
97
  email:
96
98
  - olli.huotari@iki.fi
@@ -102,14 +104,16 @@ extra_rdoc_files: []
102
104
 
103
105
  files:
104
106
  - .gitignore
107
+ - .travis.yml
105
108
  - Gemfile
106
109
  - README.md
107
110
  - Rakefile
108
111
  - auto_strip_attributes.gemspec
112
+ - gemfiles/Gemfile.rails-3.0.x
113
+ - gemfiles/Gemfile.rails-3.1.x
109
114
  - lib/auto_strip_attributes.rb
110
115
  - lib/auto_strip_attributes/version.rb
111
116
  - test/auto_strip_attributes_test.rb
112
- - test/test_helper.rb
113
117
  homepage: https://github.com/holli/auto_strip_attributes
114
118
  licenses: []
115
119
 
data/test/test_helper.rb DELETED
@@ -1,16 +0,0 @@
1
- require 'minitest/autorun'
2
- require "active_record"
3
- require "auto_strip_attributes"
4
- #require 'ruby-debug'
5
-
6
- class ActiveRecord::Base
7
- alias_method :save, :valid?
8
- def self.columns()
9
- @columns ||= []
10
- end
11
-
12
- def self.column(name, sql_type = nil, default = nil, null = true)
13
- @columns ||= []
14
- @columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
15
- end
16
- end