Module: Sem4rSinatra::Helpers

Defined in:
lib/sem4r_sinatra/helpers.rb

Instance Method Summary collapse

Instance Method Details

#base_urlObject



25
26
27
28
29
30
31
# File 'lib/sem4r_sinatra/helpers.rb', line 25

def base_url
  if Sinatra::Application.port == 80
    "http://#{Sinatra::Application.host}/"
  else
    "http://#{Sinatra::Application.host}:#{Sinatra::Application.port}/"
  end
end

#gen_url(field, visible_field = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/sem4r_sinatra/helpers.rb', line 45

def gen_url(field, visible_field = nil)
  visible_field ||= field
  pos = request.env['REQUEST_URI'].index "?"
  url = if pos
    request.env['REQUEST_URI'][0...pos]
  else
    request.env['REQUEST_URI']
  end
  "<a href='#{url}?order=#{field}'>#{visible_field}</a>"
end

#partial(template, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sem4r_sinatra/helpers.rb', line 5

def partial(template, *args)
  options = args.extract_options!
  options.merge!(:layout => false)
  if collection = options.delete(:collection) then
    collection.inject([]) do |buffer, member|
      buffer << haml(template, options.merge(
          :layout => false,
          :locals => {template.to_sym => member}
        )
      )
    end.join("\n")
  else
    haml(template, options)
  end
end

#partial_erb(template) ⇒ Object



21
22
23
# File 'lib/sem4r_sinatra/helpers.rb', line 21

def partial_erb(template)
  erb template
end

#rfc_3339(timestamp) ⇒ Object



56
57
58
# File 'lib/sem4r_sinatra/helpers.rb', line 56

def rfc_3339(timestamp)
  timestamp.strftime("%Y-%m-%dT%H:%M:%SZ")
end

#when_updated(time) ⇒ Object



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

def when_updated(time)
  days = (Time.now - time) / (60 * 60 * 24)
  case days.to_i
  when 0
    "today"
  when 1
    "yesterday"
  else
    "#{sprintf("%i", days) } days ago"
  end
end