Module: LimitsHelper

Defined in:
app/helpers/limits_helper.rb

Instance Method Summary collapse

Instance Method Details

#validated_max(record, attr) ⇒ Integer, NilClass

Examples:

validated_required @rating, :score #=> 5

Parameters:

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

Returns:

  • (Integer, NilClass)

Raises:

  • (TypeError)

See Also:



41
42
43
# File 'app/helpers/limits_helper.rb', line 41

def validated_max(record, attr)
	validated_value(record: record, attr: attr, type: :numericality, options: [:less_than, :less_than_or_equal_to])
end

#validated_maxlength(record, attr) ⇒ Integer, NilClass

Examples:

validated_maxlength @review, :comment #=> 1_000

Parameters:

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

Returns:

  • (Integer, NilClass)

Raises:

  • (TypeError)

See Also:



13
14
15
16
17
# File 'app/helpers/limits_helper.rb', line 13

def validated_maxlength(record, attr)
	maximum = validated_value(record: record, attr: attr, type: :length, options: [:maximum])
	range   = validated_value(record: record, attr: attr, type: :length, options: [:in])
	range&.max || maximum
end

#validated_min(record, attr) ⇒ Integer, NilClass

Examples:

validated_min @rating, :score #=> 1

Parameters:

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

Returns:

  • (Integer, NilClass)

Raises:

  • (TypeError)

See Also:



53
54
55
# File 'app/helpers/limits_helper.rb', line 53

def validated_min(record, attr)
	validated_value(record: record, attr: attr, type: :numericality, options: [:greater_than, :greater_than_or_equal_to])
end

#validated_minlength(record, attr) ⇒ Integer, NilClass

Examples:

validated_minlength @review, :comment #=> 3

Parameters:

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

Returns:

  • (Integer, NilClass)

Raises:

  • (TypeError)

See Also:



27
28
29
30
31
# File 'app/helpers/limits_helper.rb', line 27

def validated_minlength(record, attr)
	minimum = validated_value(record: record, attr: attr, type: :length, options: [:minimum])
	range   = validated_value(record: record, attr: attr, type: :length, options: [:in])
	range&.min || minimum
end

#validated_required(record, attr) ⇒ Boolean

Examples:

validated_required @user, :email #=> true

Parameters:

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

Returns:

  • (Boolean)

Raises:

  • (TypeError)

See Also:



65
66
67
68
# File 'app/helpers/limits_helper.rb', line 65

def validated_required(record, attr)
	# validated_value(record: record, attr: attr, type: :presence, options: [:presece]) ?
	validators_for(record: record, attr: attr, type: :presence).any?
end