From e32faf3a3ce21983639a65e73a652ea4db15412b Mon Sep 17 00:00:00 2001 From: Julian Herrero Date: Tue, 14 May 2019 18:44:29 +0200 Subject: [PATCH] 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 --- app/models/setting.rb | 5 ++++- spec/models/setting_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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")