Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tweaking docs.

  • Loading branch information...
commit b19506ebe383c37b9a30a0fea51fd252b3f87d77 1 parent 7dc601e
@kenn authored
Showing with 24 additions and 28 deletions.
  1. +23 −8 README.md
  2. +1 −20 lib/enum_accessor.rb
View
31 README.md
@@ -12,25 +12,26 @@ Add this line to your application's Gemfile.
gem 'enum_accessor'
```
-Define:
+Add an integer column.
```ruby
-class User < ActiveRecord::Base
- enum_accessor :gender, [ :female, :male ]
+create_table :users do |t|
+ t.column :gender, :integer, default: 0
end
```
-Migration:
+Define `enum_accessor` in a model class.
```ruby
-create_table :users do |t|
- t.column :gender, :integer, default: 0
+class User < ActiveRecord::Base
+ enum_accessor :gender, [ :female, :male ]
end
```
-Now
+And now you have a set of methods and constants.
```ruby
+User::GENDERS # => { "female" => 0, "male" => 1 }
User.genders # => { :female => 0, :male => 1 }
user = User.new
@@ -50,7 +51,7 @@ There are times when it makes more sense to manually pick particular integers fo
Just pass a hash with coded integer values.
```ruby
-enum_accessor :status, ok: 200, bad_request: 400, not_found: 404, internal_server_error: 500, service_unavailable: 503
+enum_accessor :status, ok: 200, not_found: 404, internal_server_error: 500
```
## Scoping query
@@ -61,6 +62,20 @@ To retrieve internal integer values for query, use `User.genders`.
User.where(gender: User.genders(:female))
```
+## Validations
+
+You can pass custom validation options to `validates_inclusion_of`.
+
+```ruby
+enum_accessor :status, [ :on, :off ], validation_options: { message: "incorrect status" }
+```
+
+Or skip validation entirely.
+
+```ruby
+enum_accessor :status, [ :on, :off ], validate: false
+```
+
## i18n
EnumAccessor supports i18n just as ActiveModel does.
View
21 lib/enum_accessor.rb
@@ -6,25 +6,6 @@ module EnumAccessor
extend ActiveSupport::Concern
module ClassMethods
- # enum_accessor encapsulates a validates_inclusion_of and automatically gives you a
- # few more goodies automatically.
- #
- # class Computer < ActiveRecord:Base
- # enum_accessor :status, [ :on, :off ], validation_options: { message: "incorrect status" }
- #
- # # Optionally with a message to replace the default one
- # # enum_accessor :status, [ :on, :off ]
- #
- # #...
- # end
- #
- # This will give you a few things:
- #
- # - add a validates_inclusion_of with a simple error message ("invalid #{field}") or your custom message
- # - define the following query methods, in the name of expressive code:
- # - status_on?
- # - status_off?
- # - define the STATUSES constant, which contains the acceptable values
def enum_accessor(field, enums, options={})
# Normalize arguments
field = field.to_s
@@ -79,7 +60,7 @@ def self.#{field.pluralize}(*args)
end
EOS
- # Human-friendly view
+ # Human-friendly print
define_method("human_#{field}") do
self.class.human_enum_accessor(field, self.send(field))
end
Please sign in to comment.
Something went wrong with that request. Please try again.