Module: Forms::Tags::Helpers

Defined in:
lib/forms/tags/helpers.rb

Class Method Summary collapse

Class Method Details

.attributes(tag, extras = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/forms/tags/helpers.rb', line 17

def attributes(tag, extras={})
  id = tag.attr['name'] || extras['name']
  id = id || tag.attr['for'] + '_label' if tag.attr['for']
  id = id.gsub('[','_').gsub(']','') unless id.nil?

  attrs = {
    'id'            => id,  
    'class'         => tag.attr['type'],
    'name'          => nil,
    'for'           => nil,
    'action'        => nil,
    'placeholder'   => nil,
    'value'         => nil,
    'maxlength'     => nil
  }.merge(extras)
  
  result = attrs.collect do |k,v|
    v = (tag.attr[k] || v)
    next if v.blank?
    %(#{k}="#{v}")
  end.reject{|e| e.blank?}

  result.join(' ')
end

.render(tag) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/forms/tags/helpers.rb', line 7

def render(tag)
  string =  %{<form enctype="multipart/form-data" method="post" #{attributes(tag)}>\n}
    string << %{<input type="hidden" name="_method" value="#{tag.attr['method']}" />\n}
    string << %{<input type="hidden" name="page_id" value="#{tag.locals.page.id}" />\n}
    string << %{<r:form>}
      string << tag.locals.form.body
    string << %{</r:form>}
  string << %{</form>}
end

.require!(tag, name, key) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/forms/tags/helpers.rb', line 42

def require!(tag,name,key)
  if tag.attr[key].present?
    return true
  else
    raise "'<r:#{name} />' requires the '#{key}' attribute"
  end
end