Sha256: 8a1214b0516499a5d649e7f1476b007dd552410e61bd2ffff9f072e2d5600e65

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

require 'simple_form/version'

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

      module InputExtension

        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

if SimpleForm::VERSION < '2.0.0'
  class ActiveEnum::FormHelpers::SimpleForm::EnumInput < ::SimpleForm::Inputs::CollectionInput
    include ActiveEnum::FormHelpers::SimpleForm::InputExtension
  end
else
  class ActiveEnum::FormHelpers::SimpleForm::EnumInput < ::SimpleForm::Inputs::CollectionSelectInput
    include ActiveEnum::FormHelpers::SimpleForm::InputExtension
  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

5 entries across 5 versions & 1 rubygems

Version Path
active_enum-0.9.12 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.9.11 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.9.10 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.9.9 lib/active_enum/form_helpers/simple_form.rb
active_enum-0.9.8 lib/active_enum/form_helpers/simple_form.rb