Set default value to Rails application's ActiveRecord model without saved.
Ruby
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
Gemfile
Gemfile.lock
MIT-LICENSE
README.md
Rakefile
has_default_value.gemspec

README.md

has_default_value

Build Status Gem Version

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

  1. Fork it ( https://github.com/kami30k/has_default_value/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request