Class: Nitro::Form::Control

Inherits:
Object
  • Object
show all
Includes:
XhtmlHelper
Defined in:
lib/nitro/helper/form/controls.rb,
lib/nitro/helper/form/controls.rb

Overview

The controls map.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XhtmlHelper

#date_select, #datetime_select, #hidden, #href_of, #js_popup, #link_to, #onclick_popup, #options, #popup, #submit, #time_select

Constructor Details

#initialize(obj, key, options = {}) ⇒ Control

setup instance vars to use in methods



32
33
34
35
36
37
# File 'lib/nitro/helper/form/controls.rb', line 32

def initialize(obj, key, options = {})
  @obj = obj
  @prop = key
  @value = options[:value] || obj.send(key.name.to_sym)
  @options = options
end

Instance Attribute Details

#objObject (readonly)

Returns the value of attribute obj.



25
26
27
# File 'lib/nitro/helper/form/controls.rb', line 25

def obj
  @obj
end

#propObject (readonly) Also known as: rel

Fetch the instance vars in a nice way use either rel or prop.

values/value contain the contents of the prop or rel, (values reads better for relations)



22
23
24
# File 'lib/nitro/helper/form/controls.rb', line 22

def prop
  @prop
end

#valueObject (readonly) Also known as: values

Returns the value of attribute value.



27
28
29
# File 'lib/nitro/helper/form/controls.rb', line 27

def value
  @value
end

Class Method Details

.fetch(obj, key, options = {}) ⇒ Object

Fetch a control, setup for ‘obj’ for a property, or relation or :symbol defaults to Control if not found



369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/nitro/helper/form/controls.rb', line 369

def self.fetch(obj, key, options={})
  if key.kind_of? Og::Relation
    control_sym = key[:control] || key.class.to_s.demodulize.underscore.to_sym
  elsif key.kind_of? Property
    control_sym = key[:control] || key.klass.to_s.underscore.to_sym
  else
    control_sym = key.to_sym
  end
  
  default_to = options.fetch(:default, Control)
  self.map.fetch(control_sym, Control).new(obj, key, options)
end

Instance Method Details

#labelObject

Label item. Override to customise



47
48
49
# File 'lib/nitro/helper/form/controls.rb', line 47

def label
  %{<label for="#{prop.name}">#{prop[:title] || prop.name.to_s.humanize}</label>}
end

#on_populate(val) ⇒ Object

Custom callback to process the request information posted back from the form

When Property.populate_object (or fill) is called with the :preprocess => true option then this method will get called before the value is pushed onto the object that is getting ‘filled’

Overide this on controls that require special mods to the incoming values



62
63
64
# File 'lib/nitro/helper/form/controls.rb', line 62

def on_populate(val)
  return val
end

#renderObject

Main bulk of the control. Overide to customise



41
42
43
# File 'lib/nitro/helper/form/controls.rb', line 41

def render
  "No view for this control"
end