lib/formtastic/helpers/inputs_helper.rb in formtastic-3.1.3 vs lib/formtastic/helpers/inputs_helper.rb in formtastic-3.1.4

- old
+ new

@@ -45,14 +45,10 @@ # @see Formtastic::Helpers::InputHelper#input module InputsHelper include Formtastic::Helpers::FieldsetWrapper include Formtastic::LocalizedString - # Which columns to skip when automatically rendering a form without any fields specified. - SKIPPED_COLUMNS = [:created_at, :updated_at, :created_on, :updated_on, :lock_version, :version] - - # {#inputs} creates an input fieldset and ol tag wrapping for use around a set of inputs. It can be # called either with a block (in which you can do the usual Rails form stuff, HTML, ERB, etc), # or with a list of fields (accepting all default arguments and options). These two examples # are functionally equivalent: # @@ -134,11 +130,11 @@ # <%= profile.input :twitter_name %> # <% end %> # <% end %> # <% end %> # - # {#inputs} also provides a DSL similar to `fields_for` / `semantic_fields_for` to reduce the + # {#inputs} also provides a DSL similar to `fields_for` / `semantic_fields_for` to reduce the # lines of code a little: # # <% semantic_form_for @user do |f| %> # <%= f.inputs :name, :email %> # @@ -160,11 +156,11 @@ # in the document structure and semantics (using `semantic_fields_for` otherwise). # # All options except `:name`, `:title` and `:for` will be passed down to the fieldset as HTML # attributes (id, class, style, etc). # - # When nesting `inputs()` inside another `inputs()` block, the nested content will + # When nesting `inputs()` inside another `inputs()` block, the nested content will # automatically be wrapped in an `<li>` tag to preserve the HTML validity (a `<fieldset>` # cannot be a direct descendant of an `<ol>`. # # # @option *args :for [Symbol, ActiveModel, Array] @@ -181,16 +177,16 @@ # <% semantic_form_for @post do |form| %> # <%= f.inputs %> # <% end %> # # @example Quick form: Skip one or more fields - # <%= f.inputs, :except => [:featured, :something_for_admin_only] %> - # <%= f.inputs, :except => :featured %> + # <%= f.inputs :except => [:featured, :something_for_admin_only] %> + # <%= f.inputs :except => :featured %> # # @example Short hand: Render inputs for a named set of attributes and simple associations on the model, with all default arguments and options # <% semantic_form_for @post do |form| %> - # <%= f.inputs, :title, :body, :user, :categories %> + # <%= f.inputs :title, :body, :user, :categories %> # <% end %> # # @example Block: Render inputs for attributes and simple associations with full control over arguments and options # <% semantic_form_for @post do |form| %> # <%= f.inputs do %> @@ -281,11 +277,11 @@ # <% end %> # <% end %> def inputs(*args, &block) wrap_it = @already_in_an_inputs_block ? true : false @already_in_an_inputs_block = true - + title = field_set_title_from_args(*args) html_options = args.extract_options! html_options[:class] ||= "inputs" html_options[:name] = title skipped_args = Array.wrap html_options.delete(:except) @@ -301,51 +297,51 @@ contents = fieldset_contents_from_column_list(args) args.unshift(legend) if legend.present? field_set_and_list_wrapping(*((args << html_options) << contents)) end end - + out = template.content_tag(:li, out, :class => "input") if wrap_it @already_in_an_inputs_block = wrap_it out end protected - + def default_columns_for_object cols = association_columns(:belongs_to) cols += content_columns - cols -= SKIPPED_COLUMNS + cols -= Formtastic::FormBuilder.skipped_columns cols.compact end - + def fieldset_contents_from_column_list(columns) columns.collect do |method| if @object if @object.class.respond_to?(:reflect_on_association) if (@object.class.reflect_on_association(method.to_sym) && @object.class.reflect_on_association(method.to_sym).options[:polymorphic] == true) raise PolymorphicInputWithoutCollectionError.new("Please provide a collection for :#{method} input (you'll need to use block form syntax). Inputs for polymorphic associations can only be used when an explicit :collection is provided.") end elsif @object.class.respond_to?(:associations) if (@object.class.associations[method.to_sym] && @object.class.associations[method.to_sym].options[:polymorphic] == true) raise PolymorphicInputWithoutCollectionError.new("Please provide a collection for :#{method} input (you'll need to use block form syntax). Inputs for polymorphic associations can only be used when an explicit :collection is provided.") - end - end + end + end end input(method.to_sym) end end - + # Collects association columns (relation columns) for the current form object class. Skips # polymorphic associations because we can't guess which class to use for an automatically # generated input. def association_columns(*by_associations) # @private if @object.present? && @object.class.respond_to?(:reflections) @object.class.reflections.collect do |name, association_reflection| if by_associations.present? if by_associations.include?(association_reflection.macro) && association_reflection.options[:polymorphic] != true - name + name end else name end end.compact @@ -375,13 +371,13 @@ raise ArgumentError, 'You gave :for option with a block to inputs method, ' << 'but the block does not accept any argument.' if block.arity <= 0 lambda do |f| contents = f.inputs(*args) do if block.arity == 1 # for backwards compatibility with REE & Ruby 1.8.x - block.call(f) + yield(f) else index = parent_child_index(options[:parent]) if options[:parent] - block.call(f, index) + yield(f, index) end end template.concat(contents) end else