File: simple_form.rb

package info (click to toggle)
ruby-enumerize 2.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 432 kB
  • sloc: ruby: 3,712; makefile: 6
file content (37 lines) | stat: -rw-r--r-- 1,155 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

require 'active_support/concern'

module Enumerize
  module Hooks
    module SimpleFormBuilderExtension

      def input(attribute_name, options={}, &block)
        add_input_options_for_enumerized_attribute(attribute_name, options)
        super(attribute_name, options, &block)
      end

      def input_field(attribute_name, options={})
        add_input_options_for_enumerized_attribute(attribute_name, options)
        super(attribute_name, options)
      end

      private

      def add_input_options_for_enumerized_attribute(attribute_name, options)
        enumerized_object = convert_to_model(object)
        klass = enumerized_object.class

        if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[attribute_name])
          options[:collection] ||= attr.options

          if attr.kind_of?(Enumerize::Multiple) && options[:as] != :check_boxes
            options[:input_html] = options.fetch(:input_html, {}).merge(:multiple => true)
          end
        end
      end
    end
  end
end

::SimpleForm::FormBuilder.send :prepend, Enumerize::Hooks::SimpleFormBuilderExtension