Class: Bureaucrat::Forms::BoundField
- Inherits:
-
Object
- Object
- Bureaucrat::Forms::BoundField
- Includes:
- Utils
- Defined in:
- lib/bureaucrat/forms.rb
Overview
Instances of BoundField
represent a fields with associated data. BoundFields are used internally by the Form
class.
Constant Summary
Constants included from Utils
Instance Attribute Summary collapse
-
#field ⇒ Object
Field label text.
-
#form ⇒ Object
Field label text.
-
#help_text ⇒ Object
Field label text.
-
#html_initial_name ⇒ Object
Field label text.
-
#html_name ⇒ Object
Field label text.
-
#label ⇒ Object
Field label text.
-
#name ⇒ Object
Field label text.
Instance Method Summary collapse
-
#as_hidden(attrs = nil, only_initial = false) ⇒ Object
Renders this field as hidden.
-
#as_text(attrs = nil, only_initial = false) ⇒ Object
Renders this field as a text input.
-
#as_textarea(attrs = nil, only_initial = false) ⇒ Object
Renders this field as a text area.
-
#as_widget(widget = nil, attrs = nil, only_initial = false) ⇒ Object
Renders this field with the option of using alternate widgets and attributes.
-
#auto_id ⇒ Object
Generates the id for this field.
- #css_classes(extra_classes = nil) ⇒ Object
-
#data ⇒ Object
The data associated to this field.
-
#errors ⇒ Object
Errors for this field.
-
#hidden? ⇒ Boolean
true if the widget for this field is of the hidden kind.
-
#initialize(form, field, name) ⇒ BoundField
constructor
Instantiates a new
BoundField
associated toform
‘s fieldfield
namedname
. -
#label_tag(contents = nil, attrs = nil) ⇒ Object
Renders the label tag for this field.
-
#to_s ⇒ Object
Renders the field.
- #value ⇒ Object
Methods included from Utils
#blank_value?, #conditional_escape, #escape, #flatatt, #format_string, #make_bool, #make_float, #mark_safe, #pretty_name
Constructor Details
#initialize(form, field, name) ⇒ BoundField
Instantiates a new BoundField
associated to form
‘s field field
named name
.
14 15 16 17 18 19 20 21 22 |
# File 'lib/bureaucrat/forms.rb', line 14 def initialize(form, field, name) @form = form @field = field @name = name @html_name = form.add_prefix(name) @html_initial_name = form.add_initial_prefix(name) @label = @field.label || pretty_name(name) @help_text = @field.help_text || '' end |
Instance Attribute Details
#field ⇒ Object
Field label text
10 11 12 |
# File 'lib/bureaucrat/forms.rb', line 10 def field @field end |
#form ⇒ Object
Field label text
10 11 12 |
# File 'lib/bureaucrat/forms.rb', line 10 def form @form end |
#help_text ⇒ Object
Field label text
10 11 12 |
# File 'lib/bureaucrat/forms.rb', line 10 def help_text @help_text end |
#html_initial_name ⇒ Object
Field label text
10 11 12 |
# File 'lib/bureaucrat/forms.rb', line 10 def html_initial_name @html_initial_name end |
#html_name ⇒ Object
Field label text
10 11 12 |
# File 'lib/bureaucrat/forms.rb', line 10 def html_name @html_name end |
#label ⇒ Object
Field label text
10 11 12 |
# File 'lib/bureaucrat/forms.rb', line 10 def label @label end |
#name ⇒ Object
Field label text
10 11 12 |
# File 'lib/bureaucrat/forms.rb', line 10 def name @name end |
Instance Method Details
#as_hidden(attrs = nil, only_initial = false) ⇒ Object
Renders this field as hidden.
68 69 70 |
# File 'lib/bureaucrat/forms.rb', line 68 def as_hidden(attrs=nil, only_initial=false) (@field., attrs, only_initial) end |
#as_text(attrs = nil, only_initial = false) ⇒ Object
Renders this field as a text input.
58 59 60 |
# File 'lib/bureaucrat/forms.rb', line 58 def as_text(attrs=nil, only_initial=false) (Widgets::TextInput.new, attrs, only_initial) end |
#as_textarea(attrs = nil, only_initial = false) ⇒ Object
Renders this field as a text area.
63 64 65 |
# File 'lib/bureaucrat/forms.rb', line 63 def as_textarea(attrs=nil, only_initial=false) (Widgets::Textarea.new, attrs, only_initial) end |
#as_widget(widget = nil, attrs = nil, only_initial = false) ⇒ Object
Renders this field with the option of using alternate widgets and attributes.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bureaucrat/forms.rb', line 36 def (=nil, attrs=nil, only_initial=false) ||= @field. attrs ||= {} auto_id = self.auto_id attrs[:id] ||= auto_id if auto_id && !.attrs.key?(:id) if !@form.bound? data = @form.initial.fetch(@name, @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 .render(name.to_s, data, attrs) end |
#auto_id ⇒ Object
Generates the id for this field.
133 134 135 136 |
# File 'lib/bureaucrat/forms.rb', line 133 def auto_id fauto_id = @form.auto_id fauto_id ? fauto_id % @html_name : '' end |
#css_classes(extra_classes = nil) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/bureaucrat/forms.rb', line 107 def css_classes(extra_classes = nil) # Returns a string of space-separated CSS classes for this field. if extra_classes.respond_to?(:split) extra_classes = extra_classes.split end extra_classes = Set.new(extra_classes) if !errors.empty? && !Utils.blank_value?(form.error_css_class) extra_classes << form.error_css_class end if field.required && !Utils.blank_value?(form.required_css_class) extra_classes << form.required_css_class end extra_classes.to_a.join(' ') end |
#data ⇒ Object
The data associated to this field.
73 74 75 |
# File 'lib/bureaucrat/forms.rb', line 73 def data @field..value_from_formdata(@form.data, @html_name) end |
#errors ⇒ Object
Errors for this field.
30 31 32 |
# File 'lib/bureaucrat/forms.rb', line 30 def errors @form.errors.fetch(@name, @form.error_class.new) end |
#hidden? ⇒ Boolean
true if the widget for this field is of the hidden kind.
128 129 130 |
# File 'lib/bureaucrat/forms.rb', line 128 def hidden? @field..hidden? end |
#label_tag(contents = nil, attrs = nil) ⇒ Object
Renders the label tag for this field.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/bureaucrat/forms.rb', line 94 def label_tag(contents=nil, attrs=nil) contents ||= conditional_escape(@label) = @field. id_ = .attrs[:id] || self.auto_id if id_ attrs = attrs ? flatatt(attrs) : '' contents = "<label for=\"#{Widgets::Widget.id_for_label(id_)}\"#{attrs}>#{contents}</label>" end mark_safe(contents) end |
#to_s ⇒ Object
Renders the field.
25 26 27 |
# File 'lib/bureaucrat/forms.rb', line 25 def to_s @field.show_hidden_initial ? + as_hidden(nil, true) : end |
#value ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bureaucrat/forms.rb', line 77 def value # Returns the value for this BoundField, using the initial value if # the form is not bound or the data otherwise. if form.bound? val = field.bound_data(data, form.initial.fetch(name, field.initial)) else val = form.initial.fetch(name, field.initial) if val.respond_to?(:call) val = val.call end end field.prepare_value(val) end |