Module: Helpers

Includes:
Rack::Utils, WillPaginate::ViewHelpers::Base
Defined in:
lib/mongo_fe/helpers/helpers.rb

Constant Summary collapse

PLACEHOLDER_SORT =
"ex. name:ASC, age:DESC"
PLACEHOLDER_QUERY_STRING =
'ex. {"name":"foo"}'
PLACEHOLDER_SHOW =
"ex. name, age"
MEGABYTE =
1024.0 * 1024.0

Instance Method Summary collapse

Instance Method Details

#bytes_to_mb(bytes = 0) ⇒ Object

convert Bytes to MegaBytes


124
125
126
# File 'lib/mongo_fe/helpers/helpers.rb', line 124

def bytes_to_mb bytes=0
  bytes / MEGABYTE
end

#collectionObject


35
36
37
# File 'lib/mongo_fe/helpers/helpers.rb', line 35

def collection
  current_db.collection(session[:collection]) if current_db? && collection?
end

#collection?Boolean

Returns:

  • (Boolean)

39
40
41
# File 'lib/mongo_fe/helpers/helpers.rb', line 39

def collection?
  true if session[:collection]
end

#current_dbObject


27
28
29
# File 'lib/mongo_fe/helpers/helpers.rb', line 27

def current_db
  MongoFe::MongoDB.connection.db(session[:db]) if current_db?
end

#current_db?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/mongo_fe/helpers/helpers.rb', line 23

def current_db?
  true if session[:db]
end

#current_db_nameObject


31
32
33
# File 'lib/mongo_fe/helpers/helpers.rb', line 31

def current_db_name
  session[:db] if current_db?
end

#current_pageObject


13
14
15
# File 'lib/mongo_fe/helpers/helpers.rb', line 13

def current_page
  url_path request.path_info.sub('/', '')
end

#flashObject

poor man flash solution


71
72
73
74
# File 'lib/mongo_fe/helpers/helpers.rb', line 71

def flash
  session[:flash] = Hashie::Mash.new if session[:flash].nil?
  session[:flash]
end

43
44
45
46
47
48
49
# File 'lib/mongo_fe/helpers/helpers.rb', line 43

def link_to(name, location, alternative = false)
  if alternative && alternative[:condition]
    "<a href=#{alternative[:location]}>#{alternative[:name]}</a>"
  else
    "<a href=#{location}>#{name}</a>"
  end
end

#partial(template, *args) ⇒ Object

stolen from github.com/cschneid/irclogger/blob/master/lib/partials.rb and made a lot more robust by me: gist.github.com/119874. Another me, not me :)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mongo_fe/helpers/helpers.rb', line 94

def partial(template, *args)
  template_array = template.to_s.split('/')
  template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
  options = args.last.is_a?(Hash) ? args.pop : {}
  options.merge!(:layout => false)
  locals = options[:locals] || {}

  if (collection = options.delete(:collection))
    collection.inject([]) do |buffer, member|
      buffer << haml(:"#{template}",
                     options.merge(:layout => false,
                                   :locals => {template_array[-1].to_sym => member}.merge(locals)))
    end.join("\n")
  else
    haml(:"#{template}", options)
  end
end

#pretty_json(json) ⇒ Object

prettifying a JSON structure


52
53
54
# File 'lib/mongo_fe/helpers/helpers.rb', line 52

def pretty_json(json)
  JSON.pretty_generate(json)
end

#queryObject

check if there is a query in the session and return its elements as an openstruct object


81
82
83
84
85
86
87
88
89
# File 'lib/mongo_fe/helpers/helpers.rb', line 81

def query
  if (q = session[:query])
    OpenStruct.new(:string => q.first.empty? ? '' : q.first.to_json,
                   :show => q[1].empty? ? '' : q[1],
                   :sort => q.last.empty? ? '' : q.last)
  else
    OpenStruct.new(:string => PLACEHOLDER_QUERY_STRING, :show => PLACEHOLDER_SHOW, :sort => PLACEHOLDER_SORT)
  end
end

#random_string(len) ⇒ Object

generate a simple random string


113
114
115
116
117
118
# File 'lib/mongo_fe/helpers/helpers.rb', line 113

def random_string(len)
  chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  str = ""
  1.upto(len) { |i| str << chars[rand(chars.size-1)] }
  str
end

#truncate(str, max = 0) ⇒ Object

truncate a string to max characters and append '…' at the end


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mongo_fe/helpers/helpers.rb', line 57

def truncate(str, max=0)
  return '' if str.blank?

  if str.length <= 1
    t_string = str
  else
    t_string = str[0..[str.length, max].min]
    t_string = str.length > t_string.length ? "#{t_string}..." : t_string
  end

  t_string
end

#url_path(*path_parts) ⇒ Object Also known as: u


17
18
19
# File 'lib/mongo_fe/helpers/helpers.rb', line 17

def url_path(*path_parts)
  [path_prefix, path_parts].join("/").squeeze('/')
end