Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Includes:
Context, TagHelper
Defined in:
lib/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#bs_static_control(method) ⇒ Object



83
84
85
# File 'lib/form_builder.rb', line 83

def bs_static_control(method)
	 :p, @object.send(method), class: 'form-control-static'
end

#form_group(method = nil, options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/form_builder.rb', line 11

def form_group(method=nil, options={}, &block)
	raise ArgumentError, 'Missing block' unless block_given?

	method, options = nil, method if method.is_a? Hash

	form_group_options = options.dup
	classes = parse_html_classes_to_arr options[:class]
	add_html_class classes, 'form-group'
	form_group_options[:class] = classes
	bs_form_helper = BsFormBuilder.new self, @object_name, method
	content = @template.capture(bs_form_helper, &block)

	if method
		if @object.errors[method].any?
			add_html_class classes, 'has-error'
		end

		if render_validation_error? options
			content << validation_error(method)
		end
	end

	 :div, content, form_group_options
end

#label(method, text = nil, options = {}, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/form_builder.rb', line 63

def label(method, text = nil, options = {}, &block)
	if text.is_a? Hash
		label_text = nil
		label_options = text.dup
	else
		label_text = text
		label_options = options.dup
	end

	label_classes = parse_html_classes_to_arr label_options[:class]

	if @object.class.validators_on(method).map(&:class).include?(ActiveRecord::Validations::PresenceValidator)
		add_html_class label_classes, 'required-label'
	end

	label_options[:class] = label_classes

	@template.label @object_name, method, label_text, label_options, &block
end

#validation_error(method) ⇒ Object



36
37
38
39
40
41
# File 'lib/form_builder.rb', line 36

def validation_error(method)
	if @object.errors[method].any?
		errors = @object.errors[method].join(' ')
		(:span, errors, class: [:'helper-block', :'text-danger'])
	end
end