No commit activity in last 3 years
No release in over 3 years
Set default value to Rails application's ActiveRecord model without saved.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 4.2.0
 Project Readme

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