Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processes ERB in yaml config files for page layouts, cells, and elements #760

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/alchemy/cell.rb
Expand Up @@ -56,7 +56,7 @@ def translated_label_for(cell_name)
private

def read_yml_file
::YAML.load_file(yml_file_path) || []
::YAML.load(ERB.new(File.read(yml_file_path)).result) || []
end

def yml_file_path
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/element/definitions.rb
Expand Up @@ -23,7 +23,7 @@ def definitions
#
def read_definitions_file
if ::File.exists?(definitions_file_path)
::YAML.load_file(definitions_file_path) || []
::YAML.load(ERB.new(File.read(definitions_file_path)).result) || []
else
raise LoadError, "Could not find elements.yml file! Please run `rails generate alchemy:scaffold`"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/alchemy/page_layout.rb
Expand Up @@ -157,7 +157,7 @@ def available_on_site?(layout)
#
def read_layouts_file
if File.exists?(layouts_file_path)
YAML.load_file(layouts_file_path) || []
YAML.load(ERB.new(File.read(layouts_file_path)).result) || []
else
raise LoadError, "Could not find page_layouts.yml file! Please run `rails generate alchemy:scaffold`"
end
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/config/alchemy/cells.yml
@@ -1,2 +1,5 @@
- name: right_column
elements: [search]

- name: <%= 'erb_' + 'cell' %>
elements: [text]
5 changes: 5 additions & 0 deletions spec/dummy/config/alchemy/elements.yml
Expand Up @@ -102,3 +102,8 @@
type: EssenceSelect
- name: essence_text
type: EssenceText

- name: <%= 'erb_' + 'element' %>
contents:
- name: text
type: EssenceRichtext
3 changes: 3 additions & 0 deletions spec/dummy/config/alchemy/page_layouts.yml
Expand Up @@ -26,3 +26,6 @@
- name: external
unique: false
redirects_to_external: true

- name: <%= 'erb_' + 'layout' %>
unique: true
6 changes: 5 additions & 1 deletion spec/libraries/page_layout_spec.rb
Expand Up @@ -14,8 +14,12 @@ module Alchemy
expect(subject.collect { |l| l['name'] }).to include('standard')
end

it "should allow erb generated layouts" do
expect(subject.collect { |l| l['name'] }).to include('erb_layout')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer map over collect.

end

context "with empty layouts file" do
before { expect(YAML).to receive(:load_file).and_return(false) }
before { expect(YAML).to receive(:load).and_return(false) }

it "returns empty array" do
is_expected.to eq([])
Expand Down
4 changes: 4 additions & 0 deletions spec/models/cell_spec.rb
Expand Up @@ -10,6 +10,10 @@ module Alchemy
it "should return an Array" do
expect(Cell.definitions).to be_a(Array)
end

it "should allow erb generated definitions" do
expect(Cell.definitions.collect {|d| d['name']}).to include('erb_cell')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer map over collect.
Space between { and | missing.
Space missing inside }.

end
end

describe '.definition_for' do
Expand Down
7 changes: 6 additions & 1 deletion spec/models/element_spec.rb
Expand Up @@ -33,6 +33,10 @@ module Alchemy
end

describe '.definitions' do
it "should allow erb generated elements" do
expect(Element.definitions.collect {|el| el['name']}).to include('erb_element')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer map over collect.
Space between { and | missing.
Space missing inside }.

end

context "without existing yml files" do
before { allow(File).to receive(:exists?).and_return(false) }

Expand All @@ -42,7 +46,8 @@ module Alchemy
end

context "without any definitions in elements.yml" do
before { allow(YAML).to receive(:load_file).and_return(false) } # Yes, YAML.load_file returns false if an empty file exists.
# Yes, YAML.load returns false if an empty file exists.
before { allow(YAML).to receive(:load).and_return(false) }

it "should return an empty array" do
expect(Element.definitions).to eq([])
Expand Down