Giter VIP home page Giter VIP logo

enumerize's Introduction

Enumerize TravisCI Gemnasium

Enumerated attributes with I18n and ActiveRecord/Mongoid support

Installation

Add this line to your application's Gemfile:

gem 'enumerize'

And then execute:

$ bundle

Or install it yourself as:

$ gem install enumerize

Usage

Basic:

class User
  extend Enumerize

  enumerize :sex, in: [:male, :female]
end

ActiveRecord:

class User < ActiveRecord::Base
  extend Enumerize

  enumerize :sex, in: [:male, :female]

  enumerize :role, in: [:user, :admin], default: :user
end

Mongoid:

class User
  include Mongoid::Document
  extend Enumerize

  field :role
  enumerize :role, in: [:user, :admin], default: :user
end

I18n:

en:
  enumerize:
    user:
      sex:
        male: "Male"
        female: "Female"

or if you use sex attribute across several models you can use this:

en:
  enumerize:
    sex:
      male: "Male"
      female: "Female"

get attribute value:

@user.sex_text # or @user.sex.text

get all values for enumerized attribute:

User.sex.values # or User.enumerized_attributes[:sex].values

use it with forms:

<%= form_for @user do |f| %>
  <%= f.select :sex, User.sex.options %>
<% end %>

Boolean methods:

user.sex = :male
user.sex.male? #=> true
user.sex.female? #=> false

Predicate methods:

class User
  extend Enumerize

  enumerize :sex, in: %w(male female), predicates: true
end

user = User.new

user.male?   # => false
user.female? # => false

user.sex = 'male'

user.male?   # => true
user.female? # => false

Using prefix:

class User
  extend Enumerize

  enumerize :sex, in: %w(male female), predicates: { prefix: true }
end

user = User.new
user.sex = 'female'
user.sex_female? # => true

Use :only and :except options to specify what values create predicate methods for.

To make some attributes shared across different classes it's possible to define them in a separate module and then include it into classes:

module PersonEnumerations
  extend Enumerize

  enumerize :sex, in: %w[male female]
end

class Person
  include PersonEnumerations
end

class User
  include PersonEnumerations
end

It's also possible to store enumerized attribute value using custom values (e.g. integers). You can pass a hash as :in option to achieve this:

class User < ActiveRecord::Base
  extend Enumerize

  enumerize :role, in: {:user => 1, :admin => 2}
end

ActiveRecord scopes:

class User < ActiveRecord::Base
  extend Enumerize
  enumerize :sex, :in => [:male, :female], scope: true
  enumerize :status, :in => { active: 1, blocked: 2 }, scope: :having_status
end

User.with_sex(:female)
# SELECT "users".* FROM "users" WHERE "users"."sex" IN ('female')

User.having_status(:blocked).with_sex(:male, :female)
# SELECT "users".* FROM "users" WHERE "users"."status" IN (2) AND "users"."sex" IN ('male', 'female')

Array-like attributes with plain ruby objects:

class User
  extend Enumerize

  enumerize :interests, in: [:music, :sports], multiple: true
end

user = User.new
user.interests << :music
user.interests << :sports

and with ActiveRecord:

class User < ActiveRecord::Base
  extend Enumerize

  serialize :interests, Array
  enumerize :interests, in: [:music, :sports], multiple: true
end

SimpleForm

If you are using SimpleForm gem you don't need to specify input type (:select by default) and collection:

<%= simple_form_for @user do |f| %>
  <%= f.input :sex %>
<% end %>

and if you want it as radio buttons:

<%= simple_form_for @user do |f| %>
  <%= f.input :sex, :as => :radio_buttons %>
<% end %>

Formtastic

If you are using Formtastic gem you also don't need to specify input type (:select by default) and collection:

<%= semantic_form_for @user do |f| %>
  <%= f.input :sex %>
<% end %>

and if you want it as radio buttons:

<%= semantic_form_for @user do |f| %>
  <%= f.input :sex, :as => :radio %>
<% end %>

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

enumerize's People

Contributors

alexgb avatar cgunther avatar dreamfall avatar jimryan avatar laserlemon avatar lest avatar nashby avatar ono avatar ukstv avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.