Class: ARFormWidget

Inherits:
FormWidget show all
Includes:
Cuca::FormElements, Cuca::Generator::Markaby
Defined in:
lib/cuca/stdlib/old_arform.rb,
lib/cuca/stdlib/arform.rb

Overview

Form’s for ActiveRecord

AR Form build a form by providing one model of ActiveRecord. Likly that you want to overwrite the form method to run a custom layout.

This Widget will call <form_name>_submit(model) if Form is submitted and validation passed. You still have to ::save the model.

Example:

ARForm('user_edit', User.find_by_username('bones'), 
                   :disable_on_update => ['username', 'created'])

Instance Method Summary collapse

Methods included from Cuca::Generator::Markaby

#mab, #mabtext

Methods inherited from FormWidget

#before_validate, #get_default_variable, #get_form_variables, posted?, #posted?, #setup, #v

Methods inherited from Cuca::Widget

#app, #cgi, #clear, clear_hints, #content, #content=, #controller, define_attr_method, #escape, #escapeHTML, #get_assigns, #hints, #initialize, #log, #params, #query_parameters, #request_method, #request_parameters, run_attr_method, #session, #to_s, #unescape, #unescapeHTML

Constructor Details

This class inherits a constructor from Cuca::Widget

Instance Method Details

#formObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cuca/stdlib/arform.rb', line 136

def form
  mab do
     FormErrors(@form_errors)
     fe_formstart
     table do
        @model.class.columns.each do |col|
           next if field_hidden?(col.name)
           tr { td { col.name }; td { fe(col.type, col.name) } }
        end
        tr { td {} ; td { fe_submit }}
     end
     fe_formend
  end
end

#on_submitObject

On submit will pass the model to the callback on the controller



46
47
48
49
# File 'lib/cuca/stdlib/arform.rb', line 46

def on_submit
 controller.send(@form_name+'_submit', @model) unless controller.nil?
 clear
end

#output(form_name, model, options = {}) ⇒ Object

valid options

  • :disabled_on_create => [‘field_name_1’, ‘field_name_2’, ..]

    switch off fields on new records
    
  • :diabled_on_update => [‘field_name_1’, ‘field_name_2’, ..]

    switch off fields on existing records
    


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cuca/stdlib/arform.rb', line 32

def output(form_name, model, options = {})
  @model = model
  @disabled_on_update = options[:disabled_on_update] || []
  @disabled_on_create = options[:disabled_on_create] || []
  @hidden_on_update     = options[:hidden_on_update] || []
  @hidden_on_create     = options[:hidden_on_create] || []

  options[:default_values] = model.attributes.merge(options[:default_values] || {})

  super(form_name, options)
end

#validateObject

Validate will check on ActiveRecord validation errors



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cuca/stdlib/arform.rb', line 56

def validate(variables)
  form	if @_content.empty? # password fields might write hints to the validator...
  clear
  @form_errors = {}
  p = variables
  p.delete(@submit_name)
  
  if @model.new_record? then 
     @disabled_on_create.each { |d| p.delete(d) }
     @hidden_on_create.each { |d| p.delete(d) }
  else 
     @disabled_on_update.each { |d| p.delete(d) }
     @hidden_on_update.each { |d| p.delete(d) }
  end

  # don't save empty passwords!!
  @password_fields ||= []
  @password_fields.each do |pwf|
     p.delete(pwf) if p[pwf].chomp.empty?
  end
  
  @model.attributes = p
  
  return true if @model.valid?

  @model.errors.each do |k,v|
     @form_errors[k] = v
  end
end