Class: EOTS::Field

Inherits:
Object
  • Object
show all
Defined in:
app/models/eots/field.rb

Constant Summary collapse

AlreadyDefinedError =
Class.new(RuntimeError)
InvalidSectionError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, label, options = {}) ⇒ Field

Returns a new instance of Field.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/eots/field.rb', line 8

def initialize(name, label, options={})
  @name = name.to_s
  @label = label
  opts = options.dup
  @caption = opts.delete(:caption)
  @section = opts.delete(:section) || :body
  @must_match = opts.delete(:must_match)
  unless [:header, :body, :footer].include? @section
    raise(InvalidSectionError,
          "Invalid section '#{@section}' -- must be :header, :body, or :footer")
  end
  apply_defaults(opts)
  @html_options = opts
end

Instance Attribute Details

#captionObject (readonly)

Returns the value of attribute caption.



6
7
8
# File 'app/models/eots/field.rb', line 6

def caption
  @caption
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.



6
7
8
# File 'app/models/eots/field.rb', line 6

def html_options
  @html_options
end

#labelObject (readonly)

Returns the value of attribute label.



6
7
8
# File 'app/models/eots/field.rb', line 6

def label
  @label
end

#must_matchObject (readonly)

Returns the value of attribute must_match.



6
7
8
# File 'app/models/eots/field.rb', line 6

def must_match
  @must_match
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'app/models/eots/field.rb', line 6

def name
  @name
end

#sectionObject (readonly)

Returns the value of attribute section.



6
7
8
# File 'app/models/eots/field.rb', line 6

def section
  @section
end

Instance Method Details

#html(form) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/eots/field.rb', line 23

def html(form)
  star = "* " if required?
  result = form.label_tag(name, "#{star}#{label}".html_safe)
  type = html_options[:type]
  if type.to_sym == :checkbox  # to_sym 'cuz it could be string or symbol
    result << form.(:span, " ")  # just to provide a spacer
    opts = html_options.dup
    checked = opts.delete :checked
    result << form.check_box_tag(name, name, checked, opts)
  else
    result << form.tag(:br)
    tag = "#{type}_field_tag".gsub("textarea_field", "text_area").to_sym
    result << form.send(tag, name, nil, html_options)
  end
  if caption
    result << form.tag(:br) 
    result << form.(:small, caption.html_safe)
  end
  result
end

#required?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/eots/field.rb', line 44

def required?
  html_options[:required]
end