From: Andy Allan Date: Wed, 27 Jul 2022 15:19:08 +0000 (+0100) Subject: Switch to using FrozenRecord for loading communities X-Git-Tag: live~1629^2~13 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/bb7f5ac2c8cf74669b421e7ae346a0cdd1ba5186 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. --- diff --git a/Gemfile b/Gemfile index 108f44971..e17772d34 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Gemfile.lock b/Gemfile.lock index 46f472d7f..bfd0152c1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index 524001d4d..50435ca85 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -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 diff --git a/app/models/community.rb b/app/models/community.rb new file mode 100644 index 000000000..8544ec470 --- /dev/null +++ b/app/models/community.rb @@ -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 diff --git a/lib/osm_community_index.rb b/lib/osm_community_index.rb index f738e8ae8..519306d8f 100644 --- a/lib/osm_community_index.rb +++ b/lib/osm_community_index.rb @@ -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 diff --git a/lib/osm_community_index/local_chapter.rb b/lib/osm_community_index/local_chapter.rb index f36a6441c..d83f13471 100644 --- a/lib/osm_community_index/local_chapter.rb +++ b/lib/osm_community_index/local_chapter.rb @@ -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 diff --git a/lib/osm_community_index/resource_backend.rb b/lib/osm_community_index/resource_backend.rb new file mode 100644 index 000000000..e16d951a4 --- /dev/null +++ b/lib/osm_community_index/resource_backend.rb @@ -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