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



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

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.



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

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:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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

#smtp_configurationObject



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

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