Skip to content

Commit

Permalink
Allow configurations to be items
Browse files Browse the repository at this point in the history
  • Loading branch information
mamhoff committed Feb 11, 2025
1 parent 1042cf1 commit 72c2cfb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/alchemy/configuration/collection_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ class CollectionOption < BaseOption
def initialize(value:, name:, item_class:, collection_class: Array, **args)
@collection_class = collection_class
@item_class = item_class
value = collection_class.new(value)
value = collection_class.new(
value.map do |item|
next item if item.is_a?(item_class) || !item_class.respond_to?(:new)
item_class.new(item)
end
)
super
end

Expand Down
17 changes: 17 additions & 0 deletions spec/libraries/alchemy/configuration/collection_option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@
it { is_expected.to contain_exactly("foo", "bar") }
end
end

context "with an array of configurations" do
let(:collection_class) { Array }
let(:item_class) { Alchemy::Configurations::Sitemap }
let(:value) do
[
{
show_root: true,
show_flag: false
}
]
end

it "contains configurations" do
expect(subject.value.first.show_root).to be true
end
end
end
2 changes: 1 addition & 1 deletion spec/libraries/alchemy/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
it "can only be set with an Array of strings" do
expect do
configuration.link_target_options = [:blank]
end.to raise_exception(TypeError, "link_target_options must be a Array of strings, given [:blank]")
end.to raise_exception(TypeError, "no implicit conversion of Symbol into String")
end
end

Expand Down

0 comments on commit 72c2cfb

Please sign in to comment.