diff --git a/app/models/setting.rb b/app/models/setting.rb index 6210b6ba5..11623624a 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -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 diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb index 6fa86b686..971fbe3df 100644 --- a/spec/models/setting_spec.rb +++ b/spec/models/setting_spec.rb @@ -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")