Skip to content

Commit

Permalink
Merge pull request #242 from alphagov/sorting-feedback
Browse files Browse the repository at this point in the history
Add sorting to organisation summary table
  • Loading branch information
fofr committed May 26, 2015
2 parents 128fd7b + 6bd7a87 commit 99bb86f
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Expand Up @@ -3,6 +3,7 @@
@import "select2";
@import "select2-bootstrap";
@import "breadcrumbs";
@import "sortable_table";

thead {
background-color: $gray-lighter;
Expand Down
26 changes: 26 additions & 0 deletions app/assets/stylesheets/sortable_table.scss
@@ -0,0 +1,26 @@
.table-sortable {
.sortable-column {
padding: 0;
}

.sortable-column div {
padding: $table-cell-padding;
}

.sortable-column a:hover,
.sortable-column a:focus {
text-decoration: none;

div {
background-color: #ccc;
}
}

.sorted-column div {
background-color: #ddd;
}

.sorted-column .glyphicon {
margin-left: 5px;
}
}
12 changes: 9 additions & 3 deletions app/controllers/anonymous_feedback/organisations_controller.rb
Expand Up @@ -4,15 +4,21 @@ class AnonymousFeedback::OrganisationsController < AuthorisationController
def show
authorize! :read, :anonymous_feedback

api_response = fetch_organisation_summary_from_support_api
if %w(path last_7_days last_30_days last_90_days).include? params[:ordering]
@ordering = params[:ordering]
else
@ordering = 'last_7_days'
end

api_response = fetch_organisation_summary_from_support_api(@ordering)

@organisation_title = api_response["title"]
@content_items = OrganisationSummaryPresenter.new(api_response)
end

private
def fetch_organisation_summary_from_support_api
support_api.organisation_summary(params[:slug])
def fetch_organisation_summary_from_support_api(ordering)
support_api.organisation_summary(params[:slug], ordering: ordering)
end

def support_api
Expand Down
6 changes: 1 addition & 5 deletions app/presenters/organisation_summary_presenter.rb
Expand Up @@ -8,10 +8,6 @@ def initialize(api_response)

private
def present_results(results)
sort_results(results).map { |entry| OpenStruct.new(entry) }
end

def sort_results(results)
results.sort_by { |content_item| content_item["path"] }
results.map { |entry| OpenStruct.new(entry) }
end
end
27 changes: 22 additions & 5 deletions app/views/anonymous_feedback/organisations/show.html.erb
Expand Up @@ -4,13 +4,30 @@
<% breadcrumb :feedex %>

<table class="table table-bordered" data-module="filterable-table">
<table class="table table-bordered table-sortable" data-module="filterable-table">
<thead>
<tr class="table-header">
<th style="width: 800px">Page</th>
<th>7 days</th>
<th>30 days</th>
<th>90 days</th>
<% {
path: 'Page',
last_7_days: '7 days',
last_30_days: '30 days',
last_90_days: '90 days',
}.each do |param, name| %>
<% sorted = @ordering == param.to_s %>
<th class="sortable-column<% if sorted %> sorted-column<% end %>">
<% if sorted %>
<div>
<%= name %><span class="glyphicon glyphicon-arrow-down"></span>
</div>
<% else %>
<a href="?ordering=<%= param %>" class="link-inherit" title="Sort table by this column">
<div>
<%= name %>
</div>
</a>
<% end %>
</th>
<% end %>
</tr>
<tr class="if-no-js-hide table-header-secondary">
<td colspan="4">
Expand Down
69 changes: 69 additions & 0 deletions spec/features/feedex_organisation_summary_spec.rb
@@ -0,0 +1,69 @@
require 'rails_helper'
require 'gds_api/test_helpers/support_api'

feature "Summary of Organisation feedback" do
include GdsApi::TestHelpers::SupportApi
background do
login_as create(:user)
end

before do
stub_summary_sorted_by('last_7_days')
explore_anonymous_feedback_with(organisation: "Cabinet Office")
end

scenario "defaults to sorting feedback by last 7 days" do
expect(page).to have_content("Feedback for Cabinet Office")
expect(organisation_summary_results).to eq(organisation_summary)
expect(page).to have_selector('th.sorted-column', text: "7 days")
end

scenario "organisation feedback table can be sorted by path, 7, 30 and 90 days" do
{
path: 'Page',
last_7_days: '7 days',
last_30_days: '30 days',
last_90_days: '90 days',
}.each do |param, name|
stub_summary_sorted_by(param.to_s)
within '.table-sortable thead' do
click_on name
end
expect(organisation_summary_results).to eq(organisation_summary)
expect(page).to have_selector('th.sorted-column', text: name)
expect(page).to have_no_selector('th a', text: name)
end
end

def stub_summary_sorted_by(ordering)
stub_anonymous_feedback_organisation_summary('cabinet-office', ordering, {
"title" => "Cabinet Office",
"anonymous_feedback_counts" => [
{ path: '/done-well', last_7_days: 5, last_30_days: 10, last_90_days: 20 },
{ path: '/not-bad-my-friend' },
{ path: '/fair-enough' },
],
})
end

def organisation_summary
[
{
"Page" => "/done-well",
"7 days" => "5 items",
"30 days" => "10 items",
"90 days" => "20 items",
}, {
"Page" => "/not-bad-my-friend",
"7 days" => "0 items",
"30 days" => "0 items",
"90 days" => "0 items",
}, {
"Page" => "/fair-enough",
"7 days" => "0 items",
"30 days" => "0 items",
"90 days" => "0 items",
}
]
end
end
34 changes: 34 additions & 0 deletions spec/features/feedex_spec.rb
Expand Up @@ -65,4 +65,38 @@

expect(page).to have_content("There’s no feedback for this URL.")
end

scenario "exploring feedback by organisation" do
stub_anonymous_feedback_organisation_summary('cabinet-office', 'last_7_days', {
"title" => "Cabinet Office",
"anonymous_feedback_counts" => [
{ path: '/done-well', last_7_days: 5, last_30_days: 10, last_90_days: 20 },
{ path: '/not-bad-my-friend' },
{ path: '/fair-enough' },
],
})

organisation_summary = [
{
"Page" => "/done-well",
"7 days" => "5 items",
"30 days" => "10 items",
"90 days" => "20 items",
}, {
"Page" => "/not-bad-my-friend",
"7 days" => "0 items",
"30 days" => "0 items",
"90 days" => "0 items",
}, {
"Page" => "/fair-enough",
"7 days" => "0 items",
"30 days" => "0 items",
"90 days" => "0 items",
}
]

explore_anonymous_feedback_with(organisation: "Cabinet Office")
expect(page).to have_content("Feedback for Cabinet Office")
expect(organisation_summary_results).to eq(organisation_summary)
end
end
6 changes: 0 additions & 6 deletions spec/presenters/organisation_summary_presenter_spec.rb
Expand Up @@ -42,11 +42,5 @@
it "should match api_response's `anonymous_feedback_counts`" do
expect(presenter.size).to eql(3)
end

it "should sort `anonymous_feedback_counts` by `path`" do
expect(presenter[0].path).to eql(path_a)
expect(presenter[1].path).to eql(path_b)
expect(presenter[2].path).to eql(path_c)
end
end
end
15 changes: 13 additions & 2 deletions spec/support/app_actions.rb
Expand Up @@ -10,9 +10,14 @@ def explore_anonymous_feedback_with(options)

click_on "Feedback explorer"
assert page.has_title?("Anonymous Feedback"), page.html
fill_in 'URL', with: options[:url]

click_on "Explore by URL"
if options[:url].present?
fill_in 'URL', with: options[:url]
click_on "Explore by URL"
else
select options[:organisation], from: 'Organisation'
click_on "Explore by organisation"
end

expect(page).to have_content("Feedback for")
end
Expand All @@ -23,6 +28,12 @@ def feedex_results
results.collect { |row| Hash[first_row.zip(row)] }
end

def organisation_summary_results
all_cells = find('table').all('tr').map { |row| row.all('th, td').map { |cell| cell.text.strip } }
first_row, results = all_cells[0], all_cells[2..-1]
results.collect { |row| Hash[first_row.zip(row)] }
end

def user_fills_out_time_constraints(details)
fill_in "MUST be published by", with: details[:needed_by_date]
fill_in "MUST NOT be published BEFORE", with: details[:not_before_date]
Expand Down

0 comments on commit 99bb86f

Please sign in to comment.