From: Jan Provaznik <jprovazn(a)redhat.com>
https://www.aeolusproject.org/redmine/issues/2220
Used respond_with helper method, except multi_destroy action which
I think should be used only be web UI.
---
src/app/controllers/catalogs_controller.rb | 39 ++++++++++++++++++----------
src/app/views/catalogs/show.haml | 2 +-
src/config/locales/en.yml | 1 +
src/features/catalogs.feature | 9 +++---
src/features/support/paths.rb | 3 ++
5 files changed, 35 insertions(+), 19 deletions(-)
diff --git a/src/app/controllers/catalogs_controller.rb
b/src/app/controllers/catalogs_controller.rb
index e6c2e44..1cda1e8 100644
--- a/src/app/controllers/catalogs_controller.rb
+++ b/src/app/controllers/catalogs_controller.rb
@@ -1,5 +1,9 @@
class CatalogsController < ApplicationController
before_filter :require_user
+ respond_to :html
+ # I think multi_destroy action is required only by
+ # web UI, so don't bother about json response in this case
+ respond_to :json, :except => :multi_destroy
def top_section
:administer
@@ -10,12 +14,14 @@ class CatalogsController < ApplicationController
@catalogs = Catalog.list_for_user(current_user, Privilege::VIEW)
save_breadcrumb(catalogs_path(:viewstate => @viewstate ? @viewstate.id : nil))
set_header
+ respond_with @catalogs
end
def new
require_privilege(Privilege::CREATE, Catalog)
@catalog = Catalog.new(params[:catalog]) # ...when should there be params on new?
load_pools
+ respond_with @catalog
end
def show
@@ -27,6 +33,7 @@ class CatalogsController < ApplicationController
{ :name => t("catalog_entries.index.name"), :sort_attr => :name },
{ :name => t('catalog_entries.index.url'), :sortable => false }
]
+ respond_with @catalog
end
def create
@@ -35,48 +42,52 @@ class CatalogsController < ApplicationController
require_privilege(Privilege::MODIFY, @catalog.pool)
if @catalog.save
flash[:notice] = t('catalogs.created', :count => 1)
- redirect_to catalogs_path and return
else
load_pools
- render :new and return
end
+ respond_with @catalog
end
def edit
@catalog = Catalog.find(params[:id])
load_pools
require_privilege(Privilege::MODIFY, @catalog)
+ respond_with @catalog
end
def update
@catalog = Catalog.find(params[:id])
require_privilege(Privilege::MODIFY, @catalog)
require_privilege(Privilege::MODIFY, @catalog.pool)
-
if @catalog.update_attributes(params[:catalog])
flash[:notice] = t('catalogs.updated', :count => 1)
- redirect_to catalogs_url
- else
- render :action => 'edit'
end
+ respond_with @catalog
end
def multi_destroy
+ failed = []
Catalog.find(params[:catalogs_selected]).to_a.each do |catalog|
require_privilege(Privilege::MODIFY, catalog)
- catalog.destroy
+ unless catalog.destroy
+ failed << catalog.name
+ end
+ end
+ unless failed.empty?
+ flash[:error] = "#{t('catalogs.destroy_failed')}: #{failed.join(',
')}"
+ end
+ respond_to do |format|
+ format.html { redirect_to catalogs_path }
end
- redirect_to catalogs_path
end
def destroy
- catalog = Catalog.find(params[:id])
- require_privilege(Privilege::MODIFY, catalog)
- catalog.destroy
-
- respond_to do |format|
- format.html { redirect_to catalogs_path }
+ @catalog = Catalog.find(params[:id])
+ require_privilege(Privilege::MODIFY, @catalog)
+ unless @catalog.destroy
+ flash[:notice] = t('catalogs.destroy_failed', :count => 1)
end
+ respond_with @catalog
end
private
diff --git a/src/app/views/catalogs/show.haml b/src/app/views/catalogs/show.haml
index fafd962..29825b9 100644
--- a/src/app/views/catalogs/show.haml
+++ b/src/app/views/catalogs/show.haml
@@ -5,7 +5,7 @@
= link_to 'New Catalog', new_catalog_url, :class => 'button
primary', :id => 'new_catalog_entry_button'
.button-group
= link_to 'Edit', edit_catalog_path(@catalog), :class => 'button
pill', :id => 'edit_button'
- = button_to "Delete", catalog_path(@catalog), :method =>
'delete', :confirm => "Are you sure you want to delete?", :class
=> 'button pill danger', :id => 'delete_button'
+ = button_to "Delete", catalog_path(@catalog), :method =>
'delete', :confirm => "Are you sure you want to delete?", :class
=> 'button pill danger', :id => 'catalog_delete_button'
.corner
%section.content-section.user
diff --git a/src/config/locales/en.yml b/src/config/locales/en.yml
index 4cb61c9..03d2951 100644
--- a/src/config/locales/en.yml
+++ b/src/config/locales/en.yml
@@ -181,6 +181,7 @@ en:
created:
one: Catalog created!
many: Catalogs created!
+ destroy_failed: Failed to destroy catalog
deployables:
index:
deployables: Deployables
diff --git a/src/features/catalogs.feature b/src/features/catalogs.feature
index caf11f1..15a92cc 100644
--- a/src/features/catalogs.feature
+++ b/src/features/catalogs.feature
@@ -15,7 +15,7 @@ Feature: Manage Catalogs
And I fill in "catalog_name" with "Finance"
And I select "default" from "catalog_pool_id"
And I press "Save"
- Then I should be on the catalogs page
+ Then I should be on Finance's catalog page
And I should see "Finance"
Scenario: Show catalog details
@@ -31,7 +31,7 @@ Feature: Manage Catalogs
And I follow "edit_button"
And I fill in "Engineering" for "catalog[name]"
And I press "Save"
- Then I should be on the catalogs page
+ Then I should be on Engineering's catalog page
And I should see "Engineering"
And I should not see "Development"
@@ -39,6 +39,7 @@ Feature: Manage Catalogs
Given there is a "Bad" catalog
And I am on the catalogs page
When I follow "Bad"
- And I press "Delete"
+ And show me the page
+ And I press "catalog_delete_button"
Then I should be on the catalogs page
- And I should not see "Bad"
\ No newline at end of file
+ And I should not see "Bad"
diff --git a/src/features/support/paths.rb b/src/features/support/paths.rb
index f3db6ff..cd366df 100644
--- a/src/features/support/paths.rb
+++ b/src/features/support/paths.rb
@@ -26,6 +26,9 @@ module NavigationHelpers
when /^(.*)'s realm page$/i
realm_path(FrontendRealm.find_by_name($1))
+ when /^(.*)'s catalog page$/i
+ catalog_path(Catalog.find_by_name($1))
+
when /the account page/
account_path
--
1.7.6.2