module Sequel::Plugins::Forme::SequelForm

  1. lib/sequel/plugins/forme.rb

This module extends all Forme::Form instances that use a Sequel::Model instance as the form's obj.

Methods

Public Instance

  1. form
  2. humanize
  3. subform

Public Instance methods

form (attr={}, &block)

Use the post method by default for Sequel forms, unless overridden with the :method attribute.

[show source]
   # File lib/sequel/plugins/forme.rb
23 def form(attr={}, &block)
24   attr[:class] = ::Forme.merge_classes(attr[:class], "forme", obj.forme_namespace)
25   super(attr, &block)
26 end
humanize (s)

Call humanize on a string version of the argument if String#humanize exists. Otherwise, do some monkeying with the string manually.

[show source]
   # File lib/sequel/plugins/forme.rb
31 def humanize(s)
32   s = s.to_s
33   s.respond_to?(:humanize) ? s.humanize : s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
34 end
subform (association, opts={}, &block)

Handle nested association usage. The association should be a name of the association for the form's obj. Inside the block, calls to the input and inputs methods for the receiver treat the associated object as the recevier's obj, using name and id attributes that work with the Sequel nested_attributes plugin.

The following options are currently supported:

:inputs

Automatically call inputs with the given values. Using this, it is not required to pass a block to the method, though it will still work if you do.

:inputs_opts

When using the :grid option, this allows you to specify options to pass to the table InputsWrapper.

:legend

Overrides the default :legend used (which is based on the association name). You can also use a proc as the value, which will called with each associated object (and the position in the associated object already for *_to_many associations), and should return the legend string to use for that object.

:grid

Sets up a table with one row per associated object, and one column per field.

:labels

When using the :grid option, override the labels that would be created via the :inputs option. If you are not providing an :inputs option or are using a block with additional inputs, you should specify this option.

:skip_primary_key

Skip adding a hidden primary key field for existing objects.

[show source]
    # File lib/sequel/plugins/forme.rb
 61 def subform(association, opts={}, &block)
 62   nested_obj = opts.has_key?(:obj) ? opts[:obj] : obj.send(association)
 63   ref = obj.class.association_reflection(association)
 64   multiple = ref.returns_array?
 65   grid = opts[:grid]
 66   ns = "#{association}_attributes"
 67 
 68   contents = proc do
 69     send(multiple ? :each_obj : :with_obj, nested_obj, ns) do |no, i|
 70       emit(input(ref.associated_class.primary_key, :type=>:hidden, :label=>nil, :wrapper=>nil)) unless no.new? || opts[:skip_primary_key]
 71       options = opts.dup
 72       if grid
 73         options.delete(:legend)
 74       else
 75         if options.has_key?(:legend)
 76           if options[:legend].respond_to?(:call)
 77             options[:legend] = multiple ? options[:legend].call(no, i) : options[:legend].call(no)
 78           end
 79         else
 80           if multiple
 81             options[:legend] = humanize("#{obj.model.send(:singularize, association)} ##{i+1}")
 82           else
 83             options[:legend] = humanize(association)
 84           end
 85         end
 86       end
 87       options[:subform] = true
 88       _inputs(options[:inputs]||[], options, &block)
 89     end
 90   end
 91   
 92   if grid
 93     labels = opts.fetch(:labels){opts[:inputs].map{|l, *| humanize(l)} if opts[:inputs]}
 94     legend = opts.fetch(:legend){humanize(association)}
 95     inputs_opts = opts[:inputs_opts] || {}
 96     inputs(inputs_opts.merge(:inputs_wrapper=>:table, :nested_inputs_wrapper=>:tr, :wrapper=>:td, :labeler=>nil, :labels=>labels, :legend=>legend), &contents)
 97   else
 98     contents.call
 99   end
100   nil
101 end