Module: InputTypeHelper

Defined in:
app/helpers/input_type_helper.rb

Instance Method Summary collapse

Instance Method Details

#resolved_input_type(record, attr) ⇒ String, NilClass

Examples:

resolved_input_type @user,   :email   #=> "email"
resolved_input_type @rating, :comment #=> "text"
resolved_input_type @rating, :score   #=> "numeric"

Parameters:

  • record (ActiveRecord::Base)
  • attr (String, Symbol)

Returns:

  • (String, NilClass)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/helpers/input_type_helper.rb', line 13

def resolved_input_type(record, attr)
	v_types = validators_for_attr(record, attr).map(&:kind)
	klass = record.class

	# TODO: try using pattern matching instead
	if v_types.include? "numericality" or [:integer, :bigint, :float, :decimal].include? klass.column_for_attribute(attr).type
		"number"

	elsif v_types.include? "email" or attr.to_s.include? "email"
		"email"

	elsif v_types.include? "url" or attr.to_s.include? "url"
		"url"

	elsif v_types.include? "tel" or attr.to_s.include? "tel"
		"tel"

	elsif v_types.include? "phone" or attr.to_s.include? "phone"
		"tel"

	elsif v_types.include? "color" or attr.to_s.include? "color"
		"color"

	elsif ["search"].any? { |i| v_types.include?(i) } or attr.to_s.include? "search"
		"search"

	elsif klass.column_for_attribute(attr).type == :text
		validated_value(record: record, attr: attr, type: :length, options: [:maximm]).to_i > 256 ? "textarea" : "text"

	elsif klass.column_for_attribute(attr).type == :date
		"date"

	elsif klass.column_for_attribute(attr).type == :datetime
		"datetimelocal"

	elsif klass.column_for_attribute(attr).type == :time
		"time"

	else
		"text"
	end
end