strip_attributes 0.9.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.
@@ -0,0 +1,33 @@
1
+ require "shoulda/active_record"
2
+
3
+ module StripAttributes
4
+ module Shoulda
5
+ module Macros
6
+ def should_strip_attributes(*attributes)
7
+ klass = respond_to?(:described_type) ? described_type : model_class
8
+ attributes.each do |attribute|
9
+ attribute = attribute.to_sym
10
+ should "strip whitespace from #{attribute}" do
11
+ object = get_instance_of(klass)
12
+ object.send("#{attribute}=", " string ")
13
+ object.valid?
14
+ assert_equal "string", object.send(attribute)
15
+ end
16
+ end
17
+ end
18
+
19
+ def should_not_strip_attributes(*attributes)
20
+ klass = respond_to?(:described_type) ? described_type : model_class
21
+ attributes.each do |attribute|
22
+ attribute = attribute.to_sym
23
+ should "not strip whitespace from #{attribute}" do
24
+ object = get_instance_of(klass)
25
+ object.send("#{attribute}=", " string ")
26
+ object.valid?
27
+ assert_equal " string ", object.send(attribute)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ require "strip_attributes/shoulda/macros"
@@ -0,0 +1,33 @@
1
+ module StripAttributes
2
+ VERSION = "0.9.0"
3
+
4
+ # Strips whitespace from model fields and converts blank values to nil.
5
+ def strip_attributes!(options = nil)
6
+ before_validation do |record|
7
+ attributes = StripAttributes.narrow(record.attributes, options)
8
+ attributes.each do |attr, value|
9
+ if value.respond_to?(:strip)
10
+ record[attr] = (value.blank?) ? nil : value.strip
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ # Necessary because Rails has removed the narrowing of attributes using :only
17
+ # and :except on Base#attributes
18
+ def self.narrow(attributes, options)
19
+ if options.nil?
20
+ attributes
21
+ else
22
+ if except = options[:except]
23
+ except = Array(except).collect { |attribute| attribute.to_s }
24
+ attributes.except(*except)
25
+ elsif only = options[:only]
26
+ only = Array(only).collect { |attribute| attribute.to_s }
27
+ attributes.slice(*only)
28
+ else
29
+ raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"
30
+ end
31
+ end
32
+ end
33
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'strip_attributes'
2
+ ActiveRecord::Base.extend(StripAttributes)
@@ -0,0 +1,96 @@
1
+ require "test_helper"
2
+
3
+ module MockAttributes
4
+ def self.included(base)
5
+ base.column :foo, :string
6
+ base.column :bar, :string
7
+ base.column :biz, :string
8
+ base.column :baz, :string
9
+ base.column :bang, :string
10
+ end
11
+ end
12
+
13
+ class StripAllMockRecord < ActiveRecord::Base
14
+ include MockAttributes
15
+ strip_attributes!
16
+ end
17
+
18
+ class StripOnlyOneMockRecord < ActiveRecord::Base
19
+ include MockAttributes
20
+ strip_attributes! :only => :foo
21
+ end
22
+
23
+ class StripOnlyThreeMockRecord < ActiveRecord::Base
24
+ include MockAttributes
25
+ strip_attributes! :only => [:foo, :bar, :biz]
26
+ end
27
+
28
+ class StripExceptOneMockRecord < ActiveRecord::Base
29
+ include MockAttributes
30
+ strip_attributes! :except => :foo
31
+ end
32
+
33
+ class StripExceptThreeMockRecord < ActiveRecord::Base
34
+ include MockAttributes
35
+ strip_attributes! :except => [:foo, :bar, :biz]
36
+ end
37
+
38
+ class StripAttributesTest < Test::Unit::TestCase
39
+ def setup
40
+ @init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "", :bang => " " }
41
+ end
42
+
43
+ def test_should_exist
44
+ assert Object.const_defined?(:StripAttributes)
45
+ end
46
+
47
+ def test_should_strip_all_fields
48
+ record = StripAllMockRecord.new(@init_params)
49
+ record.valid?
50
+ assert_equal "foo", record.foo
51
+ assert_equal "bar", record.bar
52
+ assert_equal "biz", record.biz
53
+ assert_nil record.baz
54
+ assert_nil record.bang
55
+ end
56
+
57
+ def test_should_strip_only_one_field
58
+ record = StripOnlyOneMockRecord.new(@init_params)
59
+ record.valid?
60
+ assert_equal "foo", record.foo
61
+ assert_equal "bar \t ", record.bar
62
+ assert_equal "\tbiz ", record.biz
63
+ assert_equal "", record.baz
64
+ assert_equal " ", record.bang
65
+ end
66
+
67
+ def test_should_strip_only_three_fields
68
+ record = StripOnlyThreeMockRecord.new(@init_params)
69
+ record.valid?
70
+ assert_equal "foo", record.foo
71
+ assert_equal "bar", record.bar
72
+ assert_equal "biz", record.biz
73
+ assert_equal "", record.baz
74
+ assert_equal " ", record.bang
75
+ end
76
+
77
+ def test_should_strip_all_except_one_field
78
+ record = StripExceptOneMockRecord.new(@init_params)
79
+ record.valid?
80
+ assert_equal "\tfoo", record.foo
81
+ assert_equal "bar", record.bar
82
+ assert_equal "biz", record.biz
83
+ assert_nil record.baz
84
+ assert_nil record.bang
85
+ end
86
+
87
+ def test_should_strip_all_except_three_fields
88
+ record = StripExceptThreeMockRecord.new(@init_params)
89
+ record.valid?
90
+ assert_equal "\tfoo", record.foo
91
+ assert_equal "bar \t ", record.bar
92
+ assert_equal "\tbiz ", record.biz
93
+ assert_nil record.baz
94
+ assert_nil record.bang
95
+ end
96
+ end
@@ -0,0 +1,21 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ begin require 'redgreen' if ENV['TM_FILENAME'].nil?; rescue LoadError; end
5
+
6
+ PLUGIN_ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
7
+
8
+ $LOAD_PATH.unshift "#{PLUGIN_ROOT}/lib"
9
+ load "#{PLUGIN_ROOT}/rails/init.rb"
10
+
11
+ class ActiveRecord::Base
12
+ alias_method :save, :valid?
13
+ def self.columns()
14
+ @columns ||= []
15
+ end
16
+
17
+ def self.column(name, sql_type = nil, default = nil, null = true)
18
+ @columns ||= []
19
+ @columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strip_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan McGeary
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70312180321220 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70312180321220
25
+ description: StripAttributes automatically strips all ActiveRecord model attributes
26
+ of leading and trailing whitespace before validation. If the attribute is blank,
27
+ it strips the value to nil.
28
+ email:
29
+ - ryan@mcgeary.org
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/strip_attributes/shoulda/macros.rb
35
+ - lib/strip_attributes/shoulda.rb
36
+ - lib/strip_attributes.rb
37
+ - rails/init.rb
38
+ - test/strip_attributes_test.rb
39
+ - test/test_helper.rb
40
+ homepage: https://github.com/rmm5t/strip_attributes
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ segments:
53
+ - 0
54
+ hash: 1913689736864890134
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ segments:
62
+ - 0
63
+ hash: 1913689736864890134
64
+ requirements: []
65
+ rubyforge_project: strip_attributes
66
+ rubygems_version: 1.8.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Whitespace cleanup for ActiveRecord attributes
70
+ test_files:
71
+ - test/strip_attributes_test.rb
72
+ - test/test_helper.rb