Class: ApiMaker::TranslatedCollections

Inherits:
Object
  • Object
show all
Defined in:
app/services/api_maker/translated_collections.rb

Class Method Summary collapse

Class Method Details

.add(blk:, collection_name:, model_class:) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/api_maker/translated_collections.rb', line 2

def self.add(blk:, collection_name:, model_class:)
  @translated_collections ||= {}
  collections = {}
  model_class_name = model_class.name
  collection_values = nil

  I18n.available_locales.each do |locale|
    I18n.with_locale(locale) do
      collection = blk.call.freeze
      collections[locale.to_s] = {
        collection: collection,
        collection_array: collection.map { |translation, value| [translation: translation, value: value] }.flatten,
        inverted_collection: collection.invert
      }
      collection_values = collection.values.clone.freeze if collection_values.nil?
    end
  end

  @translated_collections[model_class_name] ||= {}
  @translated_collections[model_class_name][collection_name] ||= collections

  plural_name = collection_name.to_s.pluralize
  inverted_translated_collection_name = "translated_#{plural_name}_inverted"

  add_translated_collection_method(model_class, plural_name, collections)
  add_translated_inverted_collection_method(model_class, inverted_translated_collection_name, collections)
  add_collection_values_method(model_class, plural_name, collection_values)
  add_translated_method(model_class, collection_name, inverted_translated_collection_name)

  model_class.validates collection_name, inclusion: {in: collection_values}
end

.add_collection_values_method(model_class, plural_name, collection_values) ⇒ Object



46
47
48
49
50
# File 'app/services/api_maker/translated_collections.rb', line 46

def self.add_collection_values_method(model_class, plural_name, collection_values)
  model_class.define_singleton_method(plural_name) do
    collection_values
  end
end

.add_translated_collection_method(model_class, plural_name, collections) ⇒ Object



34
35
36
37
38
# File 'app/services/api_maker/translated_collections.rb', line 34

def self.add_translated_collection_method(model_class, plural_name, collections)
  model_class.define_singleton_method("translated_#{plural_name}") do
    collections.fetch(I18n.locale.to_s).fetch(:collection)
  end
end

.add_translated_inverted_collection_method(model_class, inverted_translated_collection_name, collections) ⇒ Object



40
41
42
43
44
# File 'app/services/api_maker/translated_collections.rb', line 40

def self.add_translated_inverted_collection_method(model_class, inverted_translated_collection_name, collections)
  model_class.define_singleton_method(inverted_translated_collection_name) do
    collections.fetch(I18n.locale.to_s).fetch(:inverted_collection)
  end
end

.add_translated_method(model_class, collection_name, inverted_translated_collection_name) ⇒ Object



52
53
54
55
56
57
58
# File 'app/services/api_maker/translated_collections.rb', line 52

def self.add_translated_method(model_class, collection_name, inverted_translated_collection_name)
  model_class.define_method("translated_#{collection_name}") do
    current_value = __send__(collection_name)
    inverted_translated_collection = self.class.__send__(inverted_translated_collection_name)
    inverted_translated_collection.fetch(current_value)
  end
end

.translated_collectionsObject



60
61
62
63
64
65
# File 'app/services/api_maker/translated_collections.rb', line 60

def self.translated_collections
  ApiMaker::Loader.load_models

  @translated_collections ||= {}
  @translated_collections
end