Skip to content

Commit

Permalink
Update README to better explain owner reference
Browse files Browse the repository at this point in the history
The owner reference is available by default as `owner`, but the readme
did not make this clear.
  • Loading branch information
beerlington committed Jun 13, 2012
1 parent 31b9308 commit 563852e
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions README.md
Expand Up @@ -132,32 +132,52 @@ The enum field works like any other model attribute. It can be mass-assigned usi

## Back reference to owning object

In some cases you may want an enum class to be able to reference the owning object (an instance of the active record model). Think of it as a `belongs_to` relationship, where the enum can reference its owning object.
In some cases you may want an enum class to reference the owning object
(an instance of the active record model). Think of it as a `belongs_to`
relationship, where the enum can reference its owning object.

In order to create the back reference, you must declare how you wish to refer to the owner using the `owner` class method.
By default, the back reference can be called using `owner`.
If you want to refer to the owner by a different name, you must explicitly declare
the owner name in the classy_enum parent class using the `owner` class method.

For example:
Example using the default `owner` method:

```ruby
class Priority < ClassyEnum::Base
enum_classes :low, :medium, :high
owner :alarm
end

class PriorityLow < Priority
...
# low and medium subclasses omitted
...

class PriorityHigh < Priority
def send_email?
owner.enabled?
end
end
```

class PriorityMedium < Priority
Example where the owner reference is explicitly declared:

```ruby
class Priority < ClassyEnum::Base
enum_classes :low, :medium, :high
owner :alarm
end

...
# low and medium subclasses omitted
...

class PriorityHigh < Priority
def send_email?
alarm.enabled?
end
end
```

In the above example, high priority alarms are only emailed if the owning alarm is enabled.
In the above examples, high priority alarms are only emailed if the owning alarm is enabled.

```ruby
@alarm = Alarm.create(:priority => :high, :enabled => true)
Expand Down

0 comments on commit 563852e

Please sign in to comment.