Skip to content

Commit

Permalink
Update README [skip CI]
Browse files Browse the repository at this point in the history
  • Loading branch information
lucascaton committed Aug 21, 2017
1 parent 09b09b9 commit ac44f62
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,25 @@ The `sort_by` method accepts one of the following values:

## Using enumerations

The cool part is that you can use these enumerations with any class, be it an `ActiveRecord`
instance or not.
The cool part is that you can use these enumerations with any class:

```ruby
# ActiveRecord instance
class Person < ApplicationRecord
has_enumeration_for :relationship_status
end
```

```ruby
# Non-ActiveRecord instance
class Person
extend EnumerateIt
attr_accessor :relationship_status

has_enumeration_for :relationship_status
end
```

For non-Rails apps, add `extend EnumerateIt` to your class.

> **Note:** **EnumerateIt** will try to load an enumeration class based on the camelized attribute
> name. If you have a different name, you can specify it by using the `with` option:
>
Expand Down Expand Up @@ -226,7 +232,7 @@ This will create:
* A helper method for each enumeration option, if you pass the `create_helpers` option as `true`:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, with: RelationshipStatus, create_helpers: true
end

Expand All @@ -244,7 +250,7 @@ This will create:
option. This can be useful when two or more of the enumerations used share the same constants:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status,
with: RelationshipStatus, create_helpers: { prefix: true }
end
Expand Down Expand Up @@ -274,12 +280,12 @@ This will create:

class Single
def saturday_night
'Party Hard!'
'Party hard!'
end
end
end

class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status,
with: RelationshipStatus, create_helpers: { polymorphic: true }
end
Expand All @@ -291,13 +297,13 @@ This will create:

p.relationship_status = RelationshipStatus::SINGLE
p.relationship_status_object.saturday_night
#=> 'Party Hard!'
#=> 'Party hard!'
```

You can also change the suffix `_object`, using the `suffix` option:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status,
with: RelationshipStatus, create_helpers: { polymorphic: { suffix: '_mode' } }
end
Expand All @@ -319,7 +325,7 @@ This will create:
* A scope method for each enumeration option if you pass the `create_scopes` option as `true`:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, with: RelationshipStatus, create_scopes: true
end

Expand All @@ -330,7 +336,7 @@ This will create:
The `:create_scopes` also accepts `prefix` option.

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status,
with: RelationshipStatus, create_scopes: { prefix: true }
end
Expand All @@ -342,7 +348,7 @@ This will create:
`validates_inclusion_of`):

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, with: RelationshipStatus
end

Expand All @@ -357,7 +363,7 @@ This will create:
`validates_presence_of` and you pass the `required` options as `true`):

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, required: true
end

Expand All @@ -371,7 +377,7 @@ This will create:
If you pass the `skip_validation` option as `true`, it will not create any validations:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, with: RelationshipStatus, skip_validation: true
end

Expand All @@ -380,8 +386,7 @@ This will create:
#=> true
```

Remember that you can add validations to any kind of class and not only to those derived from
`ActiveRecord::Base`.
Remember that you can add validations to any kind of class and not only `ActiveRecord` ones.

## FAQ

Expand Down

0 comments on commit ac44f62

Please sign in to comment.