Class: ArticleUnit
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ArticleUnit
- Defined in:
- app/models/article_unit.rb
Class Method Summary collapse
- .all_cached ⇒ Object
- .as_hash(config = nil) ⇒ Object
- .as_options(config = nil) ⇒ Object
- .clear_cache ⇒ Object
Class Method Details
.all_cached ⇒ Object
7 8 9 10 11 12 13 |
# File 'app/models/article_unit.rb', line 7 def self.all_cached @all_cached = {} if @all_cached.nil? cached_units_in_locale = @all_cached[I18n.locale] return cached_units_in_locale unless cached_units_in_locale.nil? @all_cached[I18n.locale] = all.load end |
.as_hash(config = nil) ⇒ Object
19 20 21 22 23 24 25 |
# File 'app/models/article_unit.rb', line 19 def self.as_hash(config = nil) additional_units = config&.dig(:additional_units) || [] available_units = all_cached.map(&:unit) ArticleUnitsLib.units.to_h do |code, unit| [code, unit.merge({ visible: available_units.include?(code) || additional_units.include?(code) })] end end |
.as_options(config = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/article_unit.rb', line 27 def self.(config = nil) additional_units = config&.dig(:additional_units) || [] = {} available_units = all_cached.map(&:unit) ArticleUnitsLib.units.each do |code, unit| next unless available_units.include?(code) || additional_units.include?(code) label = unit[:name] label += " (#{unit[:symbol]})" if unit[:symbol].present? [label] = code end end |
.clear_cache ⇒ Object
15 16 17 |
# File 'app/models/article_unit.rb', line 15 def self.clear_cache @all_cached = {} end |