Module: RailsDictionary

Defined in:
lib/rails_dictionary.rb,
lib/rails_dictionary/railtie.rb,
lib/rails_dictionary/version.rb,
lib/rails_dictionary/acts_as_dict_type.rb,
lib/rails_dictionary/acts_as_dict_slave.rb,
lib/rails_dictionary/acts_as_dictionary.rb,
lib/rails_dictionary/active_record_extension.rb

Defined Under Namespace

Modules: ActiveRecordExtension, ActsAsDictSlave, ActsAsDictType, ActsAsDictionary Classes: Railtie

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.deprecatorObject

Gem-specific deprecator so warnings can be silenced/raised independently of the host app. Silenced by default: the removal version is not decided yet, so we don’t nag apps. Re-enable with ‘RailsDictionary.deprecator.silenced = false`.



20
21
22
23
24
# File 'lib/rails_dictionary.rb', line 20

def self.deprecator
  @deprecator ||= ActiveSupport::Deprecation.new("a future major release", "rails_dictionary").tap do |deprecator|
    deprecator.silenced = true
  end
end

.dictionary_model_namesObject

Models that called acts_as_dictionary. Stored by name so the registry survives Zeitwerk reloads in development.



7
8
9
# File 'lib/rails_dictionary.rb', line 7

def self.dictionary_model_names
  @dictionary_model_names ||= []
end

.extract_to_hash(array, keys_array) ⇒ Object

Internal implementation of the (deprecated) Array#extract_to_hash, kept here so the gem’s own callers don’t trip the deprecation warning.



28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_dictionary.rb', line 28

def self.extract_to_hash(array, keys_array)
  ret_hash = {}
  keys_array.each { |ky| ret_hash[ky.to_sym] = [] }
  array.each do |sf|
    keys_array.each do |ky|
      ret_hash[ky.to_sym] << sf.sub("#{ky}_", "") if sf =~ Regexp.new("^#{ky}_")
    end
  end
  ret_hash.reject { |_k, v| v.blank? }
end

.load_dict_methodsObject

Boot-time entry point (used by the Railtie). Guarded so a missing or unmigrated database during boot, asset builds, or db:create never raises.



50
51
52
53
54
55
56
57
# File 'lib/rails_dictionary.rb', line 50

def self.load_dict_methods
  return unless ActiveRecord::Base.connection.table_exists?("dict_types")
  reload_dict_methods
rescue ActiveRecord::NoDatabaseError,
       ActiveRecord::StatementInvalid,
       ActiveRecord::ConnectionNotEstablished
  nil
end

.register_dictionary_model(klass) ⇒ Object



11
12
13
14
# File 'lib/rails_dictionary.rb', line 11

def self.register_dictionary_model(klass)
  return if klass.name.nil?
  dictionary_model_names << klass.name unless dictionary_model_names.include?(klass.name)
end

.reload_dict_methodsObject

Regenerate the per-type lookup methods on every registered dictionary model. Called whenever a DictType row changes.



41
42
43
44
45
46
# File 'lib/rails_dictionary.rb', line 41

def self.reload_dict_methods
  dictionary_model_names.each do |name|
    klass = name.safe_constantize
    klass&.reload_dict_methods
  end
end