Module: InputModeHelper

Defined in:
app/helpers/input_mode_helper.rb

Instance Method Summary collapse

Instance Method Details

#validated_inputmode(record, attr) ⇒ String, NilClass

Examples:

validated_inputmode @user,   :email   #=> "email"
validated_inputmode @rating, :comment #=> "text"
validated_inputmode @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
# File 'app/helpers/input_mode_helper.rb', line 13

def validated_inputmode(record, attr)
	v_types = validators_for_attr(record, attr).map(&:kind).map(&:to_s)

	# TODO: try using pattern matching instead
	if v_types.include?("numericality")
		needs_integer?(record, attr) ? "numeric" : "decimal"
	elsif v_types.include?("email")
		"email"
	elsif v_types.include?("url")
		"url"
	elsif ["tel", "phone"].any? { |i| v_types.include?(i) }
		"tel"
	elsif ["search"].any? { |i| v_types.include?(i) } || attr.to_s.include?("search")
		"search"
	else
		"text"
	end
end

#validated_step(record, attr) ⇒ Numeric, NilClass

Examples:

validated_step @rating,   :score  #=> 1
validated_step @dumbbell, :weight #=> 0.25

Parameters:

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

Returns:

  • (Numeric, NilClass)


39
40
41
42
43
44
# File 'app/helpers/input_mode_helper.rb', line 39

def validated_step(record, attr)
	case validated_inputmode(record, attr)
	when "numeric" then   1
	when "decimal" then 0.1
	end
end