Class: ARFormWidget

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

Overview

Form’s for ActiveRecord

AR Form can work just by providing one model of ActiveRecord. Likly that you want to overwrite the form method to run a custom layout. You can use the fe* methods to build form elements for the model columns definition.

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

#get_variables, #load_variables, #posted?, #setup

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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cuca/stdlib/arform.rb', line 195

def form
  r = mabtext { FormErrors(@form_errors) }
  r << "<form action='#{@post_to}' method='POST'>\n"
  r << "<table>"
  @model.class.columns.each do |col|
    next if field_hidden?(col.name)
    k = col.name
    v = @model.send(k.intern)		# this allows us to overwrite accessors
    r << "<tr><td>#{k}</td><td>#{fe(col.type,k,v)}</td></tr>"
  end
  r << "<tr><td><br/></td></tr>"
  r << "<tr><td></td><td><input type='submit' value=#{@model.new_record? ? 'Save' : 'Update'} name='#{@submit_name}'></td></tr>"
  r << "</table>\n</form>\n"
  @_content = r
end

#on_submitObject



37
38
39
# File 'lib/cuca/stdlib/arform.rb', line 37

def on_submit
 controller.send(@form_name+'_submit', @model) unless controller.nil?
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
    


28
29
30
31
32
33
34
35
# File 'lib/cuca/stdlib/arform.rb', line 28

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] || []
  super(form_name, options[:post_to])
end

#validateObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cuca/stdlib/arform.rb', line 41

def validate
  form	if @_content.empty? # password fields might write hints to the validator...
  @form_erros = {}
  p = request_parameters.dup
  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
  $stderr.puts p.inspect
  @model.attributes = p
  
  return true if @model.valid?

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