Readme.md in enumify-0.2.0 vs Readme.md in enumify-1.0.0
- old
+ new
@@ -10,29 +10,18 @@
gem 'enumify'
```
## How to use
-Just call the `enum` function in any ActiveRecord object, the function accepts the field name as the first variable and the possible values as an array
+Just call the `enumify` function in any ActiveRecord object, the function accepts the field name as the first variable and the possible values as an array
```ruby
class Event < ActiveRecord::Base
- enum :status, [:available, :canceled, :completed]
-end
-```
-
-## Rails 4.1+
-
-You can also instantiate a _Enumify_ enum by calling the `enumify` method instead of `enum`.
-This is especially helpful when your app is running Rail 4.1 or better which has it's own built in enum (although not as good)
-
-```ruby
-class Event < ActiveRecord::Base
enumify :status, [:available, :canceled, :completed]
end
```
-
+
## Usage
After that you get several autogenerated commands to use with the enum
```ruby
@@ -56,24 +45,24 @@
#### :allow_nil
By default the enum field does not support a nil value. In order to allow nil values add the `allow_nil` option (similar to the Rails validation option).
```ruby
class Event < ActiveRecord::Base
- enum :status, [:available, :canceled, :completed], :allow_nil => true
+ enumify :status, [:available, :canceled, :completed], :allow_nil => true
end
Event.create! # Is valid and does not throw an exception.
```
#### :prefix
-By default all enum values are available as scopes, bang and query methods based on the value.
-You can add a prefix for the enum values in order to differentiate different enums on the same object.
+By default all enum values are available as scopes, bang and query methods based on the value.
+You can add a prefix for the enum values in order to differentiate different enums on the same object.
```ruby
class Event < ActiveRecord::Base
- enum :status, [:available, :canceled, :completed], :prefix => true
- enum :subtype, [:company, :personal], :prefix => 'type'
+ enumify :status, [:available, :canceled, :completed], :prefix => true
+ enumify :subtype, [:company, :personal], :prefix => 'type'
end
event.available? # Not available anymore
event.status_available? # when prefix true
event.type_company? # you can set a specific name for your prefix
@@ -83,13 +72,13 @@
By default, a constant is created on the class, containing the enum values.
You can remove the constant by passing a falsy value (i.e: `nil`, `false`) or rename it by passing a symbol or a string with a different name.
```ruby
class Event < ActiveRecord::Base
- enum :status, [:available, :canceled, :completed]
- enum :without_const, [:foo, :bar], :constant => false
- enum :custom_name, [:a, :b, :c], :constant => :special_name
+ enumify :status, [:available, :canceled, :completed]
+ enumify :without_const, [:foo, :bar], :constant => false
+ enumify :custom_name, [:a, :b, :c], :constant => :special_name
end
event::STATUSES # returns [:available, :canceled, :completed]
event::WITHOUT_CONST # raises NameError
event::SPECIAL_NAME # returns [:a, :b, :c]. Note the name was not pluralized.
@@ -102,10 +91,10 @@
All you need to do is add a x_changed method in your class and the enumify will call it
```ruby
class Event < ActiveRecord::Base
- enum :status, [:available, :canceled, :completed]
+ enumify :status, [:available, :canceled, :completed]
def status_changed(old, new)
puts "status changed from #{old} to #{new}"
end
end