Skip to content

Commit

Permalink
Updating docs for default values
Browse files Browse the repository at this point in the history
  • Loading branch information
beerlington committed Feb 8, 2013
1 parent 41cf577 commit f33a7dc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -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
Expand Down
33 changes: 25 additions & 8 deletions README.md
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/classy_enum/active_record.rb
Expand Up @@ -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
Expand Down

0 comments on commit f33a7dc

Please sign in to comment.