Class: Apidae::Reference
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Apidae::Reference
- Defined in:
- app/models/apidae/reference.rb
Constant Summary collapse
- INTERNAL =
'internal'
Class Method Summary collapse
- .default_scope ⇒ Object
- .import(refs_json) ⇒ Object
- .import_file(json_file) ⇒ Object
- .import_internal(internal_json) ⇒ Object
Instance Method Summary collapse
Class Method Details
.default_scope ⇒ Object
8 9 10 |
# File 'app/models/apidae/reference.rb', line 8 def self.default_scope where(is_active: true) end |
.import(refs_json) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/models/apidae/reference.rb', line 12 def self.import(refs_json) locales = Rails.application.config.respond_to?(:apidae_locales) ? Rails.application.config.apidae_locales : [DEFAULT_LOCALE] locales_map = Hash[locales.map {|loc| ["libelle#{loc.camelize.gsub('-', '')}".to_sym, loc]}] if count == 0 || Time.current > (maximum(:updated_at) + 1.day) refs_hashes = JSON.parse(refs_json, symbolize_names: true) if refs_hashes.length != where("apidae_type != ?", INTERNAL).count refs_hashes.each do |ref_data| ref = Reference.find_or_initialize_by(apidae_id: ref_data[:id], apidae_type: ref_data[:elementReferenceType]) ref.label_data = ref_data.slice(*locales_map.keys).transform_keys {|k| locales_map[k]} ref.parent = ref_data[:parent][:id] if ref_data[:parent] ref.category = ref_data[:familleCritere] ? ref_data[:familleCritere][:id] : (ref_data[:typeLabel] ? ref_data[:typeLabel][:id] : nil) ref.is_active = ref_data[:actif] ref.save! end end end end |
.import_file(json_file) ⇒ Object
30 31 32 33 |
# File 'app/models/apidae/reference.rb', line 30 def self.import_file(json_file) refs_json = File.read(json_file) import(refs_json) end |
.import_internal(internal_json) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/apidae/reference.rb', line 35 def self.import_internal(internal_json) refs_hashes = JSON.parse(internal_json, symbolize_names: true) if refs_hashes.length != where(apidae_type: INTERNAL).count refs_hashes.each do |ref_data| ref = Reference.find_or_initialize_by(apidae_id: ref_data[:id], apidae_type: INTERNAL) ref.label_data = {libelleFr: ref_data[:libelle]} ref.save! end end end |
Instance Method Details
#detailed_label(language) ⇒ Object
50 51 52 |
# File 'app/models/apidae/reference.rb', line 50 def detailed_label(language) category ? "#{Reference.find_by_apidae_id(category).label(language)} - #{label(language)}" : label(language) end |
#label(language) ⇒ Object
46 47 48 |
# File 'app/models/apidae/reference.rb', line 46 def label(language) label_data[language.to_s] end |