Sha256: 308cdfeb3461cc66786c0099810f55d00b3611cfaa978a269042f0e6ab7911cb

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

module ActiveEnum
  module FormHelpers
    module SimpleForm
      module BuilderExtension
        def self.included(base)
          base.alias_method_chain :default_input_type, :active_enum
        end

        def default_input_type_with_active_enum
          return :enum if @options[:as].nil? &&
            object.class.respond_to?(:active_enum_for) &&
            object.class.active_enum_for(attribute_name)
          default_input_type_without_active_enum
        end
      end

      class EnumInput < ::SimpleForm::Inputs::CollectionInput

        def initialize(builder)
          super
          raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.active_enum_for(attribute_name)
          builder.options[:collection] = enum.to_select
        end
        
      end
    end
  end
end

SimpleForm::FormBuilder.class_eval do
  include ActiveEnum::FormHelpers::SimpleForm::BuilderExtension

  map_type :enum, :to => ActiveEnum::FormHelpers::SimpleForm::EnumInput
  alias_method :collection_enum, :collection_select
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_enum-0.9.0 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.8.2 lib/active_enum/form_helpers/simple_form.rb