]> git.openstreetmap.org Git - rails.git/commitdiff
Switch to using FrozenRecord for loading communities
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 27 Jul 2022 15:19:08 +0000 (16:19 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 27 Jul 2022 15:19:08 +0000 (16:19 +0100)
This allows us to query the records to get the local chapters, which
is more flexible and allows us to use other resources too.

Gemfile
Gemfile.lock
app/controllers/site_controller.rb
app/models/community.rb [new file with mode: 0644]
lib/osm_community_index.rb
lib/osm_community_index/local_chapter.rb
lib/osm_community_index/resource_backend.rb [new file with mode: 0644]

diff --git a/Gemfile b/Gemfile
index 108f449712673a83aed372383bb12426d4117722..e17772d348be8241207f0d20e1bedf69d9aa2cb1 100644 (file)
--- 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"
index 46f472d7f7c5f7cd7912c4b5d7e67c37e4cdd307..bfd0152c1003b128fc2e3a546eee048c538b8725 100644 (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
index 524001d4db926fca2c4511451843e035fa0df25d..50435ca85d5181d87fd7415a1a7a93562122636c 100644 (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
diff --git a/app/models/community.rb b/app/models/community.rb
new file mode 100644 (file)
index 0000000..8544ec4
--- /dev/null
@@ -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
index f738e8ae88b23cdef15b69281c713c28a3a1a8ec..519306d8f72a008dc0d1dc261050ba0c231478c8 100644 (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
index f36a6441cd0748419c9bdefa82c98eab29d38e77..d83f134719f5ea692da80b23eeac056a1d96c5db 100644 (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
diff --git a/lib/osm_community_index/resource_backend.rb b/lib/osm_community_index/resource_backend.rb
new file mode 100644 (file)
index 0000000..e16d951
--- /dev/null
@@ -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