Class: DataMapper::Form::Base

Inherits:
Object
  • Object
show all
Includes:
Tag
Defined in:
lib/dm-forms/base.rb

Constant Summary collapse

REGULAR_ELEMENTS =
:textarea, :select
SELF_CLOSING_ELEMENTS =
:textfield, :submit, :file, :button, :hidden, :password, :radio, :checkbox
DEFAULT_VALUE_ELEMENTS =
:textarea, :select, :textfield, :radio, :checkbox

Constants included from Tag

Tag::BOOL_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tag

#close_tag, #open_tag, #self_closing_tag, #tag

Constructor Details

#initialize(model = nil, origin = nil) ⇒ Base

Returns a new instance of Base.



14
15
16
17
# File 'lib/dm-forms/base.rb', line 14

def initialize model = nil, origin = nil
  @model, @origin = model, origin
  @name = model.class.name.downcase.split('::').last
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



12
13
14
# File 'lib/dm-forms/base.rb', line 12

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/dm-forms/base.rb', line 12

def name
  @name
end

#originObject (readonly)

Returns the value of attribute origin.



12
13
14
# File 'lib/dm-forms/base.rb', line 12

def origin
  @origin
end

Instance Method Details

#fieldset(attrs = {}, &block) ⇒ Object



26
27
28
29
# File 'lib/dm-forms/base.rb', line 26

def fieldset attrs = {}, &block
  legend_tag = (legend = attrs.delete(:legend)) ? tag(:legend, legend) : ''
  origin.buffer << tag(:fieldset, legend_tag + origin.capture(&block), attrs)
end

#form(attrs = {}, &block) ⇒ Object



19
20
21
22
23
24
# File 'lib/dm-forms/base.rb', line 19

def form attrs = {}, &block
  origin.clear_buffer
  captured = origin.capture &block
  faux_method = process_form_attrs attrs 
  tag :form, faux_method + captured, attrs 
end

#select_options(options, selected = nil, prompt = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/dm-forms/base.rb', line 31

def select_options options, selected = nil, prompt = nil
  options[''] = prompt unless prompt.nil?
  selected ||= prompt || nil
  options.map do |value, contents|
    tag :option, contents, :value => value, :selected => (value == selected or contents == selected)
  end.join("\n")
end

#unbound_select(attrs = {}) ⇒ Object



45
46
47
48
49
# File 'lib/dm-forms/base.rb', line 45

def unbound_select attrs = {}
  options = select_options *attrs.extract!(:options, :selected, :prompt)
  process_unbound_element :select, attrs
  origin.buffer << tag(:select, options, attrs)
end

#unbound_textarea(*args) ⇒ Object



39
40
41
42
43
# File 'lib/dm-forms/base.rb', line 39

def unbound_textarea *args
  contents, attrs = split_args *args
  process_unbound_element :textarea, attrs
  origin.buffer << tag(:textarea, element_value(attrs) || contents, attrs)
end