Skip to content
Browse files

allow for configuration of selected attributes

  • Loading branch information...
1 parent 73e92fd commit a0bda5cde04f41b953c062ea49051bd98b02ac95 Ryan Sonnek committed Jun 25, 2009
Showing with 218 additions and 4 deletions.
  1. +2 −1 .gitignore
  2. +10 −2 lib/acts_as_stripped.rb
  3. +25 −1 test/acts_as_stripped_test.rb
  4. +181 −0 test/debug.log
View
3 .gitignore
@@ -3,4 +3,5 @@
coverage
rdoc
pkg
-*.sqlite3
+*.sqlite3
+*.log
View
12 lib/acts_as_stripped.rb
@@ -4,7 +4,10 @@ def self.included(base)
end
module ClassMethods
- def acts_as_stripped
+ def acts_as_stripped(*attrs)
+ class_inheritable_accessor :acts_as_stripped_attributes
+ self.acts_as_stripped_attributes = attrs if attrs.any?
+
before_validation :strip_fields
include ActsAsStripped::InstanceMethods
@@ -18,7 +21,12 @@ module SingletonMethods
module InstanceMethods
private
def strip_fields
- self.attributes.each_key {|a| self[a].strip! if self[a].respond_to? :strip! }
+ strippable_attributes.each do |attr|
+ self[attr.to_s].strip!
+ end
+ end
+ def strippable_attributes
+ self.acts_as_stripped_attributes || self.attributes.keys.select {|attr| self[attr].respond_to?(:strip!) }
end
end
end
View
26 test/acts_as_stripped_test.rb
@@ -4,25 +4,49 @@
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
setup do
@user = User.new
end
- should "strips all attributes of extra whitespace" do
+ 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
end
end
+
+ context 'a basic post instance' do
+ setup do
+ @post = Post.new
+ 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
View
181 test/debug.log
@@ -75,3 +75,184 @@

SQL (0.4ms) SELECT version FROM "schema_migrations"
User Create (0.6ms) INSERT INTO "users" ("last_name", "first_name") VALUES('sonnek', 'ryan')
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (0.4ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.5ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.5ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.1ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" varchar(255)) 
+ SQL (2.2ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
+ SQL (2.3ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
+ SQL (0.6ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.3ms) SELECT version FROM "schema_migrations"
+ SQL (2.2ms) INSERT INTO "schema_migrations" (version) VALUES ('1')
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (1.0ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.1ms) DROP TABLE "users"
+ SQL (2.3ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.8ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.0ms) DROP TABLE "posts"
+ SQL (2.1ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" varchar(255)) 
+ SQL (0.9ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.5ms) SELECT version FROM "schema_migrations"
+ Post Create (0.8ms) INSERT INTO "posts" ("title", "body") VALUES('hello world', ' awesome ')
+ User Create (0.6ms) INSERT INTO "users" ("last_name", "first_name") VALUES(' sonnek ', ' ryan ')
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (0.9ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.7ms) DROP TABLE "users"
+ SQL (2.3ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.7ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.8ms) DROP TABLE "posts"
+ SQL (2.5ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" varchar(255)) 
+ SQL (1.0ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.4ms) SELECT version FROM "schema_migrations"
+ Post Create (0.6ms) INSERT INTO "posts" ("title", "body") VALUES('hello world', ' awesome ')
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (1.0ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.4ms) DROP TABLE "users"
+ SQL (1.9ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (1.0ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.2ms) DROP TABLE "posts"
+ SQL (2.5ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" varchar(255)) 
+ SQL (1.0ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.5ms) SELECT version FROM "schema_migrations"
+ Post Create (0.9ms) INSERT INTO "posts" ("title", "body") VALUES('hello world', ' awesome ')
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (0.9ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.2ms) DROP TABLE "users"
+ SQL (2.5ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.8ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.7ms) DROP TABLE "posts"
+ SQL (2.3ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" varchar(255)) 
+ SQL (0.8ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.4ms) SELECT version FROM "schema_migrations"
+ Post Create (0.6ms) INSERT INTO "posts" ("title", "body") VALUES('hello world', ' awesome ')
+ User Create (1.1ms) INSERT INTO "users" ("last_name", "first_name") VALUES(' sonnek ', ' ryan ')
+ SQL (0.7ms) select sqlite_version(*)
+ SQL (0.9ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.4ms) DROP TABLE "users"
+ SQL (2.1ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.6ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.1ms) DROP TABLE "posts"
+ SQL (2.6ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" varchar(255)) 
+ SQL (0.7ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.4ms) SELECT version FROM "schema_migrations"
+ Post Create (0.6ms) INSERT INTO "posts" ("title", "body") VALUES('hello world', ' awesome ')
+ User Create (0.5ms) INSERT INTO "users" ("last_name", "first_name") VALUES('sonnek', 'ryan')
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (0.9ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.5ms) DROP TABLE "users"
+ SQL (2.4ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255)) 
+ SQL (0.8ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.6ms) DROP TABLE "posts"
+ SQL (2.3ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" varchar(255)) 
+ SQL (1.8ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (1.0ms) SELECT version FROM "schema_migrations"
+ Post Create (1.0ms) INSERT INTO "posts" ("title", "body") VALUES('hello world', ' awesome ')
+ User Create (0.6ms) INSERT INTO "users" ("last_name", "first_name") VALUES('sonnek', 'ryan')
+ SQL (0.7ms) select sqlite_version(*)
+ SQL (1.0ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.3ms) DROP TABLE "users"
+ SQL (2.1ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255), "logged_in_at" datetime) 
+ SQL (0.6ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (2.8ms) DROP TABLE "posts"
+ SQL (2.2ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" varchar(255)) 
+ SQL (1.0ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.4ms) SELECT version FROM "schema_migrations"
+ Post Create (0.6ms) INSERT INTO "posts" ("title", "body") VALUES('hello world', ' awesome ')
+ User Create (0.6ms) INSERT INTO "users" ("last_name", "logged_in_at", "first_name") VALUES('sonnek', NULL, 'ryan')
+ SQL (0.6ms) select sqlite_version(*)
+ SQL (1.0ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.3ms) DROP TABLE "users"
+ SQL (2.3ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar(255), "last_name" varchar(255), "logged_in_at" datetime) 
+ SQL (0.7ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (3.4ms) DROP TABLE "posts"
+ SQL (2.5ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "body" varchar(255)) 
+ SQL (0.9ms)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.4ms) SELECT version FROM "schema_migrations"
+ Post Create (0.6ms) INSERT INTO "posts" ("title", "body") VALUES('hello world', ' awesome ')
+ User Create (0.6ms) INSERT INTO "users" ("last_name", "logged_in_at", "first_name") VALUES('sonnek', '2009-06-25 10:15:33', 'ryan')

0 comments on commit a0bda5c

Please sign in to comment.
Something went wrong with that request. Please try again.