A simple Ruby on Rails plugin to let model auto do something (like strip, downcase, etc.) before validation.
Before validation do strip, in your model:
auto :strip, :for => :field
For many fields:
auto :strip, :for => [:field1, :field2, ...]
Before validation do substr (with one argument):
auto :, :with => 0...10, :for => :field_or_fields
Or if you want with more argument:
auto :, :with => [0, 10], :for => :field_or_fields
An example from spec:
class Item < ActiveRecord::Base auto :strip, :for => [:nickname, :email, :info] auto :downcase, :for => :email auto :, :with => 0...10, :for => :info # or auto :, :with => [0, 10], :for => :info end user = User.new(:nickname => ' ayaya ', :email => ' AYAYA@EXAMPLE.COM ', :info => ' hello world! ') user.valid? # or save user.nickname.should == 'ayaya' user.email.should == 'email@example.com' user.info.should == 'hello worl'
Just include AutoDoSomething in model class then use it like in ActiveRecord, see full example in spec/spec_helper.rb.
gem 'auto_do_something', :git => 'git://github.com/ayamomiji/auto_do_something.git'
or install as a plugin:
rails plugin install git://github.com/ayamomiji/auto_do_something.git
Contributing to auto_do_something
Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
Fork the project
Start a feature/bugfix branch
Commit and push until you are happy with your contribution
Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright © 2011 ayaya. See LICENSE.txt for further details.