Sha256: 8f06eb3b7631214b4c16ec2e6795daa3e87b034589f484b88fa297abe365c8bb

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

module ActiveEnum
  module FormHelpers
    module SimpleForm
      module BuilderExtension

        def default_input_type_with_active_enum(*args, &block)
          return :enum if (args.last.is_a?(Hash) ? args.last[:as] : @options[:as]).nil? &&
                          object.class.respond_to?(:active_enum_for) &&
                          object.class.active_enum_for(args.first || @attribute_name)
          default_input_type_without_active_enum(*args, &block)
        end

      end

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

        def initialize(*args)
          super
          raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.active_enum_for(attribute_name)
          if respond_to?(:input_options)
            input_options[:collection] = enum.to_select
          else
            @builder.options[:collection] = enum.to_select
          end
        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
  alias_method_chain :default_input_type, :active_enum
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
active_enum-0.9.7 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.9.6 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.9.5 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.9.4 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.9.3 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.9.1 lib/active_enum/form_helpers/simple_form.rb