diff --git a/lib/alchemy/configuration/regexp_option.rb b/lib/alchemy/configuration/regexp_option.rb index c4d46748da..484a6a3ae3 100644 --- a/lib/alchemy/configuration/regexp_option.rb +++ b/lib/alchemy/configuration/regexp_option.rb @@ -6,8 +6,26 @@ module RegexpOption private def validate_regexp(value, option_name) - raise TypeError, "#{option_name} must be set as a Regexp, given #{value.inspect}" unless value.is_a?(Regexp) - value + raise TypeError, "#{option_name} must be set as a String or a regular expression, given #{value.inspect}" unless value.is_a?(String) || value.is_a?(Regexp) + if value.is_a?(String) + matches = /^\/(?.*)\/(?[mixn]*)$/m.match(value) + source = matches[:string].gsub('\/', '/') + options = 0 + lang = nil + matches[:options].each_char do |option| + case option + when 'x' then options |= Regexp::EXTENDED + when 'i' then options |= Regexp::IGNORECASE + when 'm' then options |= Regexp::MULTILINE + when 'n' then options |= Regexp::NOENCODING + else lang = option + end + end + Regexp.new(*[source, options, lang].compact) + else + # It's already a regex + value + end end end end diff --git a/lib/generators/alchemy/install/files/config.yml b/lib/generators/alchemy/install/files/config.yml index d63ea23fb8..f7aa54b79f 100644 --- a/lib/generators/alchemy/install/files/config.yml +++ b/lib/generators/alchemy/install/files/config.yml @@ -199,9 +199,9 @@ link_target_options: [blank] # validates_format_of :url, with: Alchemy.config.get('format_matchers')['url'] # format_matchers: - email: !ruby/regexp '/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/' - url: !ruby/regexp '/\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?\z/ix' - link_url: !ruby/regexp '/^(tel:|mailto:|\/|[a-z]+:\/\/)/' + email: /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/ + url: /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?\z/ix + link_url: /^(tel:|mailto:|/|[a-z]+://)/ # The layout used for rendering the +alchemy/admin/pages#show+ action. admin_page_preview_layout: application diff --git a/spec/libraries/alchemy/configuration_spec.rb b/spec/libraries/alchemy/configuration_spec.rb index 6b8c669f60..7dfb2e49b9 100644 --- a/spec/libraries/alchemy/configuration_spec.rb +++ b/spec/libraries/alchemy/configuration_spec.rb @@ -173,10 +173,16 @@ expect(configuration.email).to eq(/\A.*\z/) end - it "can only be set with a regexp" do + it "can be set with a regexp" do expect do - configuration.email = '/\A.*\z/' - end.to raise_exception(TypeError, /email must be set as a Regexp, given .*/) + configuration.email = /\A.+\z/ + end.to change(configuration, :email).to(/\A.+\z/) + end + + it "can be set with a string" do + expect do + configuration.email = '/\A\/+\z/i' + end.to change(configuration, :email).to(/\A\/+\z/i) end end diff --git a/spec/libraries/alchemy/configurations/main_spec.rb b/spec/libraries/alchemy/configurations/main_spec.rb index a2b1253a5f..901f51a553 100644 --- a/spec/libraries/alchemy/configurations/main_spec.rb +++ b/spec/libraries/alchemy/configurations/main_spec.rb @@ -33,4 +33,15 @@ end.to change { subject.output_image_quality }.from(85).to(90) end end + + describe "generated config.yml" do + let(:config_template) { Alchemy::Engine.root.join("lib", "generators", "alchemy", "install", "files", "config.yml") } + let(:default_config) { Alchemy::Configurations::Main.new } + + it "does not change the defaults" do + expect do + default_config.set_from_yaml(config_template) + end.not_to change { default_config.to_h } + end + end end