Switch to using FrozenRecord for loading communities

This allows us to query the records to get the local chapters, which
is more flexible and allows us to use other resources too.
This commit is contained in:
Andy Allan 2022-07-27 16:19:08 +01:00
parent 819e5ecf94
commit bb7f5ac2c8
7 changed files with 31 additions and 49 deletions

View file

@ -46,6 +46,7 @@ gem "cancancan"
gem "composite_primary_keys", "~> 13.0.0", "!= 13.0.1"
gem "config"
gem "delayed_job_active_record"
gem "frozen_record"
gem "http_accept_language", "~> 2.1.1"
gem "i18n-js", ">= 3.0.0"
gem "oauth-plugin", ">= 0.5.1"

View file

@ -233,6 +233,8 @@ GEM
rake
ffi-libarchive (1.1.3)
ffi (~> 1.0)
frozen_record (0.26.0)
activemodel
fspath (3.1.2)
gd2-ffij (0.4.0)
ffi (>= 1.0.0)
@ -530,6 +532,7 @@ DEPENDENCIES
factory_bot_rails
faraday
ffi-libarchive
frozen_record
gd2-ffij (>= 0.4.0)
htmlentities
http_accept_language (~> 2.1.1)
@ -586,4 +589,4 @@ DEPENDENCIES
webmock
BUNDLED WITH
2.2.16
2.2.19

View file

@ -107,7 +107,7 @@ class SiteController < ApplicationController
end
def communities
@local_chapters = OsmCommunityIndex::LocalChapter.local_chapters
@local_chapters = Community.where(:type => "osm-lc").where.not(:id => "OSMF")
end
def export; end

8
app/models/community.rb Normal file
View file

@ -0,0 +1,8 @@
class Community < FrozenRecord::Base
self.base_path = Rails.root.join("node_modules/osm-community-index/dist/")
self.backend = OsmCommunityIndex::ResourceBackend
def url
strings["url"]
end
end

View file

@ -1,12 +1,2 @@
module OsmCommunityIndex
require "yaml"
def self.community_index
@community_index ||= community_index_from_json
end
def self.community_index_from_json
json_file = Rails.root.join("node_modules/osm-community-index/dist/resources.json")
JSON.parse(File.read(json_file))
end
end

View file

@ -1,40 +1,7 @@
module OsmCommunityIndex
class LocalChapter
attr_reader :id, :url
def initialize(id, url)
@id = id
@url = url
end
def self.local_chapters
@chapters = init_local_chapters
end
def self.init_local_chapters
raw_local_chapters = load_raw_local_chapters
raw_local_chapters.map do |chapter|
id = chapter[:id]
url = chapter[:resource]["strings"]["url"]
LocalChapter.new(id, url)
end
end
def self.load_raw_local_chapters
community_index = OsmCommunityIndex.community_index
raw_local_chapters = []
community_index["resources"].each do |id, resource|
resource.each do |key, value|
next unless key == "type" && value == "osm-lc" && id != "OSMF"
raw_local_chapters.push({ :id => id, :resource => resource })
end
end
raw_local_chapters
end
def self.add_to_i18n
raw_local_chapters = load_raw_local_chapters
local_chapters = Community.where(:type => "osm-lc").where.not(:id => "OSMF")
files = Dir.glob(Rails.root.join("node_modules/osm-community-index/i18n/*"))
files.each do |file|
locale = File.basename(file, ".yaml")
@ -43,14 +10,13 @@ module OsmCommunityIndex
locale_rails = locale.tr("_", "-")
data = {}
raw_local_chapters.each do |chapter|
local_chapters.each do |chapter|
id = chapter[:id]
resource = chapter[:resource]
strings = community_index_yaml[id] || {}
# if the name isn't defined then fall back on community,
# as per discussion here: https://github.com/osmlab/osm-community-index/issues/483
strings["name"] = strings["name"] || resource["strings"]["name"] || resource["strings"]["community"]
strings["name"] = strings["name"] || chapter["strings"]["name"] || chapter["strings"]["community"]
data.deep_merge!({ "osm_community_index" => { "local_chapter" => { id => strings } } })
end

View file

@ -0,0 +1,14 @@
# A backend for FrozenRecord
module OsmCommunityIndex
module ResourceBackend
def self.filename(_model)
"resources.json"
end
def self.load(file_path)
resources = JSON.parse(File.read(file_path))
resources["resources"].values
end
end
end