From c7c6200208874b54e25c11cccc670d2b6c2811f6 Mon Sep 17 00:00:00 2001 From: haruki_zaemon Date: Tue, 6 Nov 2007 06:44:04 +0000 Subject: [PATCH] [NEW] Plugin to strip leading and trailing blanks from attributes. git-svn-id: svn://rubyforge.org/var/svn/redhillonrails/trunk@301 67570413-2614-0410-8c7a-c7689aa8d500 --- test/unit/customer_test.rb | 24 ++++++++++++++++++++++ vendor/plugins/stripper/CHANGELOG | 3 +++ vendor/plugins/stripper/MIT-LICENSE | 20 ++++++++++++++++++ vendor/plugins/stripper/README | 8 ++++++++ vendor/plugins/stripper/about.yml | 5 +++++ vendor/plugins/stripper/init.rb | 1 + .../acts/stripper/active_record/base.rb | 13 ++++++++++++ 7 files changed, 74 insertions(+) create mode 100644 vendor/plugins/stripper/CHANGELOG create mode 100644 vendor/plugins/stripper/MIT-LICENSE create mode 100644 vendor/plugins/stripper/README create mode 100644 vendor/plugins/stripper/about.yml create mode 100644 vendor/plugins/stripper/init.rb create mode 100644 vendor/plugins/stripper/lib/red_hill_consulting/acts/stripper/active_record/base.rb diff --git a/test/unit/customer_test.rb b/test/unit/customer_test.rb index 9317ec0..c5c2f82 100644 --- a/test/unit/customer_test.rb +++ b/test/unit/customer_test.rb @@ -52,4 +52,28 @@ class CustomerTest < Test::Unit::TestCase assert !customer.valid? assert_equal "is invalid", customer.errors[:phone] end + + def test_blanks_are_stripped_from_name_using_setter + customer = Customer.new + customer.name = " this is a name " + assert_equal "this is a name", customer.name + end + + def test_blanks_are_stripped_from_name_using_indexed_setter + customer = Customer.new + customer["name"] = " this is a name " + assert_equal "this is a name", customer.name + end + + def test_blanks_are_stripped_from_name_using_write_attribute + customer = Customer.new + customer.write_attribute("name", " this is a name ") + assert_equal "this is a name", customer.name + end + + def test_blanks_are_stripped_from_name_using_bulk_setter + customer = Customer.new + customer.attributes = { :name => " this is a name " } + assert_equal "this is a name", customer.name + end end diff --git a/vendor/plugins/stripper/CHANGELOG b/vendor/plugins/stripper/CHANGELOG new file mode 100644 index 0000000..a0782eb --- /dev/null +++ b/vendor/plugins/stripper/CHANGELOG @@ -0,0 +1,3 @@ +[REVISION 20071106] + +[NEW] Initial revision. diff --git a/vendor/plugins/stripper/MIT-LICENSE b/vendor/plugins/stripper/MIT-LICENSE new file mode 100644 index 0000000..2e57ee9 --- /dev/null +++ b/vendor/plugins/stripper/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2007 RedHill Consulting, Pty. Ltd. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/plugins/stripper/README b/vendor/plugins/stripper/README new file mode 100644 index 0000000..05499ab --- /dev/null +++ b/vendor/plugins/stripper/README @@ -0,0 +1,8 @@ += Acts As Stripper + +Acts As Stripper is a plugin that + +=== License + +This plugin is copyright 2006 by RedHill Consulting, Pty. Ltd. and is released +under the MIT license. diff --git a/vendor/plugins/stripper/about.yml b/vendor/plugins/stripper/about.yml new file mode 100644 index 0000000..2667c5d --- /dev/null +++ b/vendor/plugins/stripper/about.yml @@ -0,0 +1,5 @@ +author: simon@redhillconsulting.com.au +summary: Acts As Stripper is a plugin that +homepage: http://www.redhillonrails.org +license: MIT +rails_version: EDGE diff --git a/vendor/plugins/stripper/init.rb b/vendor/plugins/stripper/init.rb new file mode 100644 index 0000000..c921518 --- /dev/null +++ b/vendor/plugins/stripper/init.rb @@ -0,0 +1 @@ +ActiveRecord::Base.send :include, RedHillConsulting::Acts::Stripper::ActiveRecord::Base diff --git a/vendor/plugins/stripper/lib/red_hill_consulting/acts/stripper/active_record/base.rb b/vendor/plugins/stripper/lib/red_hill_consulting/acts/stripper/active_record/base.rb new file mode 100644 index 0000000..fd50611 --- /dev/null +++ b/vendor/plugins/stripper/lib/red_hill_consulting/acts/stripper/active_record/base.rb @@ -0,0 +1,13 @@ +module RedHillConsulting::Acts::Stripper::ActiveRecord + module Base + def self.included(base) + base.alias_method_chain :write_attribute, :stripper + end + + def write_attribute_with_stripper(attr_name, value) + value = value.strip if value.respond_to?(:strip) + value = nil if value.blank? + write_attribute_without_stripper(attr_name, value) + end + end +end -- 2.11.4.GIT