Module: ApplicationHelper

Defined in:
lib/deltacloud/helpers/application_helper.rb

Overview

Methods added to this helper will be available to all templates in the application.

Instance Method Summary collapse

Instance Method Details



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/deltacloud/helpers/application_helper.rb', line 22

def bread_crumb
  s = "<ul class='breadcrumb'><li class='first'><a href='#{url_for('/')}'>&#948</a></li>"
  url = request.path_info.split('?')  #remove extra query string parameters
  levels = url[0].split('/') #break up url into different levels
  levels.each_with_index do |level, index|
    unless level.blank?
      if index == levels.size-1 ||
         (level == levels[levels.size-2] && levels[levels.size-1].to_i > 0)
        s += "<li class='subsequent'>#{level.gsub(/_/, ' ')}</li>\n" unless level.to_i > 0
      else
          link = levels.slice(0, index+1).join("/")
          s += "<li class='subsequent'><a href=\"#{url_for(link)}\">#{level.gsub(/_/, ' ')}</a></li>\n"
      end
    end
  end
  s+="</ul>"
end

#cdata(&block) ⇒ Object



116
117
118
119
# File 'lib/deltacloud/helpers/application_helper.rb', line 116

def cdata(&block)
  text = capture_haml(&block)
  "<![CDATA[#{text.strip}]]>"
end

#driver_auth_feature_nameObject



52
53
54
55
# File 'lib/deltacloud/helpers/application_helper.rb', line 52

def driver_auth_feature_name
  return 'key' if driver_has_feature?(:authentication_key)
  return 'password' if driver_has_feature?(:authentication_password)
end

#driver_has_auth_features?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/deltacloud/helpers/application_helper.rb', line 48

def driver_has_auth_features?
  driver_has_feature?(:authentication_password) || driver_has_feature?(:authentication_key)
end

#driver_has_bucket_location_feature?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/deltacloud/helpers/application_helper.rb', line 57

def driver_has_bucket_location_feature?
  driver.features(:buckets).each do |feat|
    return true if feat.name == :bucket_location
  end
  false
end

#driver_has_feature?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/deltacloud/helpers/application_helper.rb', line 44

def driver_has_feature?(feature_name)
  not driver.features(:instances).select{ |f| f.name.eql?(feature_name) }.empty?
end

#filter_all(model) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/deltacloud/helpers/application_helper.rb', line 64

def filter_all(model)
    filter = {}
    filter.merge!(:id => params[:id]) if params[:id]
    filter.merge!(:architecture => params[:architecture]) if params[:architecture]
    filter.merge!(:owner_id => params[:owner_id]) if params[:owner_id]
    filter.merge!(:state => params[:state]) if params[:state]
    filter = nil if filter.keys.size.eql?(0)
    singular = model.to_s.singularize.to_sym
    @elements = driver.send(model.to_sym, credentials, filter)
    instance_variable_set(:"@#{model}", @elements)
    respond_to do |format|
      format.html { haml :"#{model}/index" }
      format.xml { haml :"#{model}/index" }
      format.json { convert_to_json(singular, @elements) }
    end
end

#instance_action(name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/deltacloud/helpers/application_helper.rb', line 104

def instance_action(name)
  @instance = driver.send(:"#{name}_instance", credentials, params["id"])

  return redirect(instances_url) if name.eql?(:destroy) or @instance.class!=Instance

  respond_to do |format|
    format.xml { haml :"instances/show" }
    format.html { haml :"instances/show" }
    format.json {convert_to_json(:instance, @instance) }
  end
end

#instance_action_method(action) ⇒ Object



40
41
42
# File 'lib/deltacloud/helpers/application_helper.rb', line 40

def instance_action_method(action)
  collections[:instances].operations[action.to_sym].method
end

#render_cdata(text) ⇒ Object



121
122
123
# File 'lib/deltacloud/helpers/application_helper.rb', line 121

def render_cdata(text)
  "<pem><![CDATA[#{text.strip}]]></pem>"
end

#report_error(status, template) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/deltacloud/helpers/application_helper.rb', line 95

def report_error(status, template)
  @error = request.env['sinatra.error']
  response.status = status
  respond_to do |format|
    format.xml { haml :"errors/#{template}", :layout => false }
    format.html { haml :"errors/#{template}" }
  end
end

#show(model) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/deltacloud/helpers/application_helper.rb', line 81

def show(model)
  @element = driver.send(model, credentials, { :id => params[:id]} )
  instance_variable_set("@#{model}", @element)
  if @element
    respond_to do |format|
      format.html { haml :"#{model.to_s.pluralize}/show" }
      format.xml { haml :"#{model.to_s.pluralize}/show" }
      format.json { convert_to_json(model, @element) }
    end
  else
      report_error(404, 'not_found')
  end
end