Git Product home page Git Product logo

has_enum's Introduction

has_enum

Gem for Rails 3 app to easily handle enumeration attributes in models.

Installation and configuration

This gem has been designed for Rails 3 only.

Install gem

Insert gem 'has_enum' in your Rails 3 Gemfile. Then do $ bundle install

Usage

Models

Here's example model:

class TestModel < ActiveRecord::Base
   has_enum :category, %w( stuff things misc )
   has_enum :color   , %w( red green blue )       , :scopes        => true
   has_enum :size    , %w( small medium large )   , :query_methods => false
   has_enum :status  , [:pending, :failed, :done]
end

Query methods are available by default. For example, query methods for color enum this methods will be available:

  color_green?
  color_red?
  color_blue?

If you set :scopes => true these scopes will be available:

   color_green
   color_red
   color_blue

See sample usage in specs.

Views

    radio_button_enum(object_name, method, options = {})
    select_enum(object_name, method, options = {})

Usage with Formtastic (Monkeypatch)

If you are using another gems that inherited from formtastic the most simple method will be to add this code in your config/initializers/formtastic.rb:

    class Formtastic::SemanticFormBuilder
      def enum_input(method, options = {})
        value = @object.send(method)
        additional_params = {:as => :select, :collection => @object.class.values_for_select_tag(method)}
        self.input(method, options.merge(additional_params)).gsub(/class="select/, 'class="enum')
      end
    end

Or if you sure you don't use any the proper method is to inherit from it:

    class CustomBuilder < Formtastic::SemanticFormBuilder
      def enum_input(method, options = {})
        value = @object.send(method)
        additional_params = {:as => :select, :collection => @object.class.values_for_select_tag(method)}
        self.input(method, options.merge(additional_params)).gsub(/class="select/, 'class="enum')
      end
    end

And uncomment

    Formtastic::SemanticFormHelper.builder = CustomBuilder

Running Tests

Run the tests

rake spec

View specs are still failing. I'm working on it.

Contributing to has_enum

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2010 Andreas Korth, Konstantin Shabanov. See LICENSE.txt for further details.

has_enum's People

Contributors

etehtsea avatar juni0r avatar jenkek avatar abecciu avatar overbryd avatar

Stargazers

Bruno Frank avatar

Watchers

Bruno Frank avatar James Cloos 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.