class Decidim::AuthorizationFormBuilder

A custom form builder to render AuthorizationHandler forms.

Public Instance Methods

all_fields() click to toggle source

Renders all form attributes defined by the handler.

Returns a String.

# File lib/decidim/authorization_form_builder.rb, line 11
def all_fields
  fields = public_attributes.map do |name, type|
    @template.content_tag(:div, input_field(name, type), class: "field")
  end

  safe_join(fields)
end
input(name, options = {}) click to toggle source

Renders a single attribute from the form handlers.

name - The String name of the attribute. options - An optional Hash, accepted options are:

:as - A String name with the type the field to render
:input - An optional Hash to pass to the field method.

Returns a String.

# File lib/decidim/authorization_form_builder.rb, line 27
def input(name, options = {})
  if options[:as]
    send(options[:as].to_s, name, options[:input] || {})
  else
    type = find_input_type(name.to_s)
    input_field(name, type)
  end
end

Private Instance Methods

find_input_type(name) click to toggle source
# File lib/decidim/authorization_form_builder.rb, line 56
def find_input_type(name)
  found_attribute = object.class.attribute_set.detect do |attribute|
    attribute.name.to_s == name
  end

  raise "Could not find attribute #{name} in #{object.class.name}" unless found_attribute

  found_attribute.type.primitive
end
form_attributes() click to toggle source
# File lib/decidim/authorization_form_builder.rb, line 72
def form_attributes
  object.class.attribute_set.select do |attribute|
    object.form_attributes.include?(attribute.name)
  end
end
input_field(name, type) click to toggle source
# File lib/decidim/authorization_form_builder.rb, line 38
def input_field(name, type)
  return hidden_field(name) if name.to_s == "handler_name"
  return scopes_selector if name.to_s == "scope_id"

  case type.name
  when "Date", "Time"
    date_field name
  else
    text_field name
  end
end
public_attributes() click to toggle source
# File lib/decidim/authorization_form_builder.rb, line 66
def public_attributes
  form_attributes.inject({}) do |all, attribute|
    all.update(attribute.name => attribute.type.primitive)
  end
end
scopes_selector() click to toggle source
# File lib/decidim/authorization_form_builder.rb, line 50
def scopes_selector
  return if object.user.blank?

  collection_select :scope_id, object.user.organization.scopes, :id, ->(scope) { translated_attribute(scope.name) }
end