Skip to content

Commit

Permalink
Allow download of links with specific statuses
Browse files Browse the repository at this point in the history
The previous behaviour of clicking a link to download broken links has
been modified so that the user can select specific statuses for which
they want to download links. Only links with the selected status will be
present in the downloaded csv file. They are initially all selected by
default.
  • Loading branch information
Alan Gabbianelli committed Apr 16, 2019
1 parent cf1108e commit f5dee13
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 96 deletions.
17 changes: 17 additions & 0 deletions app/assets/javascripts/local_authorities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
document.addEventListener("DOMContentLoaded", function(event) {
var checkboxes = document.getElementsByClassName('links_status_checkbox');
var links_download_button = document.getElementById('links_download_button');
var url = links_download_button.href.split('?')[0];

for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].addEventListener('change', function() {
var params = [];
for (var j = 0; j < checkboxes.length; j++) {
if (checkboxes[j].checked) {
params.push(checkboxes[j].name + '=' + checkboxes[j].value)
}
}
links_download_button.href = url + '?' + params.join('&')
});
}
});
6 changes: 3 additions & 3 deletions app/controllers/local_authorities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def show
@link_count = links_for_authority.count
end

def broken_links_csv
def links_csv
@authority = LocalAuthority.find_by_slug!(params[:local_authority_slug])
authority_name = @authority.name.parameterize.underscore
data = LocalLinksManager::Export::LinksExporter.new.export_broken_links(@authority)
send_data data, filename: "#{authority_name}_broken_links.csv"
data = LocalLinksManager::Export::LinksExporter.new.export_links(@authority.id, params)
send_data data, filename: "#{authority_name}_links.csv"
end

def bad_homepage_url_and_status_csv
Expand Down
27 changes: 26 additions & 1 deletion app/views/shared/_local_authority_details.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
<%= javascript_include_tag params[:controller] %>

<div class="page-title">
<h1><%= authority.name %></h1>
<p>
Homepage <%= link_to_if(authority.homepage_url, nil, authority.homepage_url) %><br>
<span class="<%= authority.label_status_class %> text-muted"><b><%= authority.homepage_status %></b></span>
<span class="text-muted"><%= authority.homepage_link_last_checked %></span>
<p><%= link_to('Download broken links', broken_links_csv_local_authority_path, class: "btn btn-default btn-s") %></p>
<p>
<%= check_box_tag(:ok, :ok, true, class: 'links_status_checkbox') %>
<%= label_tag(:ok, 'OK') %>
<%= check_box_tag(:broken, :broken, true, class: 'links_status_checkbox') %>
<%= label_tag(:broken, 'Broken') %>
<%= check_box_tag(:caution, :caution, true, class: 'links_status_checkbox') %>
<%= label_tag(:caution, 'Caution') %>
<%= check_box_tag(:missing, :missing, true, class: 'links_status_checkbox') %>
<%= label_tag(:missing, 'Missing') %>
<%= check_box_tag(:pending, :pending, true, class: 'links_status_checkbox') %>
<%= label_tag(:pending, 'Pending') %>
<%= link_to(
'Download links',
links_csv_local_authority_path(
ok: :ok,
broken: :broken,
caution: :caution,
missing: :missing,
pending: :pending
),
class: "btn btn-default btn-s",
id: "links_download_button"
) %>
</p>
</p>
</div>
1 change: 1 addition & 0 deletions config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Rails.application.config.assets.precompile += %w( local_authorities.js )
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

resources 'local_authorities', only: [:index, :show], param: :local_authority_slug do
member do
get 'broken_links_csv'
get 'links_csv'
end
end

Expand Down
71 changes: 39 additions & 32 deletions lib/local-links-manager/export/links_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
module LocalLinksManager
module Export
class LinksExporter
HEADINGS = ["Authority Name", "SNAC", "GSS", "Description", "LGSL", "LGIL", "URL"].freeze
ALL_LINKS_HEADINGS = ["Supported by GOV.UK"].freeze
BROKEN_LINKS_HEADINGS = ["New URL"].freeze
SELECTION = [
"local_authorities.name",
:snac,
:gss,
"services.label as service_label",
"interactions.label as interaction_label",
"links.status as status",
:lgsl_code,
:lgil_code,
:url,
:enabled
].freeze
COMMON_HEADINGS = [
"Authority Name",
"SNAC",
"GSS",
"Description",
"LGSL",
"LGIL",
"URL",
"Supported by GOV.UK"
].freeze
EXTRA_HEADINGS = ["Status", "New URL"].freeze

def self.export_links
path = Rails.root.join("public", "data", 'links_to_services_provided_by_local_authorities.csv')
Expand All @@ -17,54 +37,40 @@ def self.export_links

def export(io)
output = CSV.generate do |csv|
csv << HEADINGS + ALL_LINKS_HEADINGS
csv << COMMON_HEADINGS
records.each do |record|
csv << format(record).push(record.enabled)
csv << format(record)
end
end
io.write(output)
end

def export_broken_links(local_authority_id)
def export_links(local_authority_id, params)
statuses = params.slice('ok', 'broken', 'caution', 'missing', 'pending').keys
CSV.generate do |csv|
csv << HEADINGS + BROKEN_LINKS_HEADINGS
broken_links(local_authority_id).each do |link|
csv << format(link)
csv << COMMON_HEADINGS + EXTRA_HEADINGS
statuses.each do |status|
links(local_authority_id, status).each do |link|
csv << format(link).push(link.status)
end
end
end
end

def records
Link.with_url.joins(:local_authority, :service, :interaction)
.select(
"local_authorities.name",
:snac,
:gss,
"services.label as service_label",
"interactions.label as interaction_label",
:lgsl_code,
:lgil_code,
:url,
:enabled
).order("local_authorities.name", "services.lgsl_code", "interactions.lgil_code").all
.select(*SELECTION)
.order("local_authorities.name", "services.lgsl_code", "interactions.lgil_code").all
end

private

def broken_links(local_authority_id)
Link.enabled_links.broken
def links(local_authority_id, status)
Link.enabled_links.public_send(status)
.where(local_authority_id: local_authority_id)
.joins(:local_authority, :service, :interaction)
.select(
"local_authorities.name",
:snac,
:gss,
"services.label as service_label",
"interactions.label as interaction_label",
:lgsl_code,
:lgil_code,
:url,
).order("services.lgsl_code", "interactions.lgil_code").all
.select(*SELECTION)
.order("services.lgsl_code", "interactions.lgil_code").all
end

def format(record)
Expand All @@ -76,6 +82,7 @@ def format(record)
record.lgsl_code,
record.lgil_code,
record.url,
record.enabled
]
end

Expand Down
14 changes: 12 additions & 2 deletions spec/controllers/local_authorities_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@
end
end

describe "GET broken_links_csv" do
describe "GET links_csv" do
before do
@local_authority = create(:local_authority)
end

it "retrieves HTTP success" do
login_as_stub_user
get :broken_links_csv, params: { local_authority_slug: @local_authority.slug }
get(
:links_csv,
params: {
local_authority_slug: @local_authority.slug,
ok: 'ok',
broken: 'broken',
caution: 'caution',
missing: 'missing',
pending: 'pending'
}
)
expect(response).to have_http_status(200)
expect(response.headers["Content-Type"]).to eq("text/csv")
end
Expand Down
Loading

0 comments on commit f5dee13

Please sign in to comment.