Skip to content

Commit

Permalink
convert EmsCloudController to use restful routes.
Browse files Browse the repository at this point in the history
this commit converts EmsCloudController to use restful routes, and
avoids using @edit.  It converts the form to just do an ajax post with
all of the form data rather than expecting the form data to be in @edit
  • Loading branch information
tenderlove authored and AparnaKarve committed Sep 9, 2015
1 parent cb46e2a commit 52458a6
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 34 deletions.
113 changes: 113 additions & 0 deletions app/controllers/ems_cloud_controller.rb
Expand Up @@ -18,8 +18,77 @@ def index
redirect_to :action => 'show_list'
end

def update
return super unless "save" == params[:button]

assert_privileges("#{model.to_s.underscore}_edit")

@ems = find_by_id_filtered(model, params[:id])
set_model_data @ems, params

if @ems.valid? && @ems.save
@ems.reload
flash = _("%{model} \"%{name}\" was saved") %
{:model => ui_lookup(:model => model.to_s), :name => @ems.name}

# AuditEvent.success(build_saved_audit(update_ems, @edit))

render :update do |page|
page.redirect_to ems_cloud_path(@ems, :flash_msg => flash)
end
else
@ems.errors.each do |field,msg|
add_flash("#{@ems.class.human_attribute_name(field)} #{msg}", :error)
end
drop_breadcrumb(:name => "Edit #{ui_lookup(:table => @table_name)} '#{@ems.name}'",
:url => edit_ems_path(@ems))
render_flash
end
end

def create
return super unless "add" == params[:button]

assert_privileges("#{model.to_s.underscore}_new")

if params[:server_emstype].blank?
add_flash(_("%s is required") % "Type", :error)
render :update do |page|
page.replace("flash_msg_div", :partial=>"layouts/flash_msg")
end
else
@ems = model.model_from_emstype(params[:server_emstype]).new
set_model_data @ems, params

name = ui_lookup(:tables=>table_name)

if @ems.valid? && @ems.save
render :update do |page|
page.redirect_to :action => 'show_list', :flash_msg => _("%{model} \"%{name}\" was saved") % {:model => name, :name => @ems.name}
end
else
@ems.errors.each do |field,msg|
add_flash("#{@ems.class.human_attribute_name(field)} #{msg}", :error)
end

drop_breadcrumb( {:name=>"Add New #{name}", :url=> new_polymorphic_path(self.class.model)} )
render :update do |page|
page.replace("flash_msg_div", :partial=>"layouts/flash_msg")
end
end
end
end

private ############################

def table_name
self.class.table_name
end

def no_blank(thing)
thing.blank? ? nil : thing
end

def get_session_data
@title = ui_lookup(:tables => "ems_cloud")
@layout = "ems_cloud"
Expand All @@ -36,4 +105,48 @@ def set_session_data
session[:ems_cloud_filters] = @filters
session[:ems_cloud_catinfo] = @catinfo
end

def show_link(ems, options = {})
ems_cloud_path(ems.id, options)
end

def set_model_data(ems, params)
ems.name = params[:name]
ems.provider_region = params[:provider_region]
ems.hostname = params[:hostname]
ems.ipaddress = params[:ipaddress]
ems.port = params[:port]
ems.zone = Zone.find_by_name(params[:server_zone] || "default")

if ems.is_a?(EmsVmware)
ems.host_default_vnc_port_start = no_blank(params[:host_default_vnc_port_start])
ems.host_default_vnc_port_end = no_blank(params[:host_default_vnc_port_end])
end

creds = {
:default => {
:default_userid => no_blank(params[:default_userid]),
:default_password => no_blank(params[:default_password]),
:default_verify => no_blank(params[:default_verify]),
}
}

if ems.supports_authentication?(:metrics)
creds[:metrics] = {
:userid => no_blank(params[:metrics_userid]),
:password => no_blank(params[:metrics_password]),
:verify => no_blank(params[:metrics_verify])
}
end

if ems.supports_authentication?(:amqp)
creds[:amqp] = {
:userid => no_blank(params[:amqp_userid]),
:password => no_blank(params[:amqp_password]),
:verify => no_blank(params[:amqp_verify])
}
end
ems.update_authentication(creds)
ems
end
end
34 changes: 20 additions & 14 deletions app/controllers/ems_common.rb
Expand Up @@ -9,24 +9,24 @@ def show
@ems = @record = identify_record(params[:id])
return if record_no_longer_exists?(@ems)

@gtl_url = "/#{@table_name}/show/" << @ems.id.to_s << "?"
@gtl_url = show_link(@ems) << "?"
@showtype = "config"
drop_breadcrumb({:name=>ui_lookup(:tables=>@table_name), :url=>"/#{@table_name}/show_list?page=#{@current_page}&refresh=y"}, true)

