Sha256: 7e6268e2b61ceb98c03bf2eed78643edd9296e78b6da9624cb80b0d047ea3ba1

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require 'simple_enum'

module SimpleEnum

  # Enables support for mongoid, also automatically creates the
  # requested field.
  #
  #   class Person
  #     include Mongoid::Document
  #     include SimpleEnum::Mongoid
  #
  #     field :name
  #     as_enum :gender, [:female, :male]
  #   end
  #
  # When no field is requested:
  #
  #   field :gender_cd, :type => Integer
  #   as_enum :gender, [:female, :male], :field => false
  #
  # or custom field options (like e.g. type want to be passed):
  #
  #   as_enum :gender, [:female, :male], :field => { :type => Integer }
  #
  module Mongoid
    extend ActiveSupport::Concern

    included do
      # create class level methods
      class_attribute :simple_enum_definitions, :instance_writer => false, :instance_reader => false
    end

    module ClassMethods
      include SimpleEnum::ClassMethods

      # Wrap method chain to create mongoid field and additional
      # column options
      def as_enum_with_mongoid(enum_cd, values, options = {})
        options = SimpleEnum.default_options.merge({ :column => "#{enum_cd}_cd" }).merge(options)

        # forward custom field options
        field_options = options.delete(:field)
        field(options[:column], field_options.is_a?(Hash) ? field_options : {}) unless field_options === false

        # call original as_enum method
        as_enum_without_mongoid(enum_cd, values, options)
      end
      alias_method_chain :as_enum, :mongoid
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_enum-1.6.3 lib/simple_enum/mongoid.rb
simple_enum-1.6.2 lib/simple_enum/mongoid.rb
simple_enum-1.6.1 lib/simple_enum/mongoid.rb