On 05/13/2011 04:20 PM, jtomasek(a)redhat.com wrote:
From: Jiri Tomasek<jtomasek(a)redhat.com>
---
src/app/controllers/application_controller.rb | 19 ++++++++++++++++++
src/app/controllers/deployments_controller.rb | 2 +
src/app/controllers/instances_controller.rb | 2 +
src/app/controllers/pools_controller.rb | 3 ++
src/app/views/layouts/_breadcrumbs.haml | 5 ++++
src/app/views/layouts/application.haml | 2 +
src/features/breadcrumbs.feature | 26 +++++++++++++++++++++++++
7 files changed, 59 insertions(+), 0 deletions(-)
create mode 100644 src/app/views/layouts/_breadcrumbs.haml
create mode 100644 src/features/breadcrumbs.feature
diff --git a/src/app/controllers/application_controller.rb
b/src/app/controllers/application_controller.rb
index 746811b..5cb4fff 100644
--- a/src/app/controllers/application_controller.rb
+++ b/src/app/controllers/application_controller.rb
@@ -224,4 +224,23 @@ class ApplicationController< ActionController::Base
redirect_to(default || session[:return_to])
session[:return_to] = nil
end
+
+ def save_breadcrumb(path, name = nil)
+ session[:breadcrumbs] ||= []
+ breadcrumbs = session[:breadcrumbs]
+ name ||= controller_name
+ viewstate = @viewstate ? @viewstate.id : nil
+
+ unless breadcrumbs.empty?
+ unless path == breadcrumbs.last[:path]
+ shifted_breadcrumb = breadcrumbs.shift unless breadcrumbs.length< 3
Please add something like:
#TODO waiting for the ViewState to be implemented
here. A code that's commented out without an obvious reason may be
confusing.
+
#ViewState.find(shifted_breadcrumb[:viewstate]).mark_for_deletion if
shifted_breadcrumb[:viewstate]
+ breadcrumbs.push({:name => name, :path => path, :viewstate =>
viewstate})
+ end
+ else
+ breadcrumbs.push({:name => name, :path => path, :viewstate =>
viewstate})
+ end
+
+ session[:breadcrumbs] = breadcrumbs
+ end
end
diff --git a/src/app/controllers/deployments_controller.rb
b/src/app/controllers/deployments_controller.rb
index bdf40de..e36039c 100644
--- a/src/app/controllers/deployments_controller.rb
+++ b/src/app/controllers/deployments_controller.rb
@@ -4,6 +4,7 @@ class DeploymentsController< ApplicationController
before_filter :load_deployment, :only => [:edit, :update]
def index
+ save_breadcrumb(deployments_path(:viewstate => @viewstate ? @viewstate.id :
nil))
respond_to do |format|
format.html
format.json { render :json => @deployments }
@@ -68,6 +69,7 @@ class DeploymentsController< ApplicationController
init_new_deployment_attrs
@tab_captions = ['Properties', 'Instances', 'Provider
Services', 'Required Services', 'History',
'Permissions','Operation']
@details_tab = params[:details_tab].blank? ? 'properties' :
params[:details_tab]
+ save_breadcrumb(deployment_path(@deployment), @deployment.name)
respond_to do |format|
format.js do
if @url_params.delete :details_pane
diff --git a/src/app/controllers/instances_controller.rb
b/src/app/controllers/instances_controller.rb
index f04db3c..dbb77d8 100644
--- a/src/app/controllers/instances_controller.rb
+++ b/src/app/controllers/instances_controller.rb
@@ -6,6 +6,7 @@ class InstancesController< ApplicationController
def index
@params = params
@search_term = params[:q]
+ save_breadcrumb(instances_path(:viewstate => @viewstate ? @viewstate.id : nil))
if @search_term.blank?
load_instances
else
@@ -94,6 +95,7 @@ class InstancesController< ApplicationController
@url_params = params.clone
@tab_captions = ['Properties', 'History', 'Permissions']
@details_tab = params[:details_tab].blank? ? 'properties' :
params[:details_tab]
+ save_breadcrumb(instance_path(@instance), @instance.name)
respond_to do |format|
format.js do
if @url_params.delete :details_pane
diff --git a/src/app/controllers/pools_controller.rb
b/src/app/controllers/pools_controller.rb
index ea09745..c2adc6f 100644
--- a/src/app/controllers/pools_controller.rb
+++ b/src/app/controllers/pools_controller.rb
@@ -4,6 +4,8 @@ class PoolsController< ApplicationController
before_filter :load_pools, :only => [:show]
def index
+ save_breadcrumb(pools_path(:viewstate => @viewstate ? @viewstate.id : nil))
+
@search_term = params[:q]
if @search_term.blank?
load_pools
@@ -23,6 +25,7 @@ class PoolsController< ApplicationController
load_instances
@tab_captions = ['Properties', 'Deployments', 'Instances',
'History', 'Permissions']
@details_tab = params[:details_tab].blank? ? 'properties' :
params[:details_tab]
+ save_breadcrumb(pool_path(@pool), @pool.name)
respond_to do |format|
format.js do
if @url_params.delete :details_pane
diff --git a/src/app/views/layouts/_breadcrumbs.haml
b/src/app/views/layouts/_breadcrumbs.haml
new file mode 100644
index 0000000..5b7c010
--- /dev/null
+++ b/src/app/views/layouts/_breadcrumbs.haml
@@ -0,0 +1,5 @@
+- unless session[:breadcrumbs].blank?
+ #nav-hist-bar
+ - session[:breadcrumbs][0..-2].each do |breadcrumb|
+»
+ = link_to breadcrumb[:name].titleize, breadcrumb[:path]
diff --git a/src/app/views/layouts/application.haml
b/src/app/views/layouts/application.haml
index 763e3e1..26d51aa 100644
--- a/src/app/views/layouts/application.haml
+++ b/src/app/views/layouts/application.haml
@@ -33,6 +33,8 @@
%nav.container_16
= render_navigation({:expand_all => true, :level => 1})
= render_navigation({:expand_all => true, :level => 2})
+ = render :partial => 'layouts/breadcrumbs'
+
#content
#saved-searches
Saved searches
diff --git a/src/features/breadcrumbs.feature b/src/features/breadcrumbs.feature
new file mode 100644
index 0000000..3fcccf4
--- /dev/null
+++ b/src/features/breadcrumbs.feature
@@ -0,0 +1,26 @@
+Feature: Breadcrumbs
+ In order to get back to visited page
+ As a user
+ I want the application to include a breadcrumb trail for site navigation
+
+ Background:
+ Given I am an authorised user
+ And I am logged in
+
+ Scenario: Save breadcrumb and use it for navigation
+ Given I am on the deployments page
+ And a pool "testpool" exists
+ When I go to the pools page
+ Then I should see "testpool"
+ When I follow "testpool"
+ Then I should see "Pools"
+ When I follow "Pools"
+ Then I should be on the pools page
+
+ Scenario: Dont create breadcrumb when reloading or pointing to the same route
+ Given a pool "testpool" exists
+ And I am on the deployments page
+ When I go to the pools page
+ And I follow "testpool"
+ When I follow "testpool"
+ Then I should see "Deployments"
Hey Jiri,
One note inline.
Between the Deployments, Instances and Pools, it seems to behave correctly.
However, there is a glitch when it comes to pages that display the
breadcrumb trail, but don't write to it.
Example: I log in, and go to the Pools page. It will get saved into the
session[:breadcrumbs] but it will not show in the UI, because we're on
that page now.
If I then go to, say, Admin Users page, the Pools entry should be
displayed in the breadcrumbs, but it's not.
Now, I realize that you've only implemented this only for a few pages.
However, there will be pages that will show the breadcrumb trail but
will not write to it. That means we need a way of shifting the
session[:breadcrumbs] array without adding a new item to it.
Here's what I think we should do:
Move the breadcrumb-shifting code outside of the save_breadcrumb method.
And make it so that when you call it, it will set the breadcrumbs so
that the latest one gets displayed.
And if you call it multiple times, nothing will happen (unless a
breadcrumb item is saved).
You can then call this function explicitly from the save_breadcrumb
method and implicitly on actions that don't generate new breadcrumbs.
This is the code sketch of the idea:
# initial value
session[:breadcrumbs] = [nil, nil]
def shift_breadcrumbs
session[:breadcrumbs].shift unless session[:breadcrumbs].length < 3
end
def save_breadcrumb(path, name = nil)
breadcrumbs = session[:breadcrumbs]
name ||= controller_name
viewstate = @viewstate ? @viewstate.id : nil
breadcrumbs.push({:name => name,
:path => path, :viewstate => viewstate})
unless path == breadcrumbs.last[:path]
shifted_breadcrumb = shift_breadcrumbs
#ViewState.find(shifted_breadcrumb[:viewstate]).mark_for_deletion
if shifted_breadcrumb[:viewstate]
end
session[:breadcrumbs] = breadcrumbs
end
# app/layouts/_breadcrumbs.rb
- session[:breadcrumbs][0..2].each do |breadcrumb|
- if breadcrumb
»
= link_to breadcrumb[:name].titleize, breadcrumb[:path]
Then, if the ProviderAccounts actions don't write to the breadcrumb
trail, we can put this in the controller:
# before_filter :shift_breadcrumbs
Does this make sense?
Thomas
--
No trees were killed to send this message, but a large number of
electrons were terribly inconvenienced.