2017-11-14 02:08:04 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 04:49:53 +01:00
|
|
|
RSpec.describe Setting do
|
2017-11-14 02:08:04 +00:00
|
|
|
describe '#to_param' do
|
|
|
|
let(:setting) { Fabricate(:setting, var: var) }
|
|
|
|
let(:var) { 'var' }
|
|
|
|
|
|
|
|
it 'returns setting.var' do
|
|
|
|
expect(setting.to_param).to eq var
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.[]' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:rails_initialized?).and_return(rails_initialized)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:key) { 'key' }
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when rails_initialized? is falsey' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:rails_initialized) { false }
|
|
|
|
|
|
|
|
it 'calls RailsSettings::Base#[]' do
|
2023-11-07 09:46:28 +00:00
|
|
|
allow(RailsSettings::Base).to receive(:[]).with(key)
|
|
|
|
|
2017-11-14 02:08:04 +00:00
|
|
|
described_class[key]
|
2023-11-07 09:46:28 +00:00
|
|
|
|
|
|
|
expect(RailsSettings::Base).to have_received(:[]).with(key)
|
2017-11-14 02:08:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when rails_initialized? is truthy' do
|
2017-11-14 02:08:04 +00:00
|
|
|
before do
|
|
|
|
allow(RailsSettings::Base).to receive(:cache_key).with(key, nil).and_return(cache_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:rails_initialized) { true }
|
|
|
|
let(:cache_key) { 'cache-key' }
|
|
|
|
let(:cache_value) { 'cache-value' }
|
|
|
|
|
|
|
|
it 'calls not RailsSettings::Base#[]' do
|
2023-11-07 09:46:28 +00:00
|
|
|
allow(RailsSettings::Base).to receive(:[]).with(key)
|
|
|
|
|
2017-11-14 02:08:04 +00:00
|
|
|
described_class[key]
|
2023-11-07 09:46:28 +00:00
|
|
|
|
|
|
|
expect(RailsSettings::Base).to_not have_received(:[]).with(key)
|
2017-11-14 02:08:04 +00:00
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when Rails.cache does not exists' do
|
2017-11-14 02:08:04 +00:00
|
|
|
before do
|
|
|
|
allow(RailsSettings::Settings).to receive(:object).with(key).and_return(object)
|
|
|
|
allow(described_class).to receive(:default_settings).and_return(default_settings)
|
|
|
|
allow_any_instance_of(Settings::ScopedSettings).to receive(:thing_scoped).and_return(records)
|
2018-02-02 09:10:18 +00:00
|
|
|
Rails.cache.delete(cache_key)
|
2017-11-14 02:08:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:object) { nil }
|
|
|
|
let(:default_value) { 'default_value' }
|
|
|
|
let(:default_settings) { { key => default_value } }
|
|
|
|
let(:records) { [Fabricate(:setting, var: key, value: nil)] }
|
|
|
|
|
|
|
|
it 'calls RailsSettings::Settings.object' do
|
2023-11-07 09:46:28 +00:00
|
|
|
allow(RailsSettings::Settings).to receive(:object).with(key)
|
|
|
|
|
2017-11-14 02:08:04 +00:00
|
|
|
described_class[key]
|
2023-11-07 09:46:28 +00:00
|
|
|
|
|
|
|
expect(RailsSettings::Settings).to have_received(:object).with(key)
|
2017-11-14 02:08:04 +00:00
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when RailsSettings::Settings.object returns truthy' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:object) { db_val }
|
2023-06-22 13:55:22 +01:00
|
|
|
let(:db_val) { instance_double(described_class, value: 'db_val') }
|
2017-11-14 02:08:04 +00:00
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when default_value is a Hash' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:default_value) { { default_value: 'default_value' } }
|
|
|
|
|
|
|
|
it 'calls default_value.with_indifferent_access.merge!' do
|
|
|
|
expect(default_value).to receive_message_chain(:with_indifferent_access, :merge!)
|
|
|
|
.with(db_val.value)
|
|
|
|
|
|
|
|
described_class[key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when default_value is not a Hash' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:default_value) { 'default_value' }
|
|
|
|
|
|
|
|
it 'returns db_val.value' do
|
|
|
|
expect(described_class[key]).to be db_val.value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when RailsSettings::Settings.object returns falsey' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:object) { nil }
|
|
|
|
|
|
|
|
it 'returns default_settings[key]' do
|
|
|
|
expect(described_class[key]).to be default_settings[key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when Rails.cache exists' do
|
2017-11-14 02:08:04 +00:00
|
|
|
before do
|
|
|
|
Rails.cache.write(cache_key, cache_value)
|
|
|
|
end
|
|
|
|
|
2018-02-17 21:35:05 +00:00
|
|
|
it 'does not query the database' do
|
2021-03-24 09:44:31 +00:00
|
|
|
callback = double
|
|
|
|
allow(callback).to receive(:call)
|
|
|
|
ActiveSupport::Notifications.subscribed callback, 'sql.active_record' do
|
|
|
|
described_class[key]
|
|
|
|
end
|
2023-02-20 01:33:27 +00:00
|
|
|
expect(callback).to_not have_received(:call)
|
2018-02-17 21:35:05 +00:00
|
|
|
end
|
|
|
|
|
2017-11-14 02:08:04 +00:00
|
|
|
it 'returns the cached value' do
|
|
|
|
expect(described_class[key]).to eq cache_value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.all_as_records' do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Settings::ScopedSettings).to receive(:thing_scoped).and_return(records)
|
|
|
|
allow(described_class).to receive(:default_settings).and_return(default_settings)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:key) { 'key' }
|
|
|
|
let(:default_value) { 'default_value' }
|
|
|
|
let(:default_settings) { { key => default_value } }
|
|
|
|
let(:original_setting) { Fabricate(:setting, var: key, value: nil) }
|
|
|
|
let(:records) { [original_setting] }
|
|
|
|
|
|
|
|
it 'returns a Hash' do
|
2023-02-18 02:24:16 +00:00
|
|
|
expect(described_class.all_as_records).to be_a Hash
|
2017-11-14 02:08:04 +00:00
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when records includes Setting with var as the key' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:records) { [original_setting] }
|
|
|
|
|
|
|
|
it 'includes the original Setting' do
|
|
|
|
setting = described_class.all_as_records[key]
|
|
|
|
expect(setting).to eq original_setting
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when records includes nothing' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:records) { [] }
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when default_value is not a Hash' do
|
2017-11-14 02:08:04 +00:00
|
|
|
it 'includes Setting with value of default_value' do
|
|
|
|
setting = described_class.all_as_records[key]
|
|
|
|
|
2023-06-06 12:58:33 +01:00
|
|
|
expect(setting).to be_a described_class
|
2017-11-14 02:08:04 +00:00
|
|
|
expect(setting).to have_attributes(var: key)
|
|
|
|
expect(setting).to have_attributes(value: 'default_value')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when default_value is a Hash' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:default_value) { { 'foo' => 'fuga' } }
|
|
|
|
|
|
|
|
it 'returns {}' do
|
|
|
|
expect(described_class.all_as_records).to eq({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.default_settings' do
|
2023-02-20 04:24:14 +00:00
|
|
|
subject { described_class.default_settings }
|
|
|
|
|
2017-11-14 02:08:04 +00:00
|
|
|
before do
|
|
|
|
allow(RailsSettings::Default).to receive(:enabled?).and_return(enabled)
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when RailsSettings::Default.enabled? is false' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:enabled) { false }
|
|
|
|
|
|
|
|
it 'returns {}' do
|
2023-02-20 04:00:48 +00:00
|
|
|
expect(subject).to eq({})
|
2017-11-14 02:08:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 04:49:08 +01:00
|
|
|
context 'when RailsSettings::Settings.enabled? is true' do
|
2017-11-14 02:08:04 +00:00
|
|
|
let(:enabled) { true }
|
|
|
|
|
|
|
|
it 'returns instance of RailsSettings::Default' do
|
2023-02-20 04:00:48 +00:00
|
|
|
expect(subject).to be_a RailsSettings::Default
|
2017-11-14 02:08:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|