Module: CoreSystemConfiguration::ClassMethods

Defined in:
lib/app/models/concerns/core_system_configuration.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &_block) ⇒ Object

NOTE: Currently ignored Codacy issue: When using ‘method_missing’, fall back on ‘super’

rubocop:disable Style/MethodMissingSuper



114
115
116
# File 'lib/app/models/concerns/core_system_configuration.rb', line 114

def method_missing(method, *args, &_block)
  configuration.send method, *args
end

Instance Method Details

#configurationObject

Fetch the system configuration based on environment name. First try the rails cache if not there, then go to the database.

Also, if an argument error is thrown, then just fetch from the database.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/app/models/concerns/core_system_configuration.rb', line 82

def configuration
  cache_key = "SystemConfiguration::#{Rails.env}"

  begin
    config = Rails.cache.fetch(cache_key, expires_in: 90.seconds) do
      SystemConfiguration.find_by(environment: Rails.env)
    end
  rescue StandardError
    # This seems to happen in testing relative to Country, when it does, remove
    # ourselves from the cache and return the DB entry.
    Rails.cache.delete cache_key
    config = SystemConfiguration.find_or_create_by!(environment: Rails.env)
  end

  config.nil? ? SystemConfiguration.create(environment: Rails.env) : config
end

#respond_to?(method_name, _include_private = false) ⇒ Boolean

Returns:



118
119
120
# File 'lib/app/models/concerns/core_system_configuration.rb', line 118

def respond_to?(method_name, _include_private = false)
  SystemConfiguration.fields.include?(method_name)
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:



122
123
124
# File 'lib/app/models/concerns/core_system_configuration.rb', line 122

def respond_to_missing?(method_name, _include_private = false)
  SystemConfiguration.fields.include?(method_name)
end

#smtp_configurationObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/app/models/concerns/core_system_configuration.rb', line 99

def smtp_configuration
  output = {}
  config = configuration
  fields = %w[name address domain port user_name password enable_starttls_auto]
  fields.each do |field|
    field_name = "smtp_#{field}".to_sym
    output[field.to_sym] = config.send(field_name)
  end
  output
end