Module: MerbExt::Helpers

Defined in:
lib/merb-ext/helpers.rb

Instance Method Summary collapse

Instance Method Details

#colorize(var, format = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/merb-ext/helpers.rb', line 33

def colorize(var, format = nil)
  color = if var.is_a?(Numeric)
    var >= 0 ? :green : :red
  else
    var ? :green : :red
  end

  var = format % var if format
  '<span style="color: %s">%s</span>' % [color, var]
end

#default_error_messages_for(obj, opts = {}) ⇒ Object



3
4
5
# File 'lib/merb-ext/helpers.rb', line 3

def default_error_messages_for(obj, opts = {})
  error_messages_for obj, :build_li => "<li>%s</li>", :header => "<h3>#{"Oops! There are some errors:".t}</h3>"
end

#delete_button(*args, &block) ⇒ Object



62
63
64
# File 'lib/merb-ext/helpers.rb', line 62

def delete_button(*args, &block)
  js_button(:delete, *args, &block)
end

#fieldset(attrs = {}, &blk) ⇒ Object



7
8
9
10
# File 'lib/merb-ext/helpers.rb', line 7

def fieldset(attrs = {}, &blk)
  legend = (l_attr = attrs.delete(:legend)) ? tag(:h2, l_attr) : ""
  legend + super
end

#gravatar_image_for(email, opts = {}) ⇒ Object



50
51
52
# File 'lib/merb-ext/helpers.rb', line 50

def gravatar_image_for(email, opts = {})
  image_tag(gravatar_url_for(email, opts), :alt => "")
end

#gravatar_url_for(email, opts = {}) ⇒ Object



45
46
47
48
# File 'lib/merb-ext/helpers.rb', line 45

def gravatar_url_for(email, opts = {})
  opts[:default] = "http://#{request.host}/images/#{opts[:default]}" if opts[:default]
  "http://www.gravatar.com/avatar.php/#{Digest::MD5.hexdigest(email || "")}?#{opts.to_params}"
end

#js_button(method, label, url, attrs = {}, &block) ⇒ Object



66
67
68
69
70
71
# File 'lib/merb-ext/helpers.rb', line 66

def js_button(method, label, url, attrs = {}, &block)
  tag :form, :class => "#{method}-btn btn", :action => url, :method => :post do
    tag(:input, :type => :hidden, :name => "_method", :value => method.to_s) <<
    tag(:input, attrs.merge(:value => label, :type => :submit))
  end
end


16
17
18
19
20
21
22
# File 'lib/merb-ext/helpers.rb', line 16

def link_to_if(condition, label, *args, &block)
  if condition
    link_to(label, *args, &block)
  else
    tag(:span, label)
  end
end


12
13
14
# File 'lib/merb-ext/helpers.rb', line 12

def link_to_unless(condition, label, *args, &block)
  link_to_if !condition, label, *args, &block
end

#number_with_delimiter(number, delimiter = ".") ⇒ Object



24
25
26
# File 'lib/merb-ext/helpers.rb', line 24

def number_with_delimiter(number, delimiter = ".")
  number.to_s.reverse.scan(/.{1,3}/).join(delimiter).reverse
end

#post_button(*agrs, &block) ⇒ Object



58
59
60
# File 'lib/merb-ext/helpers.rb', line 58

def post_button(*agrs, &block)
  js_button(:post, *args, &block)
end

#put_button(*args, &block) ⇒ Object



54
55
56
# File 'lib/merb-ext/helpers.rb', line 54

def put_button(*args, &block)
  js_button(:put, *args, &block)
end

#truncate(text, max_length) ⇒ Object



28
29
30
31
# File 'lib/merb-ext/helpers.rb', line 28

def truncate(text, max_length)
  return text[0, max_length] + "..." if text.length > max_length
  text
end