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

#action_method(action, collection) ⇒ Object



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

def action_method(action, collection)
  collections[collection].operations[action.to_sym].method
end


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(text = nil, &block) ⇒ Object



128
129
130
131
# File 'lib/deltacloud/helpers/application_helper.rb', line 128

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

#driver_auth_feature_nameObject



56
57
58
59
# File 'lib/deltacloud/helpers/application_helper.rb', line 56

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)


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

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

#driver_has_bucket_location_feature?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'lib/deltacloud/helpers/application_helper.rb', line 61

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, collection_name = :instances) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#filter_all(model) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/deltacloud/helpers/application_helper.rb', line 68

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/deltacloud/helpers/application_helper.rb', line 108

def instance_action(name)
  original_instance = driver.instance(credentials, :id => params[:id])

  # If original instance doesn't include called action
  # return with 405 error (Method is not Allowed)
  unless driver.instance_actions_for(original_instance.state).include?(name.to_sym)
    return report_error(405, 'not_allowed')
  end

  @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)
  action_method(action, :instances)
end


137
138
139
140
141
142
143
144
145
146
# File 'lib/deltacloud/helpers/application_helper.rb', line 137

def link_to_action(action, url, method)
  capture_haml do
    haml_tag :form, :method => :post, :action => url, :class => :link do
      haml_tag :input, :type => :hidden, :name => '_method', :value => method
      haml_tag :button, :type => :submit do 
        haml_concat action
      end
    end
  end
end

#render_cdata(text) ⇒ Object



133
134
135
# File 'lib/deltacloud/helpers/application_helper.rb', line 133

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

#report_error(status, template) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/deltacloud/helpers/application_helper.rb', line 99

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



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/deltacloud/helpers/application_helper.rb', line 85

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