lib/formtastic/inputs/select_input.rb in formtastic-3.1.3 vs lib/formtastic/inputs/select_input.rb in formtastic-3.1.4

- old
+ new

@@ -6,10 +6,11 @@ # for assigning many genres to a song, for example). # # This is the default input choice when: # # * the database column type is an `:integer` and there is an association (`belongs_to`) + # * the database column type is an `:integer` and there is an enum defined (`enum`) # * the database column type is a `:string` and the `:collection` option is used # * there an object with an association, but no database column on the object (`has_many`, etc) # * there is no object and the `:collection` option is used # # The flexibility of the `:collection` option (see examples) makes the :select input viable as @@ -36,10 +37,17 @@ # is customisable through the `:member_label` and `:member_value` options. When not provided, # we fall back to a list of methods to try on each object such as `:to_label`, `:name` and # `:to_s`, which are defined in the configurations `collection_label_methods` and # `collection_value_methods` (see examples below). # + # For select inputs that map to ActiveRecord `enum` attributes, Formtastic will automatically + # load in your enum options to be used as the select's options. This can be overridden with + # the `:collection` option, or augmented with I18n translations. See examples below. + # An error is raised if you try to render a multi-select with an enum, as ActiveRecord can + # only store one choice in the database. + # + # # @example Basic `belongs_to` example with full form context # # <%= semantic_form_for @post do |f| %> # <%= f.inputs do %> # <%= f.input :author, :as => :select %> @@ -122,18 +130,38 @@ # @example Exclude, include, or customize the prompt at the top of the select. Only shown if the field does not have a value. Suitable for required inputs. # <%= f.input :author, :as => :select, :prompt => false %> # <%= f.input :author, :as => :select, :prompt => true %> => <option value="">Please select</option> # <%= f.input :author, :as => :select, :prompt => "Please select an author" %> # + # @example Using ActiveRecord enum attribute with i18n translation: + # # post.rb + # class Post < ActiveRecord::Base + # enum :status => [ :active, :archived ] + # end + # # en.yml + # en: + # activerecord: + # attributes: + # post: + # statuses: + # active: I am active! + # archived: I am archived! + # # form + # <%= f.input :status, :as => :select %> # # @see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documentation of all possible options. # @see Formtastic::Inputs::CheckBoxesInput CheckBoxesInput as an alternative for `has_many` and `has_and_belongs_to_many` associations # @see Formtastic::Inputs::RadioInput RadioInput as an alternative for `belongs_to` associations # # @todo Do/can we support the per-item HTML options like RadioInput? class SelectInput include Base include Base::Collections + + def initialize(*args) + super + raise Formtastic::UnsupportedEnumCollection if collection_from_enum? && multiple? + end def to_html input_wrapping do label_html << select_html