Module: Sequel::Plugins::Forme::SequelForm
- Included in:
- Form
- Defined in:
- lib/sequel/plugins/forme.rb
Overview
This module extends all Forme::Form instances that use a Sequel::Model instance as the form’s obj.
Instance Method Summary collapse
-
#button(opts = {}) ⇒ Object
If the form has the :formactions option and the button has the formaction option or attribute, append the action and method for this button to the formactions.
-
#form(attr = {}, &block) ⇒ Object
Use the post method by default for Sequel forms, unless overridden with the :method attribute.
-
#humanize(s) ⇒ Object
Call humanize on a string version of the argument if String#humanize exists.
-
#subform(association, opts = {}, &block) ⇒ Object
Handle nested association usage.
Instance Method Details
#button(opts = {}) ⇒ Object
If the form has the :formactions option and the button has the formaction option or attribute, append the action and method for this button to the formactions. This is used by the Roda forme_route_csrf and forme_set plugins so that formaction can work as expected.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sequel/plugins/forme.rb', line 25 def (opts={}) if opts.is_a?(Hash) && (formactions = self.opts[:formactions]) && (formaction = opts[:formaction] || ((attr = opts[:attr]) && (attr[:formaction] || attr['formaction']))) formmethod = opts[:formmethod] || ((attr = opts[:attr]) && (attr[:formmethod] || attr['formmethod'])) || ((attr = form_tag_attributes) && (attr[:method] || attr['method'])) formactions << [formaction, formmethod] end super end |
#form(attr = {}, &block) ⇒ Object
Use the post method by default for Sequel forms, unless overridden with the :method attribute.
38 39 40 41 |
# File 'lib/sequel/plugins/forme.rb', line 38 def form(attr={}, &block) attr[:class] = ::Forme.merge_classes(attr[:class], "forme", obj.forme_namespace) super(attr, &block) end |
#humanize(s) ⇒ Object
Call humanize on a string version of the argument if String#humanize exists. Otherwise, do some monkeying with the string manually.
46 47 48 49 |
# File 'lib/sequel/plugins/forme.rb', line 46 def humanize(s) s = s.to_s s.respond_to?(:humanize) ? s.humanize : s.gsub(/_id$/, "").gsub(/_/, " ").capitalize end |
#subform(association, opts = {}, &block) ⇒ Object
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. Returns the HTML generated by the subform.
The following options are currently supported:
- :inputs
-
Automatically call
inputswith 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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sequel/plugins/forme.rb', line 77 def subform(association, opts={}, &block) content_added do nested_obj = opts.has_key?(:obj) ? opts[:obj] : obj.send(association) ref = obj.class.association_reflection(association) multiple = ref.returns_array? grid = opts[:grid] ns = "#{association}_attributes" send(multiple ? :each_obj : :with_obj, nested_obj, ns) do |no, i| input(ref.associated_class.primary_key, :type=>:hidden, :label=>nil, :wrapper=>nil) unless no.new? || opts[:skip_primary_key] end contents = proc do send(multiple ? :each_obj : :with_obj, nested_obj, ns) do |no, i| = opts.dup if grid .delete(:legend) else if .has_key?(:legend) if [:legend].respond_to?(:call) [:legend] = multiple ? [:legend].call(no, i) : [:legend].call(no) end else if multiple [:legend] = humanize("#{obj.model.send(:singularize, association)} ##{i+1}") else [:legend] = humanize(association) end end end [:subform] = true inputs([:inputs]||[], , &block) end end if grid labels = opts.fetch(:labels){opts[:inputs].map{|l,| humanize(l)} if opts[:inputs]} legend = opts.fetch(:legend){humanize(association)} inputs_opts = opts[:inputs_opts] || {} inputs(inputs_opts.merge(:inputs_wrapper=>:table, :nested_inputs_wrapper=>:tr, :wrapper=>:td, :labeler=>nil, :labels=>labels, :legend=>legend), &contents) else contents.call end end end |