Hey Matt,
ACK. There's a few notes inline.
As for your worry about having the same method accessible both from the
controllers and views, there are 2 idiomatic ways to tackle that,
depending on whether you want the code to reside in the controller or
the helper module.
1) Keep the method in the controller and add this line to the
controller's class definition:
helper_method :filter_view?
That will provide a helper out of the method.
2) If you want to have the code in a helper file, you can include that
helper module in your controller class:
include ApplicationHelper
I tried it and both ways work.
Thomas
--
No trees were killed to send this message, but a large number of
electrons were terribly inconvenienced.
On 05/24/2011 10:54 PM, Matt Wagner wrote:
Implements
https://www.aeolusproject.org/redmine/issues/1575
Standard method of toggling between pretty and filter views. Also
extends to Pools#show.
---
src/app/helpers/application_helper.rb | 11 +++++++++++
src/app/views/layouts/_pretty_filter_toggle.haml | 2 ++
src/app/views/pools/index.haml | 22 +---------------------
src/app/views/pools/show.haml | 4 +++-
src/features/pool.feature | 20 ++++++++++++++++----
src/public/javascripts/application.js | 20 ++++++++++++++++++++
6 files changed, 53 insertions(+), 26 deletions(-)
create mode 100644 src/app/views/layouts/_pretty_filter_toggle.haml
diff --git a/src/app/helpers/application_helper.rb
b/src/app/helpers/application_helper.rb
index a73f293..9afcb84 100644
--- a/src/app/helpers/application_helper.rb
+++ b/src/app/helpers/application_helper.rb
@@ -221,4 +221,15 @@ module ApplicationHelper
"#{obj.owner.first_name} #{obj.owner.last_name}"
end
end
+
+ def pretty_filter_toggle(pretty_link, filter_link)
+ render :partial => 'layouts/pretty_filter_toggle', :locals => {
+ :pretty_link => pretty_link,
+ :filter_link => filter_link
+ }
+ end
+
+ def filter_view?
+ params.include?(:view) and params[:view] == 'filter'
If the :view key is not present in params then `params[:view]` returns
nil. Therefore, it's sufficient to just use this:
params[:view] == 'filter'
Will someone please think of the poor bytes?
+ end
end
diff --git a/src/app/views/layouts/_pretty_filter_toggle.haml
b/src/app/views/layouts/_pretty_filter_toggle.haml
new file mode 100644
index 0000000..c985915
--- /dev/null
+++ b/src/app/views/layouts/_pretty_filter_toggle.haml
@@ -0,0 +1,2 @@
+= link_to "Pretty View", pretty_link, :id => 'pretty_view', :class
=> (filter_view? ? '' : 'active')
+= link_to "Filter View", filter_link, :id => 'filter_view', :class
=> (filter_view? ? 'active' : '')
\ No newline at end of file
diff --git a/src/app/views/pools/index.haml b/src/app/views/pools/index.haml
index 35fcd43..d04782e 100644
--- a/src/app/views/pools/index.haml
+++ b/src/app/views/pools/index.haml
@@ -1,29 +1,9 @@
= render :partial => 'header_index'
= render :partial => 'scoreboard_index'
-= link_to "Pretty View", pools_path(:view => 'pretty'), :id
=>'pretty_view'
-= link_to "Filtred View", pools_path(:view => 'filter'), :id =>
'filtred_view'
+= pretty_filter_toggle(pools_path(:view => 'pretty'), pools_path(:view =>
'filter'))
%div#view
- if params[:view] == 'filter'
= render :partial => 'layouts/tabpanel'
- else
= render :partial => 'pretty_list'
Trailing newline at EOF ^
-:javascript
- $(document).ready(function (){
- $("#pretty_view").click(function(){
- $.get($(this).attr("href"), $(this).serialize(),
- function(result) {
- $("#view").html(result);
- }, "script");
- return false;
- });
- $("#filtred_view").click(function(){
- $.get($(this).attr("href"), $(this).serialize(),
- function(result) {
- $("#view").html(result);
- $('#details-selected').hide();
- $('#details-view').tabs('destroy').tabs();
- }, "script");
- return false;
- });
- });
diff --git a/src/app/views/pools/show.haml b/src/app/views/pools/show.haml
index 355d33d..9986a23 100644
--- a/src/app/views/pools/show.haml
+++ b/src/app/views/pools/show.haml
@@ -1,8 +1,10 @@
= render :partial => 'header_show'
= render :partial => 'scoreboard_show'
+= pretty_filter_toggle(pool_path(@pool, :view => 'pretty'), pool_path(@pool,
:view => 'filter'))
-# TODO - Alerts aren't implemented just yet
-#= render :partial => 'alerts'
-# TODO: %w(Deployments History Properties Permissions) links/tabs, plus filter
links/icons, go here
-= render :partial => @view, :locals => {:deployments => @pool.deployments}
\ No newline at end of file
+%div#view
+ = render :partial => @view, :locals => {:deployments =>
@pool.deployments}
diff --git a/src/features/pool.feature b/src/features/pool.feature
index 6c88c69..b507050 100644
--- a/src/features/pool.feature
+++ b/src/features/pool.feature
@@ -154,21 +154,33 @@ Feature: Manage Pools
When I delete "mockpool" pool
Then I should get back JSON object with success and errors
- Scenario: Switch pretty view to filtred
+ Scenario: Switch pretty view to filtred view on pools index
Given I am on the pools page
And I see "Overview"
And I should see "expand all"
- When I follow "Filtred View"
+ When I follow "Filter View"
Then I should see "Pools" within "#details-view"
And I should see "Instances" within "#details-view"
And I should see "Deployments" within "#details-view"
And I should not see "expand all"
When I follow "Pretty View"
- Scenario: Switch from filtred view to pretty
+ Scenario: Switch from filtred view to pretty view on pools index
Given I am on the pools page
- And I follow "Filtred View"
+ And I follow "Filter View"
And I should see "Pools" within "#view"
When I follow "Pretty View"
Then I should not see "Pools" within "#view"
And I should see "expand all"
+
+ Scenario: Pools#show pretty view
+ Given a pool "mockpool" exists with deployment "mockdeployment"
+ When I am viewing the pool "mockpool"
+ And I follow "Pretty View"
+ Then I should see "0 Instances" within "#view"
+
+ Scenario: Pools#show filter view
+ Given a pool "mockpool" exists with deployment "mockdeployment"
+ When I am viewing the pool "mockpool"
+ And I follow "Filter View"
+ Then I should not see "0 Instances" within "#view"
\ No newline at end of file
diff --git a/src/public/javascripts/application.js
b/src/public/javascripts/application.js
index af29bd6..4e513c3 100644
--- a/src/public/javascripts/application.js
+++ b/src/public/javascripts/application.js
@@ -167,3 +167,23 @@ $(document).ready(function () {
Conductor.enhanceListView();
Conductor.enhanceDetailsTabs();
});
+
+/* Standard method for switching filter/pretty views */
+$(document).ready(function (){
+$("#pretty_view_toggle").click(function(){
+ $.get($(this).attr("href"), $(this).serialize(),
+ function(result) {
+ $("#view").html(result);
+ }, "script");
+ return false;
+ });
+$("#filter_view").click(function(){
+ $.get($(this).attr("href"), $(this).serialize(),
+ function(result) {
+ $("#view").html(result);
+ $('#details-selected').hide();
+ $('#details-view').tabs('destroy').tabs();
+ }, "script");
+ return false;
+});
+});
I think it would make more sense to move this code to a standalone
function that would be called from $(document).ready. It would make it
cleaner in my opinion.