module Conjoin::FormBuilder

Constants

INPUTS

Public Class Methods

setup(app) click to toggle source
# File lib/conjoin/form_builder.rb, line 11
def self.setup app
  require 'conjoin/mab'

  app.plugin Conjoin::Csrf
  # Dir["#{File.dirname(__FILE__)}/plugin/inputs/**/*.rb"].each  { |rb| require rb  }
  INPUTS.each do |input|
    require_relative "inputs/#{input}"
  end

  Dir["#{app.root}/app/inputs/**/*.rb"].each  { |rb| require rb  }
end

Public Instance Methods

form_for(record, options = {}) click to toggle source
# File lib/conjoin/form_builder.rb, line 23
def form_for record, options = {}, &block
  raise ArgumentError, "Missing block" unless block_given?

  if as = options.delete(:as)
    model_name = as
  elsif record.is_form?
    model_name = record.class.model_name.to_s.gsub(/\w+::/, '').gsub(/Form$/, '').underscore
  else
    model_name = record.class.model_name.singular
  end

  fields = Fields.new self, [model_name], record, block

  form_options = {
    class: 'form-horizontal',
    role: 'form',
    method: 'post',
    novalidate: 'true',
    remote: true,
    action: options.delete(:url) || "/" + record.class.model_name.plural
  }.merge! options

  if form_options.delete(:remote)
    form_options['data-remote'] = 'true'
  end

  mab do
    form form_options do
      input type: 'hidden', name: '_method', value: (record.id ? 'patch' : 'post')
      text! csrf_tag
      text! fields.render
    end
  end
end