Sha256: bbf5da62e1b57a2849adb12f33eba9306d7042e1e24797e9262ce39cb066326c

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

require 'simple_form/version'

module ActiveEnum
  module FormHelpers
    module SimpleForm
      module BuilderExtension

        def find_custom_type(attribute_name)
          return :enum if object.class.active_enum_for(attribute_name) if object.class.respond_to?(:active_enum_for)
          super
        end

      end
    end
  end
end

class ActiveEnum::FormHelpers::SimpleForm::EnumInput < ::SimpleForm::Inputs::CollectionSelectInput
  def initialize(*args)
    super
    raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.active_enum_for(attribute_name)
    input_options[:collection] = enum.to_select
  end
end

class ActiveEnum::FormHelpers::SimpleForm::GroupedEnumInput < ::SimpleForm::Inputs::GroupedCollectionSelectInput
  def initialize(*args)
    super
    raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.active_enum_for(attribute_name)

    input_options[:collection] = enum.to_grouped_select(input_options[:group_by])
    input_options[:group_method] = :last
  end
end

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

  map_type :enum, to: ActiveEnum::FormHelpers::SimpleForm::EnumInput
  map_type :grouped_enum, to: ActiveEnum::FormHelpers::SimpleForm::GroupedEnumInput

  alias_method :collection_enum, :collection_select
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
active_enum-1.2.1 lib/active_enum/form_helpers/simple_form.rb
active_enum-1.2.0 lib/active_enum/form_helpers/simple_form.rb
active_enum-1.1.0 lib/active_enum/form_helpers/simple_form.rb
active_enum-1.0.0 lib/active_enum/form_helpers/simple_form.rb
active_enum-1.0.0.rc8 lib/active_enum/form_helpers/simple_form.rb
active_enum-1.0.0.rc7 lib/active_enum/form_helpers/simple_form.rb
active_enum-1.0.0.rc6 lib/active_enum/form_helpers/simple_form.rb
active_enum-1.0.0.rc5 lib/active_enum/form_helpers/simple_form.rb
active_enum-1.0.0.rc4 lib/active_enum/form_helpers/simple_form.rb
active_enum-1.0.0.rc3 lib/active_enum/form_helpers/simple_form.rb