auto_strip_attributes 1.0.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 ADDED
@@ -0,0 +1,9 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ .loadpath
7
+ .project
8
+ .idea
9
+ .redcar
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in auto_strip_attributes.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # AutoStripAttributes
2
+
3
+ AutoStripAttributes helps to remove unnecessary whitespaces from ActiveRecord or ActiveModel attributes.
4
+ It's good for removing accidental spaces from user inputs (e.g. when user copy/pastes some value to a form and the value has extra spaces at the end).
5
+
6
+ It works by adding a before_validation hook to the record. No other methods are added. Gem is kept as simple as possible.
7
+
8
+ Gem has option to set empty strings to nil or to remove extra spaces inside the string.
9
+
10
+ ## Howto / examples
11
+
12
+ Include gem in your Gemfile:
13
+
14
+ ```ruby
15
+ gem "auto_strip_attributes", "~> 1.0"
16
+ ```
17
+
18
+ ```ruby
19
+ class User < ActiveRecord::Base
20
+
21
+ # Normal usage where " aaa bbb\t " changes to "aaa bbb"
22
+ auto_strip_attributes :nick, :comment
23
+
24
+ # Squeezes spaces inside the string: "James Bond " => "James Bond"
25
+ auto_strip_attributes :name, :squish => true
26
+
27
+ # Won't set to null even if string is blank. " " => ""
28
+ auto_strip_attributes :email, :nullify => false
29
+ end
30
+ ```
31
+
32
+ # Support
33
+
34
+ Submit suggestions or feature requests as a GitHub Issue or Pull Request. Remember to update tests. Tests are quite extensive.
35
+
36
+ ## Similar gems
37
+
38
+ 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.
40
+
41
+ - https://github.com/phatworx/acts_as_strip
42
+ - https://github.com/rmm5t/strip_attributes
43
+ - https://github.com/thomasfedb/attr_cleaner
44
+
45
+ # Licence
46
+
47
+ Released under the MIT license (http://www.opensource.org/licenses/mit-license.php)
48
+
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "rake/testtask"
2
+ require "bundler/gem_tasks"
3
+ require "bundler/setup"
4
+
5
+ desc "Default: run unit tests."
6
+ task :default => :test
7
+
8
+ desc "Run tests for gem."
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << "lib" << "test"
11
+ t.pattern = "test/**/*_test.rb"
12
+ t.verbose = true
13
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "auto_strip_attributes/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "auto_strip_attributes"
7
+ s.version = AutoStripAttributes::VERSION
8
+ s.authors = ["Olli Huotari"]
9
+ s.email = ["olli.huotari@iki.fi"]
10
+ s.homepage = "https://github.com/holli/auto_strip_attributes"
11
+ s.summary = "Removes unnecessary whitespaces in attributes. Extension to ActiveRecord or ActiveModel."
12
+ s.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."
13
+
14
+ s.rubyforge_project = "auto_strip_attributes"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+
22
+ s.add_runtime_dependency "activemodel", "~> 3.0"
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'
27
+ s.add_development_dependency 'rake'
28
+ end
@@ -0,0 +1,37 @@
1
+ require "auto_strip_attributes/version"
2
+
3
+ module AutoStripAttributes
4
+
5
+ def auto_strip_attributes(*attributes)
6
+ options = {:nullify => true, :squish => false}
7
+ if attributes.last.is_a?(Hash)
8
+ options = options.merge(attributes.pop)
9
+ end
10
+
11
+ attributes.each do |attribute|
12
+ before_validation do |record|
13
+ value = record.send(attribute)
14
+ #if value.respond_to?('strip')
15
+ if value.respond_to?(:strip)
16
+ value_stripped = (options[:nullify] && value.blank?) ? nil : value.strip
17
+
18
+ # gsub(/\s+/, ' ') is same as in Rails#String.squish http://api.rubyonrails.org/classes/String.html#method-i-squish-21
19
+ value_stripped = value_stripped.gsub(/\s+/, ' ') if options[:squish] && value_stripped.respond_to?(:gsub)
20
+
21
+ record[attribute] = value_stripped
22
+
23
+ # Alternate way would be to use send(attribute=)
24
+ # But that would end up calling =-method twice, once when setting, once in before_validate
25
+ # if (attr != attr_stripped)
26
+ # record.send("#{attribute}=", value_stripped) # does add some overhead, attribute= will be called before each validation
27
+ # end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ ActiveRecord::Base.send(:extend, AutoStripAttributes) if defined? ActiveRecord
36
+ #ActiveModel::Validations::HelperMethods.send(:include, AutoStripAttributes)
37
+
@@ -0,0 +1,4 @@
1
+ module AutoStripAttributes
2
+ #VERSION = "0.0.1"
3
+ VERSION = "1.0.0"
4
+ end
@@ -0,0 +1,134 @@
1
+ require "test/test_helper"
2
+ # bundle exec ruby test/auto_strip_attributes_test.rb -v --name /test_name/
3
+
4
+ #class AutoStripAttributesTest < Test::Unit::TestCase
5
+ #class AutoStripAttributesTest < MiniTest::Unit::TestCase
6
+ describe AutoStripAttributes do
7
+
8
+ def setup
9
+ @init_params = { :foo => "\tfoo ", :bar => " bar bar "}
10
+ end
11
+
12
+ it "should have defined AutoStripAttributes" do
13
+ assert Object.const_defined?(:AutoStripAttributes)
14
+ end
15
+
16
+ describe "Basic attribute with default options" do
17
+ class MockRecordBasic < ActiveRecord::Base
18
+ column :foo, :string
19
+ auto_strip_attributes :foo
20
+ end
21
+ before do
22
+ @record = MockRecordBasic.new()
23
+ end
24
+
25
+ it "should be ok for normal strings" do
26
+ @record.foo = " aaa \t"
27
+ @record.valid?
28
+ @record.foo.must_equal "aaa"
29
+ end
30
+
31
+ it "should set empty strings to nil" do
32
+ @record.foo = " "
33
+ @record.valid?
34
+ @record.foo.must_be_nil
35
+ end
36
+
37
+ 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")
41
+ @record.foo = str_mock
42
+ @record.valid?
43
+ str_mock.verify
44
+ @record.foo.must_be_same_as @stripped_str
45
+ end
46
+
47
+ it "should not call strip method for non strippable attributes" do
48
+ str_mock = MiniTest::Mock.new() # answers false to str_mock.respond_to?(:strip)
49
+ str_mock.expect :'nil?', false
50
+ @record.foo = str_mock
51
+ @record.valid?
52
+ str_mock.verify
53
+ assert @record.foo === str_mock
54
+ end
55
+ end
56
+
57
+ describe "Attribute with nullify option" do
58
+ class MockRecordWithNullify < ActiveRecord::Base
59
+ column :foo, :string
60
+ auto_strip_attributes :foo, :nullify => false
61
+ end
62
+
63
+ it "should not set blank strings to nil" do
64
+ @record = MockRecordWithNullify.new
65
+ @record.foo = " "
66
+ @record.valid?
67
+ @record.foo.must_equal ""
68
+ end
69
+ end
70
+
71
+ describe "Attribute with squish option" do
72
+ class MockRecordWithSqueeze < ActiveRecord::Base
73
+ column :foo, :string
74
+ auto_strip_attributes :foo, :squish => true
75
+ end
76
+
77
+ it "should squish string also form inside" do
78
+ @record = MockRecordWithSqueeze.new
79
+ @record.foo = " aaa\t\n bbb"
80
+ @record.valid?
81
+ @record.foo.must_equal "aaa bbb"
82
+ end
83
+
84
+ it "should do normal nullify with empty string" do
85
+ @record = MockRecordWithSqueeze.new
86
+ @record.foo = " "
87
+ @record.valid?
88
+ @record.foo.must_be_nil
89
+ end
90
+ end
91
+
92
+ 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
98
+ auto_strip_attributes :foo, :bar
99
+ auto_strip_attributes :biz, {:nullify => false, :squish => true}
100
+ end
101
+
102
+ it "should handle everything ok" do
103
+ @record = MockRecordWithMultipleAttributes.new
104
+ @record.foo = " foo\tfoo"
105
+ @record.bar = " "
106
+ @record.biz = " "
107
+ @record.valid?
108
+ @record.foo.must_equal "foo\tfoo"
109
+ @record.bar.must_be_nil
110
+ @record.biz.must_equal ""
111
+ end
112
+ end
113
+
114
+ describe "Attribute with custom setter" do
115
+ class MockRecordWithCustomSetter < ActiveRecord::Base
116
+ column :foo, :string
117
+ auto_strip_attributes :foo
118
+
119
+ def foo=(val)
120
+ self[:foo] = "#{val}-#{val}"
121
+ end
122
+ end
123
+
124
+ it "should not call setter again in before_validation" do
125
+ @record = MockRecordWithCustomSetter.new
126
+ @record.foo = " foo "
127
+ @record.foo.must_equal " foo - foo "
128
+ @record.valid?
129
+ @record.foo.must_equal "foo - foo"
130
+ end
131
+ end
132
+
133
+
134
+ end
@@ -0,0 +1,16 @@
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
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_strip_attributes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Olli Huotari
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-26 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ type: :runtime
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ version: "3.0"
33
+ version_requirements: *id001
34
+ name: activemodel
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ type: :development
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 3
46
+ - 0
47
+ version: "3.0"
48
+ version_requirements: *id002
49
+ name: activerecord
50
+ - !ruby/object:Gem::Dependency
51
+ prerelease: false
52
+ type: :development
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 27
59
+ segments:
60
+ - 2
61
+ - 5
62
+ - 0
63
+ version: 2.5.0
64
+ version_requirements: *id003
65
+ name: minitest
66
+ - !ruby/object:Gem::Dependency
67
+ prerelease: false
68
+ type: :development
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ version_requirements: *id004
79
+ name: ruby-debug
80
+ - !ruby/object:Gem::Dependency
81
+ prerelease: false
82
+ type: :development
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ version_requirements: *id005
93
+ name: rake
94
+ 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
+ email:
96
+ - olli.huotari@iki.fi
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files: []
102
+
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - README.md
107
+ - Rakefile
108
+ - auto_strip_attributes.gemspec
109
+ - lib/auto_strip_attributes.rb
110
+ - lib/auto_strip_attributes/version.rb
111
+ - test/auto_strip_attributes_test.rb
112
+ - test/test_helper.rb
113
+ homepage: https://github.com/holli/auto_strip_attributes
114
+ licenses: []
115
+
116
+ post_install_message:
117
+ rdoc_options: []
118
+
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ hash: 3
136
+ segments:
137
+ - 0
138
+ version: "0"
139
+ requirements: []
140
+
141
+ rubyforge_project: auto_strip_attributes
142
+ rubygems_version: 1.8.6
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: Removes unnecessary whitespaces in attributes. Extension to ActiveRecord or ActiveModel.
146
+ test_files: []
147
+