Skip to content

Commit

Permalink
Use new Config object instead of classic one
Browse files Browse the repository at this point in the history
  • Loading branch information
mamhoff committed Jan 30, 2025
1 parent f6b8732 commit 5f0f43a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 160 deletions.
8 changes: 6 additions & 2 deletions app/models/alchemy/ingredient_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ def validate_uniqueness(*)
end

def validate_format(format)
matcher = Alchemy::Config.get("format_matchers")[format] || format
if !ingredient.value.to_s.match?(Regexp.new(matcher))
matcher = if format.is_a?(String) || format.is_a?(Symbol)
Alchemy::Config.get("format_matchers").get(format)
else
format
end
if !ingredient.value.to_s.match?(matcher)
ingredient.errors.add(:value, :invalid)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/alchemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

require "alchemy/admin/preview_url"
require "importmap-rails"
require "alchemy/configurations/main"

module Alchemy
YAML_PERMITTED_CLASSES = %w[Symbol Date Regexp]

Config = Alchemy::Configurations::Main.new
# Define page preview sources
#
# A preview source is a Ruby class returning an URL
Expand Down
9 changes: 4 additions & 5 deletions lib/alchemy/admin/preview_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def initialize(routes:)

def url_for(page)
@preview_config = preview_config_for(page)

if @preview_config && uri
uri_class.build(
host: uri.host,
Expand All @@ -60,10 +59,10 @@ def url_for(page)
attr_reader :routes

def preview_config_for(page)
preview_config = Alchemy::Config.get(:preview)
preview_config = Alchemy::Config.preview
return unless preview_config

preview_config[page.site.name] || preview_config
preview_config.for_site(page.site) || preview_config
end

def uri
Expand All @@ -81,8 +80,8 @@ def uri_class
end

def userinfo
auth = @preview_config["auth"]
auth ? "#{auth["username"]}:#{auth["password"]}" : nil
auth = @preview_config.auth
auth.username ? "#{auth["username"]}:#{auth["password"]}" : nil
end
end
end
Expand Down
114 changes: 0 additions & 114 deletions lib/alchemy/config.rb

This file was deleted.

22 changes: 10 additions & 12 deletions lib/alchemy/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ def initialize(configuration_hash = {})
end

def set(configuration_hash)
configuration_hash.transform_keys do |key|
transform_key(key)
end.each do |key, value|
configuration_hash.each do |key, value|
send(:"#{key}=", value)
end
end

def get(key)
send(transform_key(key))
end
alias_method :get, :send
alias_method :[], :get

def fetch(key, default = nil)
get(key) || default
end

def show
self
end

def set_from_yaml(file)
set(
YAML.safe_load(
Expand Down Expand Up @@ -84,11 +88,5 @@ def option(name, type, default: nil)
end
end
end

private

def transform_key(key)
key.tr("/", "_")
end
end
end
8 changes: 1 addition & 7 deletions lib/alchemy/test_support/config_stubbing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ module ConfigStubbing
# @param value [Object] The value you want to return instead of the original one
#
def stub_alchemy_config(key, value)
allow(Alchemy::Config).to receive(:get) do |arg|
if arg == key
value
else
Alchemy::Config.show[arg.to_s]
end
end
allow(Alchemy::Config).to receive(key).and_return(value)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/alchemy_cms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
require_relative "alchemy/admin/preview_url"
require_relative "alchemy/auth_accessors"
require_relative "alchemy/cache_digests/template_tracker"
require_relative "alchemy/config"
require_relative "alchemy/configuration_methods"
require_relative "alchemy/controller_actions"
require_relative "alchemy/deprecation"
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/alchemy/base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Alchemy

describe "#configuration" do
it "returns certain configuration options" do
allow(Config).to receive(:show).and_return({"some_option" => true})
allow(Config).to receive(:some_option).and_return(true)
expect(controller.configuration(:some_option)).to eq(true)
end
end
Expand Down
36 changes: 18 additions & 18 deletions spec/libraries/admin/preview_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
context "with preview configured" do
context "without protocol" do
before do
stub_alchemy_config(:preview, {
stub_alchemy_config(:preview, Alchemy::Configurations::Preview.new({
"host" => "www.example.com"
})
}))
end

it "raises error" do
Expand All @@ -33,9 +33,9 @@

context "as http url" do
before do
stub_alchemy_config(:preview, {
stub_alchemy_config(:preview, Alchemy::Configurations::Preview.new({
"host" => "http://www.example.com"
})
}))
end

it "returns the configured preview url" do
Expand All @@ -45,9 +45,9 @@

context "as https url" do
before do
stub_alchemy_config(:preview, {
stub_alchemy_config(:preview, Alchemy::Configurations::Preview.new({
"host" => "https://www.example.com"
})
}))
end

it "returns the configured preview url with https" do
Expand All @@ -57,13 +57,13 @@

context "and with basic auth configured" do
before do
stub_alchemy_config(:preview, {
stub_alchemy_config(:preview, Alchemy::Configurations::Preview.new({
"host" => "https://www.example.com",
"auth" => {
"username" => "foo",
"password" => "baz"
}
})
}))
end

it "returns the configured preview url with userinfo" do
Expand All @@ -73,9 +73,9 @@

context "with a port configured" do
before do
stub_alchemy_config(:preview, {
stub_alchemy_config(:preview, Alchemy::Configurations::Preview.new({
"host" => "https://www.example.com:8080"
})
}))
end

it "returns the configured preview url with userinfo" do
Expand All @@ -90,11 +90,11 @@

context "that matches the pages site name" do
let(:config) do
{
Alchemy::Configurations::Preview.new({
page.site.name => {
"host" => "http://new.example.com"
}
}
})
end

it "returns the configured preview url for that site" do
Expand All @@ -105,12 +105,12 @@
context "that does not match the pages site name" do
context "with a default configured" do
let(:config) do
{
Alchemy::Configurations::Preview.new({
"Not matching site name" => {
"host" => "http://new.example.com"
},
"host" => "http://www.example.com"
}
})
end

it "returns the default configured preview url" do
Expand All @@ -120,11 +120,11 @@

context "without a default configured" do
let(:config) do
{
Alchemy::Configurations::Preview.new({
"Not matching site name" => {
"host" => "http://new.example.com"
}
}
})
end

it "returns the internal preview url" do
Expand All @@ -138,9 +138,9 @@
let(:page) { create(:alchemy_page, :language_root) }

before do
stub_alchemy_config(:preview, {
stub_alchemy_config(:preview, Alchemy::Configurations::Preview.new({
"host" => "https://www.example.com"
})
}))
end

it "returns the preview url without urlname" do
Expand Down

0 comments on commit 5f0f43a

Please sign in to comment.