if ["download_pdf","main","summary_only"].include?(@display)
get_tagdata(@ems)
drop_breadcrumb( {:name=>@ems.name + " (Summary)", :url=>"/#{@table_name}/show/#{@ems.id}"} )
drop_breadcrumb( {:name=>@ems.name + " (Summary)", :url=>show_link(@ems)} )
@showtype = "main"
set_summary_pdf_data if ["download_pdf","summary_only"].include?(@display)
elsif @display == "props"
drop_breadcrumb( {:name=>@ems.name+" (Properties)", :url=>"/#{@table_name}/show/#{@ems.id}?display=props"} )
drop_breadcrumb( {:name=>@ems.name+" (Properties)", :url=>show_link(@ems, :display => "props") } )
elsif @display == "ems_folders"
if params[:vat]
drop_breadcrumb({:name=>@ems.name+" (VMs & Templates)",
:url=>"/#{@table_name}/show/#{@ems.id}?display=ems_folders&vat=true"})
:url=>show_link(@ems, :display => "ems_folder", :vat => "true")})
else
drop_breadcrumb({:name=>@ems.name+" (Hosts & Clusters)",
:url=>"/#{@table_name}/show/#{@ems.id}?display=ems_folders"} )
:url=>show_link(@ems, :display => "ems_folders")} )
end
@showtype = "config"
build_dc_tree
Expand All @@ -37,7 +37,7 @@ def show
@timeline = @timeline_filter = true
@lastaction = "show_timeline"
tl_build_timeline # Create the timeline report
drop_breadcrumb( {:name=>"Timelines", :url=>"/#{@table_name}/show/#{@record.id}?refresh=n&display=timeline"} )
drop_breadcrumb(:name=>"Timelines", :url=>show_link(@record.id, :refresh => "n", :display => "timeline"))
elsif ["instances","images","miq_templates","vms"].include?(@display) || session[:display] == "vms" && params[:display].nil?
if @display == "instances"
title = "Instances"
Expand All @@ -52,7 +52,7 @@ def show
title = "VMs"
kls = Vm
end
drop_breadcrumb( {:name=>@ems.name+" (All #{title})", :url=>"/#{@table_name}/show/#{@ems.id}?display=#{@display}"} )
drop_breadcrumb(:name=>@ems.name+" (All #{title})", :url=>show_link(@ems, :display => @display))
@view, @pages = get_view(kls, :parent=>@ems) # Get the records (into a view) and the paginator
@showtype = @display
if @view.extras[:total_count] && @view.extras[:auth_count] &&
Expand All @@ -61,7 +61,7 @@ def show
end
elsif @display == "availability_zones" || session[:display] == "availability_zones" && params[:display].nil?
title = "Availability Zones"
drop_breadcrumb( {:name=>@ems.name+" (All #{title})", :url=>"/#{@table_name}/show/#{@ems.id}?display=#{@display}"} )
drop_breadcrumb(:name=>@ems.name+" (All #{title})", :url=>show_link(@ems, :display => @display))
@view, @pages = get_view(AvailabilityZone, :parent=>@ems) # Get the records (into a view) and the paginator
@showtype = @display
if @view.extras[:total_count] && @view.extras[:auth_count] &&
Expand Down Expand Up @@ -179,15 +179,15 @@ def show
end
elsif @display == "cloud_tenants" || (session[:display] == "cloud_tenants" && params[:display].nil?)
title = "Cloud Tenants"
drop_breadcrumb( {:name => "#{@ems.name} (All #{title})", :url => "/#{@table_name}/show/#{@ems.id}?display=#{@display}"} )
drop_breadcrumb(:name => "#{@ems.name} (All #{title})", :url => show_link(@ems, :display => @display))
@view, @pages = get_view(CloudTenant, :parent => @ems) # Get the records (into a view) and the paginator
@showtype = @display
if @view.extras[:total_count] && @view.extras[:auth_count] && @view.extras[:total_count] > @view.extras[:auth_count]
@bottom_msg = "* You are not authorized to view " + pluralize(@view.extras[:total_count] - @view.extras[:auth_count], "other #{title.singularize}") + " on this " + ui_lookup(:tables => @table_name)
end
elsif @display == "flavors" || session[:display] == "flavors" && params[:display].nil?
title = "Flavors"
drop_breadcrumb( {:name=>@ems.name+" (All #{title})", :url=>"/#{@table_name}/show/#{@ems.id}?display=#{@display}"} )
drop_breadcrumb(:name=>@ems.name+" (All #{title})", :url=>show_link(@ems, :display => @display))
@view, @pages = get_view(Flavor, :parent=>@ems) # Get the records (into a view) and the paginator
@showtype = @display
if @view.extras[:total_count] && @view.extras[:auth_count] &&
Expand All @@ -196,15 +196,15 @@ def show
end
elsif @display == "security_groups" || session[:display] == "security_groups" && params[:display].nil?
title = "Security Groups"
drop_breadcrumb( {:name=>@ems.name+" (All #{title})", :url=>"/#{@table_name}/show/#{@ems.id}?display=#{@display}"} )
drop_breadcrumb(:name=>@ems.name+" (All #{title})", :url=>show_link(@ems, :display => @display))
@view, @pages = get_view(SecurityGroup, :parent=>@ems) # Get the records (into a view) and the paginator
@showtype = @display
if @view.extras[:total_count] && @view.extras[:auth_count] &&
@view.extras[:total_count] > @view.extras[:auth_count]
@bottom_msg = "* You are not authorized to view " + pluralize(@view.extras[:total_count] - @view.extras[:auth_count], "other #{title.singularize}") + " on this " + ui_lookup(:tables=>@table_name)
end
elsif @display == "storages" || session[:display] == "storages" && params[:display].nil?
drop_breadcrumb( {:name=>@ems.name+" (All Managed #{ui_lookup(:tables=>"storages")})", :url=>"/#{@table_name}/show/#{@ems.id}?display=storages"} )
drop_breadcrumb(:name=>@ems.name+" (All Managed #{ui_lookup(:tables=>"storages")})", :url=>show_link(@ems, :display => "storages"))
@view, @pages = get_view(Storage, :parent=>@ems) # Get the records (into a view) and the paginator
if @view.extras[:total_count] && @view.extras[:auth_count] &&
@view.extras[:total_count] > @view.extras[:auth_count]
Expand All @@ -221,7 +221,7 @@ def show
elsif @display == "orchestration_stacks" || session[:display] == "orchestration_stacks" && params[:display].nil?
title = "Stacks"
drop_breadcrumb(:name => "#{@ems.name} (All #{title})",
:url => "/#{@table_name}/show/#{@ems.id}?display=#{@display}")
:url => show_link(@ems, :display => @display))
@view, @pages = get_view(OrchestrationStack, :parent => @ems) # Get the records (into a view) and the paginator
@showtype = @display
if @view.extras[:total_count] &&
Expand All @@ -231,7 +231,7 @@ def show
@bottom_msg = "* You are not authorized to view #{count_text} on this #{ui_lookup(:tables => @table_name)}"
end
else # Must be Hosts # FIXME !!!
drop_breadcrumb( {:name=>@ems.name+" (All Managed Hosts)", :url=>"/#{@table_name}/show/#{@ems.id}?display=hosts"} )
drop_breadcrumb(:name=>@ems.name+" (All Managed Hosts)", :url=>show_link(@ems, :display => :hosts))
@view, @pages = get_view(Host, :parent=>@ems) # Get the records (into a view) and the paginator
if @view.extras[:total_count] && @view.extras[:auth_count] &&
@view.extras[:total_count] > @view.extras[:auth_count]
Expand Down Expand Up @@ -1026,4 +1026,10 @@ def model
def permission_prefix
self.class.permission_prefix
end

