Permalink
Browse files

Updating docs for default values

  • Loading branch information...
beerlington committed Feb 8, 2013
1 parent 41cf577 commit f33a7dc0fc93e3ccdb08d37bad64c1b3a0c78ac1
Showing with 36 additions and 8 deletions.
  1. +8 −0 CHANGELOG.md
  2. +25 −8 README.md
  3. +3 −0 lib/classy_enum/active_record.rb
View
@@ -1,5 +1,13 @@
# ClassyEnum Changelog
+## 3.2.0
+
+* Default values can now be specified within an ActiveRecord model
+
+ class Alarm < ActiveRecord::Base
+ classy_enum_attr :priority, :default => 'medium'
+ end
+
## 3.1.3
* Fixes saving and reloading ActiveRecord models that assign enum using
View
@@ -8,14 +8,15 @@ ClassyEnum is a Ruby on Rails gem that adds class-based enumerator functionality
## README Topics
-* [Example Usage](https://github.com/beerlington/classy_enum#example-usage)
-* [Internationalization](https://github.com/beerlington/classy_enum#internationalization)
-* [Using Enum as a Collection](https://github.com/beerlington/classy_enum#using-enum-as-a-collection)
-* [Reference to Owning Object](https://github.com/beerlington/classy_enum#back-reference-to-owning-object)
-* [Serializing as JSON](https://github.com/beerlington/classy_enum#serializing-as-json)
-* [Special Cases](https://github.com/beerlington/classy_enum#special-cases)
-* [Built-in Model Validation](https://github.com/beerlington/classy_enum#model-validation)
-* [Formtastic Support](https://github.com/beerlington/classy_enum#formtastic-support)
+* [Example Usage](#example-usage)
+* [Internationalization](#internationalization)
+* [Using Enum as a Collection](#using-enum-as-a-collection)
+* [Default Enum Value](#default-enum-value)
+* [Reference to Owning Object](#back-reference-to-owning-object)
+* [Serializing as JSON](#serializing-as-json)
+* [Special Cases](#special-cases)
+* [Built-in Model Validation](#model-validation)
+* [Formtastic Support](#formtastic-support)
## Rails & Ruby Versions Supported
@@ -189,6 +190,22 @@ Priority.each do |priority|
end
```
+## Default Enum Value
+
+As with any ActiveRecord attribute, default values can be specified in
+the database table and will propagate to new instances. However, there
+may be times when you can't or don't want to set the default value in
+the database. For these occasions, a default value can be specified like
+so:
+
+```ruby
+class Alarm < ActiveRecord::Base
+ classy_enum_attr :priority, :default => 'medium'
+end
+
+Alarm.new.priority # => Priority::Medium
+```
+
## Back reference to owning object
In some cases you may want an enum class to reference the owning object
@@ -23,6 +23,9 @@ module ActiveRecord
#
# # Allow enum value to be blank
# classy_enum_attr :priority, :allow_blank => true
+ #
+ # # Specifying a default enum value
+ # classy_enum_attr :priority, :default => 'low'
def classy_enum_attr(attribute, options={})
enum = (options[:enum] || attribute).to_s.camelize.constantize
allow_blank = options[:allow_blank] || false

0 comments on commit f33a7dc

Please sign in to comment.