Skip to content

Commit c448f03

Browse files
committed
New configuration: Importmap
This should allow configuring importmaps. This commit is a bit larger, but it allows setting default configurations to lambdas, so we can have access to e.g. `Alchemy::Engine`.
1 parent aa3a3c4 commit c448f03

File tree

7 files changed

+80
-5
lines changed

7 files changed

+80
-5
lines changed

lib/alchemy/configuration.rb

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require "alchemy/configuration/class_option"
1010
require "alchemy/configuration/class_set_option"
1111
require "alchemy/configuration/integer_option"
12+
require "alchemy/configuration/pathname_option"
1213
require "alchemy/configuration/regexp_option"
1314
require "alchemy/configuration/string_option"
1415

lib/alchemy/configuration/base_option.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@ def self.value_class
99

1010
def initialize(value:, name:, **args)
1111
@name = name
12-
validate(value) unless value.nil?
12+
validate(value) unless value.nil? || value.is_a?(Proc)
1313
@value = value
1414
end
15-
attr_reader :name, :value
15+
attr_reader :name
1616

17-
def validate(value)
18-
raise ConfigurationError.new(name, value, self.class.value_class.name) unless value.is_a?(self.class.value_class)
17+
def value
18+
return @value unless @value.is_a?(Proc)
19+
20+
evaluated_value = @value.call
21+
validate(evaluated_value)
22+
evaluated_value
23+
end
24+
25+
def validate(validatable_value)
26+
raise ConfigurationError.new(name, validatable_value, self.class.value_class.name) unless validatable_value.is_a?(self.class.value_class)
1927
end
2028
end
2129
end

lib/alchemy/configuration/collection_option.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ def initialize(value:, name:, item_type:, collection_class: Array, **args)
1616
@collection_class = collection_class
1717
@item_option_class = "Alchemy::Configuration::#{item_type.to_s.classify}Option".constantize
1818
super
19-
@value = value.map { @item_option_class.new(value: _1, name: "#{name}_item") }
19+
if value.is_a?(Proc)
20+
@value = value
21+
else
22+
@value = value.map { @item_option_class.new(value: _1, name: "#{name}_item") }
23+
end
2024
end
2125

2226
def value
27+
return @collection_class.new(@value.map(&:value)) unless @value.is_a?(Proc)
28+
evaluated_value = @value.call
29+
@value = evaluated_value.map { @item_option_class.new(value: _1, name: "#{name}_item") }
2330
@collection_class.new(@value.map(&:value))
2431
end
2532

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module Alchemy
4+
class Configuration
5+
class PathNameOption < BaseOption
6+
def self.value_class
7+
::Pathname
8+
end
9+
end
10+
end
11+
end
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module Alchemy
4+
module Configurations
5+
class Importmap < Alchemy::Configuration
6+
option :importmap_path, :path_name, default: ->() { Alchemy::Engine.root.join("config/importmap.rb") }
7+
option :source_paths, :collection, collection_type: Array, item_type: :path_name, default: -> do
8+
[
9+
Alchemy::Engine.root.join("app/javascript"),
10+
Alchemy::Engine.root.join("vendor/javascript")
11+
]
12+
end
13+
option :name, :string, default: "alchemy_admin"
14+
end
15+
end
16+
end

lib/alchemy/configurations/main.rb

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "alchemy/configurations/default_language"
55
require "alchemy/configurations/default_site"
66
require "alchemy/configurations/format_matchers"
7+
require "alchemy/configurations/importmap"
78
require "alchemy/configurations/mailer"
89
require "alchemy/configurations/preview"
910
require "alchemy/configurations/sitemap"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
RSpec.describe Alchemy::Configurations::Importmap do
6+
let(:configuration) { described_class.new }
7+
8+
describe "default values" do
9+
describe "#importmap_path" do
10+
subject { configuration.importmap_path }
11+
it { is_expected.to eq(Alchemy::Engine.root.join("config/importmap.rb")) }
12+
end
13+
14+
describe "#source_paths" do
15+
subject { configuration.source_paths }
16+
it {
17+
is_expected.to eq(
18+
[
19+
Alchemy::Engine.root.join("app/javascript"),
20+
Alchemy::Engine.root.join("vendor/javascript")
21+
]
22+
)
23+
}
24+
end
25+
26+
describe "#name" do
27+
subject { configuration.name }
28+
it { is_expected.to eq("alchemy_admin") }
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)