Extract setting prefix to a method

We may need to access the setting key prefix in the future, so it's
better to have it in a method to avoid duplication
This commit is contained in:
Julian Herrero
2019-05-14 18:44:29 +02:00
parent b4e8395bd6
commit e32faf3a3c
2 changed files with 14 additions and 1 deletions

View File

@@ -3,8 +3,11 @@ class Setting < ApplicationRecord
default_scope { order(id: :asc) }
def prefix
key.split(".").first
end
def type
prefix = key.split(".").first
if %w[feature process proposals map html homepage].include? prefix
prefix
else

View File

@@ -17,6 +17,16 @@ describe Setting do
expect(described_class.where(key: "official_level_1_name", value: "Stormtrooper")).to exist
end
describe "#prefix" do
it "returns the prefix of its key" do
expect(Setting.create(key: "prefix.key_name").prefix).to eq "prefix"
end
it "returns the whole key for a non prefixed key" do
expect(Setting.create(key: "key_name").prefix).to eq "key_name"
end
end
describe "#type" do
it "returns the key prefix for 'process' settings" do
process_setting = Setting.create(key: "process.whatever")