StripAttributes is an ActiveModel extension that automatically strips all
attributes of leading and trailing whitespace before validation. If the
attribute is blank, it strips the value to nil
by default.
It works by adding a before_validation hook to the record. By default, all
attributes are stripped of whitespace, but :only
and :except
options can be used to limit which attributes are stripped. Both options accept
a single attribute (:only => :field
) or arrays of attributes (:except => [:field1, :field2, :field3]
).
How You Can Help
If you like this project, buy me a coffee, donate via Gratipay, or book a session with me on Codementor.
Bitcoin: 1rmm5tv6f997JK5bLcGbRCZyVjZUPkQ2m
Include the gem in your Gemfile:
gem "strip_attributes"
class DrunkPokerPlayer < ActiveRecord::Base
strip_attributes
end
# all attributes will be stripped except :boxers
class SoberPokerPlayer < ActiveRecord::Base
strip_attributes :except => :boxers
end
# only :shoe, :sock, and :glove attributes will be stripped
class ConservativePokerPlayer < ActiveRecord::Base
strip_attributes :only => [:shoe, :sock, :glove]
end
# Empty attributes will not be converted to nil
class BrokePokerPlayer < ActiveRecord::Base
strip_attributes :allow_empty => true
end
# Sequential spaces in attributes will be collapsed to one space
class EloquentPokerPlayer < ActiveRecord::Base
strip_attributes :collapse_spaces => true
end
# Newlines in attributes will be replaced with a space
class EloquentPokerPlayer < ActiveRecord::Base
strip_attributes :replace_newlines => true
end
class User < ActiveRecord::Base
# Strip off characters defined by RegEx
strip_attributes :only => [:first_name, :last_name], :regex => /[^[:alpha:]\s]/
# Strip off non-integers
strip_attributes :only => [:phone], :regex => /[^0-9]/
# Strip off all spaces and keep only alphabetic and numeric characters
strip_attributes :only => [:nick_name], :regex => /[^[:alnum:]\S]/
end
It also works on other ActiveModel classes, such as Mongoid documents:
class User
include Mongoid::Document
strip_attributes :only => :email
end
Using it with ActiveAttr
class Person
include ActiveAttr::Model
include ActiveModel::Validations::Callbacks
attribute :name
attribute :email
strip_attributes
end
# where record is an ActiveModel instance
StripAttributes.strip(record, :collapse_spaces => true)
# works directly on Strings too
StripAttributes.strip(" foo \t") #=> "foo"
StripAttributes.strip(" foo bar", :collapse_spaces => true) #=> "foo bar"
StripAttributes provides an RSpec/Shoulda-compatible matcher for easier testing of attribute assignment. You can use this with RSpec, Shoulda, or Minitest-Matchers.
To initialize RSpec, add this to your spec_helper.rb
:
require "strip_attributes/matchers"
RSpec.configure do |config|
config.include StripAttributes::Matchers
end
To initialize Shoulda (with test-unit), add this to your test_helper.rb
:
require "strip_attributes/matchers"
class Test::Unit::TestCase
extend StripAttributes::Matchers
end
To initialize Minitest-Matchers, add this to your test_helper.rb
:
require "strip_attributes/matchers"
class MiniTest::Spec
include StripAttributes::Matchers
end
RSpec:
describe User do
it { is_expected.to strip_attribute(:name).collapse_spaces }
it { is_expected.to strip_attribute :email }
it { is_expected.not_to strip_attribute :password }
end
Shoulda (with test-unit):
class UserTest < ActiveSupport::TestCase
should strip_attribute(:name).collapse_spaces
should strip_attribute :email
should_not strip_attribute :password
end
Minitest-Matchers:
describe User do
subject { User.new }
must { strip_attribute(:name).collapse_spaces }
must { strip_attribute :email }
wont { strip_attribute :password }
end
Submit suggestions or feature requests as a GitHub Issue or Pull Request (preferred). If you send a pull request, remember to update the corresponding unit tests. In fact, I prefer new features to be submitted in the form of new unit tests.
The idea was originally triggered by the information at the (now defunct) Rails Wiki but was modified from the original to include more idiomatic ruby and rails support.
Semantic Versioning 2.0 as defined at http://semver.org.
Copyright (c) 2007-2015 Ryan McGeary released under the MIT license