Skip to content

Commit

Permalink
Tweaking docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenn committed Sep 17, 2012
1 parent 7dc601e commit b19506e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
31 changes: 23 additions & 8 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@ Add this line to your application's Gemfile.
gem 'enum_accessor' gem 'enum_accessor'
``` ```


Define: Add an integer column.


```ruby ```ruby
class User < ActiveRecord::Base create_table :users do |t|
enum_accessor :gender, [ :female, :male ] t.column :gender, :integer, default: 0
end end
``` ```


Migration: Define `enum_accessor` in a model class.


```ruby ```ruby
create_table :users do |t| class User < ActiveRecord::Base
t.column :gender, :integer, default: 0 enum_accessor :gender, [ :female, :male ]
end end
``` ```


Now And now you have a set of methods and constants.


```ruby ```ruby
User::GENDERS # => { "female" => 0, "male" => 1 }
User.genders # => { :female => 0, :male => 1 } User.genders # => { :female => 0, :male => 1 }


user = User.new user = User.new
Expand All @@ -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. Just pass a hash with coded integer values.


```ruby ```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 ## Scoping query
Expand All @@ -61,6 +62,20 @@ To retrieve internal integer values for query, use `User.genders`.
User.where(gender: User.genders(:female)) 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 ## i18n


EnumAccessor supports i18n just as ActiveModel does. EnumAccessor supports i18n just as ActiveModel does.
Expand Down
21 changes: 1 addition & 20 deletions lib/enum_accessor.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ module EnumAccessor
extend ActiveSupport::Concern extend ActiveSupport::Concern


module ClassMethods 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={}) def enum_accessor(field, enums, options={})
# Normalize arguments # Normalize arguments
field = field.to_s field = field.to_s
Expand Down Expand Up @@ -79,7 +60,7 @@ def self.#{field.pluralize}(*args)
end end
EOS EOS


# Human-friendly view # Human-friendly print
define_method("human_#{field}") do define_method("human_#{field}") do
self.class.human_enum_accessor(field, self.send(field)) self.class.human_enum_accessor(field, self.send(field))
end end
Expand Down

0 comments on commit b19506e

Please sign in to comment.