Module: Bureaucrat::Utils

Included in:
Adaptors::FloatAdaptor, Fields::ErrorHash, Fields::ErrorList, Forms::BoundField, Forms::Form, Formsets::BaseFormSet, Widgets::Media, Widgets::Widget
Defined in:
lib/bureaucrat/utils.rb,
lib/bureaucrat/wizard.rb

Defined Under Namespace

Modules: SafeData Classes: OrderedHash, SafeString

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.conditional_escape(html) ⇒ Object



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

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

.escape(html) ⇒ Object



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

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

.flatatt(attrs) ⇒ Object



60
61
62
# File 'lib/bureaucrat/utils.rb', line 60

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

.format_string(string, values) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/bureaucrat/utils.rb', line 64

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

.make_bool(value) ⇒ Object



81
82
83
# File 'lib/bureaucrat/utils.rb', line 81

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

.make_float(value) ⇒ Object



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

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

.mark_safe(s) ⇒ Object



41
42
43
# File 'lib/bureaucrat/utils.rb', line 41

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

.pretty_name(name) ⇒ Object



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

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

.security_hash(request, form, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bureaucrat/wizard.rb', line 15

def security_hash(request, form, *args)
  data = []
  form.each do |bf|
    value = if form.empty_permitted? && ! form.changed? then bf.data
            else bf.field.clean(bf.data)
            end
    value ||= ''
    value = value.strip if value.respond_to? :strip
    data.append([bf.name, value])
  end

  data += args
  data << settings.SECRET_KEY # FIXME

  Digest::MD5.hexdigest(Marshal.dump(data))
end