Module: Bureaucrat::Utils

Extended by:
Utils
Included in:
Fields::ErrorHash, Fields::ErrorList, Forms::BoundField, Forms::Form, Formsets::BaseFormSet, Utils, Widgets::RadioFieldRenderer, Widgets::RadioInput, Widgets::Widget
Defined in:
lib/bureaucrat/utils.rb

Defined Under Namespace

Modules: SafeData Classes: SafeString, StringAccessHash

Constant Summary

ESCAPES =
{
  '&' => '&',
  '<' => '&lt;',
  '>' => '&gt;',
  '"' => '&quot;',
  "'" => '&#39;'
}

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) blank_value?(value)



52
53
54
# File 'lib/bureaucrat/utils.rb', line 52

def blank_value?(value)
  !value || value == ''
end

- (Object) conditional_escape(html)



71
72
73
# File 'lib/bureaucrat/utils.rb', line 71

def conditional_escape(html)
  html.is_a?(SafeData) ? html : escape(html)
end

- (Object) escape(html)



67
68
69
# File 'lib/bureaucrat/utils.rb', line 67

def escape(html)
  mark_safe(html.gsub(/[&<>"']/) {|match| ESCAPES[match]})
end

- (Object) flatatt(attrs)



75
76
77
# File 'lib/bureaucrat/utils.rb', line 75

def flatatt(attrs)
  attrs.map {|k, v| " #{k}=\"#{conditional_escape(v)}\""}.join('')
end

- (Object) format_string(string, values)



79
80
81
82
83
84
85
# File 'lib/bureaucrat/utils.rb', line 79

def format_string(string, values)
  output = string.dup
  values.each_pair do |variable, value|
    output.gsub!(/%\(#{variable}\)s/, value.to_s)
  end
  output
end

- (Object) make_bool(value)



96
97
98
# File 'lib/bureaucrat/utils.rb', line 96

def make_bool(value)
  !(value.respond_to?(:empty?) ? value.empty? : [0, nil, false].include?(value))
end

- (Object) make_float(value)



91
92
93
94
# File 'lib/bureaucrat/utils.rb', line 91

def make_float(value)
  value += '0' if value.is_a?(String) && value != '.' && value[-1,1] == '.'
  Float(value)
end

- (Object) mark_safe(s)



56
57
58
# File 'lib/bureaucrat/utils.rb', line 56

def mark_safe(s)
  s.is_a?(SafeData) ? s : SafeString.new(s.to_s)
end

- (Object) pretty_name(name)



87
88
89
# File 'lib/bureaucrat/utils.rb', line 87

def pretty_name(name)
  name.to_s.capitalize.gsub(/_/, ' ')
end