has_default_value
has_default_value sets default value to Rails application's ActiveRecord model without saved.
The default value set with I18n.
Installation
Add this line to your application's Gemfile:
gem 'has_default_value'
And then execute:
$ bundle
Usage
In app/models/user.rb
:
class User < ActiveRecord::Base
has_default_value :name
end
In config/locales/en.yml
:
en:
activerecord:
defaults:
user:
name: no name
Then application behaves as follows:
$ bin/rails c
> user = User.create
>
> user.name #=> "no name"
> user.read_attribute(:name) #=> nil
>
> user.name = 'user name'
> user.name #=> "user name"
That's it.
If you want to set multiple default values, you can write as follows:
class User < ActiveRecord::Base
has_default_value :name, :email, :address, ...
end
Contributing
- Fork it ( https://github.com/kami30k/has_default_value/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request