Class: Bureaucrat::Forms::BoundField

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/bureaucrat/forms.rb

Constant Summary

Constants included from Utils

Utils::ESCAPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

conditional_escape, escape, flatatt, format_string, make_bool, make_float, mark_safe, pretty_name, security_hash

Constructor Details

#initialize(form, field, name) ⇒ BoundField

Returns a new instance of BoundField.



12
13
14
15
16
17
18
19
20
# File 'lib/bureaucrat/forms.rb', line 12

def initialize(form, field, name)
  @form = form
  @field = field
  @name = name
  @html_name = form.add_prefix(name).to_sym
  @html_initial_name = form.add_initial_prefix(name).to_sym
  @label = @field.label || pretty_name(name)
  @help_text = @field.help_text || ''
end

Instance Attribute Details

#labelObject

Returns the value of attribute label.



10
11
12
# File 'lib/bureaucrat/forms.rb', line 10

def label
  @label
end

Instance Method Details

#as_hidden(attrs = nil, only_initial = false) ⇒ Object



60
61
62
# File 'lib/bureaucrat/forms.rb', line 60

def as_hidden(attrs=nil, only_initial=false)
  as_widget(@field.hidden_widget, attrs, only_initial)
end

#as_text(attrs = nil, only_initial = false) ⇒ Object



52
53
54
# File 'lib/bureaucrat/forms.rb', line 52

def as_text(attrs=nil, only_initial=false)
  as_widget(Widgets::TextInput.new, attrs, only_initial)
end

#as_textarea(attrs = nil, only_initial = false) ⇒ Object



56
57
58
# File 'lib/bureaucrat/forms.rb', line 56

def as_textarea(attrs=nil, only_initial=false)
  as_widget(Widgets::Textarea.new, attrs, only_initial)
end

#as_widget(widget = nil, attrs = nil, only_initial = false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bureaucrat/forms.rb', line 30

def as_widget(widget=nil, attrs=nil, only_initial=false)
  widget ||= @field.widget
  attrs ||= {}
  auto_id = self.auto_id
  attrs[:id] ||= auto_id if auto_id && !widget.attrs.key?(:id)

  if !@form.bound?
    data = @form.initial.fetch(@name.to_s, @field.initial)
    data = data.call if data.respond_to?(:call)
  else
    if @field.is_a?(Fields::FileField) && @data.nil?
      data = @form.initial.fetch(@name, @field.initial)
    else
      data = self.data
    end
  end

  name = only_initial ? @html_initial_name : @html_name

  widget.render(name.to_s, data, attrs)
end

#auto_idObject



85
86
87
88
# File 'lib/bureaucrat/forms.rb', line 85

def auto_id
  fauto_id = @form.auto_id
  fauto_id ? fauto_id % @html_name : ''
end

#dataObject



64
65
66
# File 'lib/bureaucrat/forms.rb', line 64

def data
  @field.widget.value_from_datahash(@form.data, @form.files, @html_name)
end

#errorsObject



26
27
28
# File 'lib/bureaucrat/forms.rb', line 26

def errors
  @form.errors.fetch(@name, @form.error_class.new)
end

#hidden?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/bureaucrat/forms.rb', line 81

def hidden?
  @field.widget.hidden?
end

#label_tag(contents = nil, attrs = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bureaucrat/forms.rb', line 68

def label_tag(contents=nil, attrs=nil)
  contents ||= conditional_escape(@label)
  widget = @field.widget
  id_ = widget.attrs[:id] || self.auto_id

  if id_
    attrs = attrs ? flattatt(attrs) : ''
    contents = "<label for=\"#{Widgets::Widget.id_for_label(id_)}\"#{attrs}>#{contents}</label>"
  end

  mark_safe(contents)
end

#to_sObject



22
23
24
# File 'lib/bureaucrat/forms.rb', line 22

def to_s
  @field.show_hidden_initial ? as_widget + as_hidden(nil, true) : as_widget
end