Skip to content

Commit

Permalink
Merge branch '0.1.5' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
RainbowGiantSquid committed Jun 7, 2022
2 parents 8c9fa24 + 8effeda commit c0ee0d5
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 65 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.5

- Assign bar chart colours cosistently based on Actor id

# 0.1.4

- Form error styling
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ GEM
nokogiri (~> 1.8)

PLATFORMS
x86_64-darwin-19
x86_64-linux

DEPENDENCIES
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@
4. `bundle exec rails s`
5. Obtain a copy of the 'commitments' CSV if not already present in `lib/data/seeds`
6. Run `rake import:commitments`
7. On the Commitments page, you should see a list of commitments.
7. On the Commitments page, you should see a list of commitments.

### Adding new commitments via csv
We are dependant on several fields on Commitment to filter the results we want to return over the API.

```scope :api_records, -> { where(state: 'live', cfn_approved: true, shareable: true) }```

We need to add these to any csv file for importing:
```
state, cfn_approved, shareable
live, true, true
live, true, true
```
6 changes: 6 additions & 0 deletions app/controllers/api/v1/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Api
module V1
class ApiController < ActionController::Base
end
end
end
72 changes: 72 additions & 0 deletions app/controllers/api/v1/commitments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

module Api
module V1
class CommitmentsController < ApiController
skip_before_action :verify_authenticity_token
before_action :validate_params

def index
commitments = Commitment.api_records

if params[:updated_after].present?
commitments = commitments.where('commitments.updated_at > ?', params[:updated_after].to_datetime)
end

commitments = commitments.paginate(
page: params[:page] || 1,
per_page: params[:per_page] || 10
)

render json: commitments_to_json(commitments), status: :ok
rescue StandardError => e
render json: { message: e }, status: :unprocessable_entity
end

private

def validate_params
return unless params[:per_page].to_i > 50

render json: {
message: I18n.t('controllers.api.v1.commitments.per_page.exceeds_maximum')
}, status: :unprocessable_entity
end

def commitments_to_json(commitments)
commitments.eager_load(:managers,
:actions,
:countries,
:objectives,
:threats,
:links).as_json(
only: %i[
id
area_owner_and_role
committed_year
current_area_ha
description
duration_years
geospatial_file
implementation_year
latitude
longitude
name
responsible_group
stage
created_at
updated_at
],
include: {
managers: { only: %i[id name] },
actions: { only: %i[id name] },
countries: { only: %i[id name iso] },
objectives: { only: %i[id name] },
threats: { only: %i[id name] },
links: { only: %i[id url] }
}
)
end
end
end
end
20 changes: 5 additions & 15 deletions app/javascript/components/map/MapPopup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<script>
import axios from "axios";
import BarChart from "../chart/BarChart";
import CHART_COLORS from "../../constants"
import { setAxiosHeaders } from "../../helpers/axios-helpers";
import Turbolinks from "turbolinks";
import MapLegend from './MapLegend.vue';
export default {
Expand All @@ -35,18 +35,6 @@ export default {
id: this.content.id,
chartData: Object,
url: '',
colors: [
"#97001F",
"#6054BA",
"#00483A",
"#E7C802",
"#4bc0c0",
"#43B2ED",
"#6380ff",
"#ff6384",
"#ffa040",
"#003e78",
],
data: {
labels: [''],
datasets: [],
Expand Down Expand Up @@ -110,11 +98,13 @@ export default {
this.randomKey += 1;
this.data.datasets = [];
this.chartData.forEach((item, key) => {
this.chartData.forEach((item) => {
const backgroundColor = CHART_COLORS[item.id] || "#E3E3E3"
this.data.datasets.push({
data: [item.count],
label: item.name,
backgroundColor: this.colors[key],
backgroundColor
})
});
},
Expand Down
14 changes: 14 additions & 0 deletions app/javascript/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const CHART_COLORS = {
1: "#97001F",
2: "#6054BA",
3: "#00483A",
4: "#E7C802",
5: "#4BC0C0",
6: "#43B2ED",
7: "#6380FF",
8: "#FF6384",
9: "#FFA040",
10: "#003E78",
}

export default CHART_COLORS
15 changes: 9 additions & 6 deletions app/models/commitment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class Commitment < ApplicationRecord
validate :name_is_10_words_or_less, if: :user_created_and_live?

scope :published, -> { where(state: 'live', cfn_approved: true) }

scope :api_records, lambda {
where(state: 'live', cfn_approved: true, shareable: true, commitment_source: %w[csv form])
}
scope :cbd_without_government_managers, lambda {
government_manager_ids = Manager.where(name: ['Other government', 'Sub-national government'])
where(commitment_source: 'cbd')
Expand Down Expand Up @@ -135,7 +137,7 @@ def self.paginate_commitments(json = nil)
@filter_params = json_params['filters'].all? { |p| p['options'].blank? } ? [] : json_params['filters']
end

unpaginated_commitments = generate_query(page, @filter_params)
unpaginated_commitments = generate_query(@filter_params)
paginated_commitments = unpaginated_commitments
.paginate(page: page || 1, per_page: @items_per_page)
.to_a.map!(&:to_hash)
Expand All @@ -156,10 +158,11 @@ def to_hash
}
end

def self.generate_query(page, filter_params)
def self.generate_query(filter_params)
# if params are empty then return the paginated results without filtering
# WARNING! Do not remove the 'published' scope, because this will show unpublished Commitments
# people might not want public and CBD commitments we've chosen not to display.
# WARNING! Do not remove the 'published' scope, because this will show
# unpublished Commitments people might not want public and CBD commitments
# we've chosen not to display.
if filter_params.empty?
return Commitment.published
.includes(:countries)
Expand All @@ -169,7 +172,7 @@ def self.generate_query(page, filter_params)
# we have to do some hard work on the filtering...
filters = filter_params.select { |hash| hash['options'].present? }
where_params = parse_filters(filters)
run_query(page, where_params)
run_query(where_params)
end

def self.parse_filters(filters)
Expand Down
4 changes: 3 additions & 1 deletion app/models/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ def country_commitments_json
.published
.joins(:managers)
.group('managers.name')
.group('managers.id')
.select("ROUND((COUNT(*)*100.0/#{commitment_count_for_country}), 0) AS percentage")
.select('managers.name AS name')
.select('COUNT(*) AS count')
.as_json(only: %i[name percentage count])
.select('managers.id AS id')
.as_json(only: %i[id name percentage count])

{
country_name: name,
Expand Down
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@ en:
header: Explore Commitments
placeholder: Search by country
button: View Commitments
controllers:
api:
v1:
commitments:
per_page:
exceeds_maximum: Maximum of 50 items per page
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
resources :progress_documents, only: [:update, :destroy]
resources :country_commitments, only: [:show, :index]

namespace :api do
namespace :v1 do
resources :commitments, only: [:index]
end
end

namespace :admin do
resources :imported_commitments, only: [:index, :update]
end
Expand Down
4 changes: 4 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@
every 1.day, at: '1:00' do
runner "CbdImportJob.perform_later"
end

every 1.day, at: '2:00 am' do
rake 'active_storage:purge_unattached'
end
Loading

0 comments on commit c0ee0d5

Please sign in to comment.