Sha256: 5283a5890283624d6af004607ac3981a37a3179ddee2d0a9f7e349ebc9bfa348

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# Adds the @enum_type@ method to a model.
#
# @example Basic usage
#   class MyModel < ActiveRecord::Base
#     extend EnumType
#     enum_type :status, %w( open closed flagged deleted )
#   end

module EnumType

  # Defines a field whose type is an enumeration. Values are stored as strings
  # in the backend (or your database's enumerated type), however in the Rails
  # level they are represented as symbols.
  #
  # @overload enum_type(field, ..., options={})
  #   @param [Symbol] field An enumerated field.
  #   @param [Hash] options A hash of options.
  #   @option options [true, false] :allow_nil (false) If @true@, a nil value
  #   is allowed.
  #   @option options [Array<String>] :values If given, restricts valid values
  #   to those in the given array.
  #
  # @example Enumerated field with restricted types
  #   enum_type :color, values: %w( red orange yellow green blue purple )

  def enum_type(*fields)
    options = fields.extract_options!
    fields.each do |field|
      define_method field do
        value = read_attribute(field)
        value ? value.to_sym : value
      end

      define_method :"#{field}=" do |value|
        write_attribute field, value.try(:to_s)
      end

      validates_presence_of(field) unless options[:allow_nil]
      validates_inclusion_of(field, in: options[:values]) if options[:values]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enum_type-1.0.0 lib/enum_type.rb