]> git.openstreetmap.org Git - rails.git/blob - app/models/local_chapter.rb
Refactor osm-community-index and local chapters models
[rails.git] / app / models / local_chapter.rb
1 class LocalChapter
2
3   attr_reader :id, :name, :url
4
5   @localised_chapters = {}
6
7   def initialize(id, name, url)
8     @id = id
9     @name = name
10     @url = url
11   end
12
13   def self.local_chapters_with_locale(locale)
14     @localised_chapters[locale] ||= load_local_chapters(locale)
15   end
16
17   protected
18
19   def self.load_local_chapters(locale)
20     community_index = OsmCommunityIndex.community_index
21     localised_strings = OsmCommunityIndex.localised_strings(locale)
22     local_chapters = []
23     community_index["resources"].each do |id, resource|
24       resource.each do |key, value|
25         next unless key == "type" && value == "osm-lc" && id != "OSMF"
26
27         strings = resource["strings"]
28         name = localised_strings.dig(id, "name") || strings["name"] || strings["community"]
29         url = strings["url"]
30         local_chapters.push(LocalChapter.new(id, name, url))
31       end
32     end
33     local_chapters
34   end
35
36 end