-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* dynamic values - work in progress * add specs * Add erb support, update content documentation * fix tab * Update values.html.erb * add nonindex + authentication on /values page * Test inputting a variable in a page (to revert after) * Revert "Test inputting a variable in a page (to revert after)" This reverts commit b992458. * fix double back-tick --------- Co-authored-by: MylesJarvis <myles.jarvis@education.gov.uk>
- Loading branch information
1 parent
4fd75fb
commit 48dffea
Showing
14 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Value | ||
PATH = "config/values/**/*.yml".freeze | ||
attr_reader :data | ||
|
||
def self.data | ||
# the value data will rarely change, so OK to cache in a class variable | ||
@@data ||= new.data | ||
end | ||
|
||
def initialize(path = nil) | ||
@data = load_values(path || Rails.root.join(PATH)) | ||
end | ||
|
||
private | ||
|
||
def load_values(dir_spec) | ||
{}.tap do |data| | ||
Dir[dir_spec].each do |filename| | ||
data.merge!(flatten_hash(YAML.load_file(filename))) | ||
end | ||
end | ||
end | ||
|
||
def flatten_hash(hash) | ||
# based on https://stackoverflow.com/questions/23521230/flattening-nested-hash-to-a-single-hash-with-ruby-rails | ||
hash.each_with_object({}) do |(k, v), h| | ||
if v.is_a? Hash | ||
flatten_hash(v).map do |h_k, h_v| | ||
h["#{k}_#{h_k}"] = h_v | ||
end | ||
else | ||
h[k] = v | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: Values | ||
noindex: true | ||
content: | ||
- pages/values/table | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<table> | ||
<thead> | ||
<tr> | ||
<th> | ||
Key | ||
</th> | ||
<th> | ||
Value | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% Value.data.each do |key, value| %> | ||
<tr> | ||
<td> | ||
<code> | ||
$<%= key %>$ | ||
</code> | ||
</td> | ||
<td> | ||
<%= value %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
dates: | ||
example: | ||
opening: 1st September 2024 | ||
|
||
dates_example_closing: 31/12/2024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
salaries: | ||
example: | ||
min: £30,000 | ||
|
||
salaries_example_max: £41k |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
data1: | ||
example: | ||
date: 01/02/2003 | ||
|
||
data1_example_amount: £1,234.56 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
data2: | ||
example: | ||
string: Hello World! | ||
|
||
data2_example_number: 0.01 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "rails_helper" | ||
|
||
describe Value do | ||
describe "##data (class method)" do | ||
subject { described_class.data } | ||
|
||
it { is_expected.to be_a Hash } | ||
end | ||
|
||
describe "#data" do | ||
subject { instance.data } | ||
|
||
let(:instance) { described_class.new(path) } | ||
|
||
context "with default path" do | ||
let(:path) { nil } | ||
|
||
it { is_expected.to be_a Hash } | ||
end | ||
|
||
context "with specific file path" do | ||
let(:path) { "spec/fixtures/files/example_values/**/*.yml" } | ||
|
||
it { | ||
is_expected.to eql( | ||
{ | ||
"data1_example_amount" => "£1,234.56", | ||
"data1_example_date" => "01/02/2003", | ||
"data2_example_number" => 0.01, | ||
"data2_example_string" => "Hello World!", | ||
}, | ||
) | ||
} | ||
end | ||
end | ||
end |