Skip to content

Commit

Permalink
Merge pull request #529 from unepwcmc/ticket-206
Browse files Browse the repository at this point in the history
Ticket 206
  • Loading branch information
Levia authored Sep 17, 2020
2 parents 181b00b + e8abbc2 commit d7134fd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
18 changes: 11 additions & 7 deletions app/controllers/assets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class AssetsController < ApplicationController
TYPES = %w(protected_area country region).freeze

def tiles
if params[:type] == "protected_area"
image = AssetGenerator.protected_area_tile(protected_area)
elsif params[:type] == "country"
image = AssetGenerator.country_tile(country)
else
raise_404
end
area_type = params[:type]
raise_404 unless TYPES.include?(area_type)

method_name = "#{area_type}_tile"
image = AssetGenerator.send(method_name, send(area_type))

send_data image, type: 'image/png', disposition: 'inline'
rescue AssetGenerator::AssetGenerationFailedError
Expand All @@ -22,4 +22,8 @@ def protected_area
def country
@country ||= Country.where(iso: params[:id]).first
end

def region
@region ||= Region.where(iso: params[:id]).first
end
end
5 changes: 4 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8
module ApplicationHelper
include ActionView::Helpers::NumberHelper
include BemHelper

COVER_HELPERS = {
Expand Down Expand Up @@ -71,7 +72,7 @@ def protected_area_cover(protected_area, with_tag: true)
cover_placeholder(protected_area.class),
{
alt: protected_area.name,
class: 'image' #TODO find a way to add classes via parameters
class: 'image'
}.merge(data)
)
end
Expand All @@ -90,6 +91,8 @@ def country_cover(country, with_tag: true)
end

def region_cover(region, with_tag: true)
version = Rails.application.secrets.mapbox[:version]
image_params = {id: region.iso, type: "region", version: version}
return tiles_path(image_params) unless with_tag

image_tag(
Expand Down
6 changes: 5 additions & 1 deletion app/javascript/components/search/SearchAreasResultsItems.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div>
<div class="search__results-bar">
<h2>{{ results.title }} ({{ results.total }})</h2>
<h2>{{ results.title }} ({{ totalAsString(results.total) }})</h2>
</div>
<div class="cards--search-results-areas">
<card-search-result-area
Expand Down Expand Up @@ -49,6 +49,10 @@ export default {
methods: {
requestMore (requestedPage) {
this.$emit('request-more', requestedPage)
},
totalAsString (total) {
return parseFloat(total).toLocaleString()
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions app/serializers/search/areas_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Search::AreasSerializer < Search::BaseSerializer

def initialize(search, geo_type=nil)
super(search)
@aggregations = @search.aggregations
Expand Down Expand Up @@ -53,18 +52,20 @@ def areas_ary(geo_type, areas)
def region_hash(region)
{
title: region.name,
totalAreas: "#{region.protected_areas.count} #{I18n.t('search.protected-areas')}",
url: region_path(iso: region.iso)
totalAreas: "#{areas_count(region)} #{I18n.t('search.protected-areas')}",
url: region_path(iso: region.iso),
image: ApplicationController.helpers.region_cover(region, with_tag: false)
}
end

def country_hash(country)
_slug = slug(country.name)
{
countryFlag: ActionController::Base.helpers.image_url("flags/#{_slug}.svg"),
totalAreas: "#{country.protected_areas.count} #{I18n.t('search.protected-areas')}",
totalAreas: "#{areas_count(country)} #{I18n.t('search.protected-areas')}",
title: country.name,
url: country_path(iso: country.iso_3)
url: country_path(iso: country.iso_3),
image: ApplicationController.helpers.country_cover(country, with_tag: false)
}
end

Expand All @@ -84,4 +85,8 @@ def slug(name)
def total_pages(items_no)
(items_no / DEFAULT_PAGE_SIZE).ceil
end

def areas_count(model)
ApplicationController.helpers.commaify(model.protected_areas.count)
end
end
9 changes: 9 additions & 0 deletions lib/modules/asset_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def self.country_tile country
''#fallback_tile
end

def self.region_tile region
raise AssetGenerationFailedError if region.nil?

tile_url = mapbox_url region.geojson({"fill-opacity" => 0, "stroke-width" => 0})
request_tile tile_url[:host], tile_url[:path]
rescue AssetGenerationFailedError
''#fallback_tile
end

def self.link_to asset_id
file_name = "tiles/#{asset_id}"
S3.link_to file_name
Expand Down

0 comments on commit d7134fd

Please sign in to comment.