def show_link(ems, options = {})
url_for(options.merge(:controller => @table_name,
:action => "show",
:id => ems.id))
end
end
8 changes: 7 additions & 1 deletion app/helpers/application_helper.rb
Expand Up @@ -117,7 +117,10 @@ def url_for_record(record, action="show") # Default action is show
end

# Create a url for a record that links to the proper controller
def url_for_db(db, action="show") # Default action is show
def url_for_db(db, action="show", item = nil) # Default action is show
if item && EmsCloud === item
return ems_cloud_path(item.id)
end
if @vm && ["Account", "User", "Group", "Patch", "GuestApplication"].include?(db)
return url_for(:controller => "vm_or_template",
:action => @lastaction,
Expand Down Expand Up @@ -162,6 +165,9 @@ def view_to_url(view, parent=nil)
end
if association == nil
controller, action = db_to_controller(view.db)
if controller == "ems_cloud" && action == "show"
return ems_clouds_path
end
if parent && parent.class.base_model.to_s == "MiqCimInstance" && ["CimBaseStorageExtent","SniaLocalFileSystem"].include?(view.db)
return url_for(:controller=>controller, :action=>action, :id=>parent.id) + "?show="
else
Expand Down
3 changes: 3 additions & 0 deletions app/helpers/application_helper/toolbar_builder.rb
Expand Up @@ -1327,6 +1327,9 @@ def build_toolbar_save_button(tb_buttons, item, parent = nil)
ridx = url.rindex('/') if url
url = url.slice(0..ridx-1) if ridx
end
if item[:full_path]
tb_buttons[button][:full_path] = ERB.new(item[:full_path]).result(binding)
end
tb_buttons[button][:url] = url if item[:url]
tb_buttons[button][:explorer] = true if @explorer && !item[:url] # Add explorer = true if ajax button
if item[:popup]
Expand Down
1 change: 1 addition & 0 deletions app/models/manageiq/providers/cloud_manager.rb
Expand Up @@ -22,6 +22,7 @@ class CloudManager < BaseManager

alias_method :all_cloud_networks, :cloud_networks

validates_presence_of :zone
include HasManyOrchestrationStackMixin

# Development helper method for Rails console for opening a browser to the EMS.
Expand Down
4 changes: 3 additions & 1 deletion app/views/ems_cloud/edit.html.haml
@@ -1 +1,3 @@
= render :partial => "shared/views/ems_common/form"
= form_for(@ems.becomes(EmsCloud)) do |f|
%input{:type => 'hidden', :name => "button", :value => "save"}
= render :partial => "shared/views/ems_common/form"
4 changes: 3 additions & 1 deletion app/views/ems_cloud/new.html.haml
@@ -1 +1,3 @@
= render :partial => "shared/views/ems_common/form"
= form_for(@ems.becomes(EmsCloud)) do |f|
%input{:type => 'hidden', :name => "button", :value => "add"}
= render :partial => "shared/views/ems_common/form"
21 changes: 12 additions & 9 deletions app/views/layouts/_edit_form_buttons.html.haml
@@ -1,13 +1,14 @@
- save_text ||= _("Save Changes")
- save_confirm_text ||= nil
- show_validate_button ||= nil
- action_url ||= "update"
- record_id ||= nil
- noreset ||= nil
- align ||= "right"
- ajax_buttons ||= false
- serialize ||= false
- continue_button ||= nil
- action_url ||= "update"
- record_id ||= nil
- noreset ||= nil
- align ||= "right"
- ajax_buttons ||= false
- serialize ||= false
- continue_button ||= nil
- restful ||= false
- show_cancel_button = %w(user_edit user_update menu_tree zone_edit zone_update ldap_seq_edit rbac_group_edit rbac_group_update rbac_group_field_changed category_edit category_update timeprofile_edit timeprofile_update timeprofile_copy)
- force_cancel_button ||= false
&nbsp;
Expand Down Expand Up @@ -43,13 +44,15 @@
:id => record_id,
:button => "continue")}');")
- else
- function = restful ? "miqRESTAjaxButton" : "miqAjaxButton"
- extra = restful ? ", this" : ""
= button_tag(_("Save"),
:class => 'btn btn-primary',
:alt => save_text,
:title => save_text,
:onclick => "miqAjaxButton('#{url_for(:action => action_url,
:onclick => "#{function}('#{url_for(:action => action_url,
:id => record_id,
:button => "save")}');")
:button => "save")}'#{extra});")
- else
= button_tag(_("Save"),
:class => 'btn btn-primary',
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/quadicon/_quadicon_text.html.haml
Expand Up @@ -70,5 +70,5 @@
%a{:href => url_for_db(db), :title => h(row['key'])}
= truncate_for_quad(row['key'])
- else
%a{:href => url_for_db(db), :title => h(row['name'])}
%a{:href => url_for_db(db, "show", item), :title => h(row['name'])}
= truncate_for_quad(row['name'])
15 changes: 10 additions & 5 deletions config/routes.rb
Expand Up @@ -588,18 +588,18 @@
dialog_load
discover
download_data
edit
index
new
protect
show
show_list
tagging_edit
) +
compare_get,
:post => %w(
button
create
dynamic_checkbox_refresh
dynamic_list_refresh
dynamic_radio_button_refresh
dynamic_text_box_refresh
form_field_changed
listnav_search_selected
panel_control
Expand Down Expand Up @@ -2018,7 +2018,9 @@
CONTROLLER_ACTIONS.each do |controller_name, controller_actions|

# Default route with no action to controller's index action
match "#{controller_name}", :controller => controller_name, :action => :index, :via => :get
unless controller_name == :ems_cloud
match "#{controller_name}", :controller => controller_name, :action => :index, :via => :get
end

# One-by-one get/post routes for defined controllers
if controller_actions.is_a?(Hash)
Expand All @@ -2039,4 +2041,7 @@
end
end
end

resources :ems_cloud, :as => :ems_clouds

end

0 comments on commit 52458a6

Please sign in to comment.