Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

force configuration to identify what fields to strip

  • Loading branch information...
commit d333e31cff70a26cfb41181f89b6e885accadd98 1 parent c50111f
Ryan Sonnek authored
View
16 README.rdoc
@@ -4,22 +4,12 @@ Simple utility to strip whitespace from string attributes.
= Installation
-Command line installation
-
- sudo gem install wireframe-acts_as_stripped
-
-Rails environment.rb configuration
-
- config.gem 'wireframe-acts_as_stripped', :source => 'http://gems.github.com', :lib => 'acts_as_stripped'
+Bundler configuration
+ gem 'acts_as_stripped'
= Usage
- # strip whitespace from *all* string attributes
- class User < ActiveRecord::Base
- acts_as_stripped
- end
-
- # strip whitespace from *select* string attributes
+ # strip whitespace from specified string attributes
class Post < ActiveRecord::Base
acts_as_stripped :title, :summary
end
View
5 lib/acts_as_stripped.rb
@@ -21,13 +21,10 @@ module SingletonMethods
module InstanceMethods
private
def strip_fields
- strippable_attributes.each do |attr|
+ self.acts_as_stripped_attributes.each do |attr|
self[attr.to_s].strip! unless self[attr.to_s].nil?
end
end
- def strippable_attributes
- self.acts_as_stripped_attributes || self.attributes.keys.select {|attr| self[attr].respond_to?(:strip!) }
- end
end
end
View
56 test/acts_as_stripped_test.rb
@@ -1,57 +1,32 @@
require 'test_helper'
ActiveRecord::Schema.define(:version => 1) do
- create_table :users, :force => true do |t|
- t.column :first_name, :string
- t.column :last_name, :string
- t.column :logged_in_at, :datetime
- end
create_table :posts, :force => true do |t|
t.column :title, :string
t.column :body, :string
end
end
-class User < ActiveRecord::Base
- acts_as_stripped
-end
-
class Post < ActiveRecord::Base
acts_as_stripped :title
end
class ActsAsStrippedTest < Test::Unit::TestCase
- context 'a basic user instance' do
+ context 'a basic post instance' do
setup do
- @user = User.new
- end
- should "strips all string attributes of extra whitespace" do
- @user.first_name = ' ryan '
- @user.last_name = ' sonnek '
- @user.logged_in_at = Time.now
-
- @user.save!
- assert_equal 'ryan', @user.first_name
- assert_equal 'sonnek', @user.last_name
+ @post = Post.new
end
-
- should "not fail if any attributes are nil" do
- @user.first_name = ' shaq '
- @user.last_name = nil
- @user.logged_in_at = Time.now
-
- assert_nothing_raised do
- @user.save!
- end
+ should 'strip whitespace from title' do
+ @post.title = ' hello world '
+ @post.save!
- assert_equal 'shaq', @user.first_name
- assert_nil @user.last_name
+ assert_equal 'hello world', @post.title
end
- end
-
- context 'a basic post instance' do
- setup do
- @post = Post.new
+ should 'not strip whitespace from body' do
+ @post.body = ' awesome '
+ @post.save!
+
+ assert_equal ' awesome ', @post.body
end
should 'not fail if title is nil' do
@post.title = nil
@@ -60,14 +35,5 @@ class ActsAsStrippedTest < Test::Unit::TestCase
end
assert_nil @post.title
end
-
- should 'strip whitespace only from title' do
- @post.title = ' hello world '
- @post.body = ' awesome '
- @post.save!
-
- assert_equal 'hello world', @post.title
- assert_equal ' awesome ', @post.body
- end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.