Skip to content

Commit

Permalink
force configuration to identify what fields to strip
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Sonnek committed Jun 15, 2010
1 parent c50111f commit d333e31
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 62 deletions.
16 changes: 3 additions & 13 deletions README.rdoc
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions lib/acts_as_stripped.rb
Expand Up @@ -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

Expand Down
56 changes: 11 additions & 45 deletions 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
Expand All @@ -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

0 comments on commit d333e31

Please sign in to comment.