Skip to content

Commit

Permalink
Use new class-level Hanami::Action settings/config (#1216)
Browse files Browse the repository at this point in the history
Use the config now accessible directly on the `Hanami::Action` class as `Hanami::Action.config` (changed in hanami/controller#392).

Also update usage of dry-configurable with `Undefined`: with the new `default_undefined` extension option, `Undefined` takes a special meaning inside dry-configurable. Avoid passing it in our case to make sure our existing behavior is preserved.
  • Loading branch information
timriley authored Oct 7, 2022
1 parent b5cc84c commit 20a388f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ gem "hanami-view", github: "hanami/view", branch: "main"
gem "hanami-devtools", github: "hanami/devtools", branch: "main"

gem "dry-types", "~> 1.0"
gem "dry-configurable", github: "dry-rb/dry-configurable"
gem "dry-system", github: "dry-rb/dry-system"

group :test do
gem "dotenv"
Expand Down
17 changes: 8 additions & 9 deletions lib/hanami/configuration/actions.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "dry/configurable"
require "hanami/action/configuration"
require_relative "actions/cookies"
require_relative "actions/sessions"
require_relative "actions/content_security_policy"
Expand All @@ -26,13 +25,13 @@ class Actions

attr_accessor :content_security_policy

attr_reader :base_configuration
protected :base_configuration
attr_reader :base_config
protected :base_config

def initialize(*, **options)
super()

@base_configuration = Hanami::Action::Configuration.new
@base_config = Hanami::Action.config.dup
@content_security_policy = ContentSecurityPolicy.new do |csp|
if assets_server_url = options[:assets_server_url]
csp[:script_src] += " #{assets_server_url}"
Expand All @@ -45,7 +44,7 @@ def initialize(*, **options)

def initialize_copy(source)
super
@base_configuration = source.base_configuration.dup
@base_config = source.base_config.dup
@content_security_policy = source.content_security_policy.dup
end

Expand All @@ -66,7 +65,7 @@ def finalize!
# @since 2.0.0
# @api private
def settings
base_configuration.settings + self.class.settings
Hanami::Action.settings + self.class.settings
end

private
Expand All @@ -86,15 +85,15 @@ def configure_defaults
def method_missing(name, *args, &block)
if config.respond_to?(name)
config.public_send(name, *args, &block)
elsif base_configuration.respond_to?(name)
base_configuration.public_send(name, *args, &block)
elsif base_config.respond_to?(name)
base_config.public_send(name, *args, &block)
else
super
end
end

def respond_to_missing?(name, _incude_all = false)
config.respond_to?(name) || base_configuration.respond_to?(name) || super
config.respond_to?(name) || base_config.respond_to?(name) || super
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/hanami/extensions/action/slice_configured_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def define_new
end

def configure_action(action_class)
action_class.config.settings.each do |setting|
action_class.settings.each do |setting|
# Configure the action from config on the slice, _unless it has already been configured
# by a parent slice_, and re-configuring it for this slice would make no change.
#
Expand Down
13 changes: 9 additions & 4 deletions lib/hanami/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module Hanami
# @see Hanami::Settings::DotenvStore
# @since 2.0.0
class Settings
# @api private
Undefined = Dry::Core::Constants::Undefined

# Exception for errors in the definition of settings.
#
# Its message collects all the individual errors that can be raised for each setting.
Expand All @@ -59,11 +62,13 @@ def to_s

# @api private
def initialize(store = EMPTY_STORE)
errors = config._settings.map(&:name).reduce({}) do |errs, name|
public_send("#{name}=", store.fetch(name) { Dry::Core::Constants::Undefined })
errs
errors = config._settings.map(&:name).each_with_object({}) do |name, errs|
value = store.fetch(name, Undefined)
next if value.eql?(Undefined)

public_send("#{name}=", value)
rescue => e # rubocop:disable Style/RescueStandardError
errs.merge(name => e)
errs[name] = e
end

raise InvalidSettingsError, errors if errors.any?
Expand Down
6 changes: 3 additions & 3 deletions lib/hanami/settings/env_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Settings
# @since 2.0.0
# @api private
class EnvStore
Undefined = Dry::Core::Constants::Undefined
NO_ARG = Object.new.freeze

attr_reader :store, :hanami_env

Expand All @@ -21,9 +21,9 @@ def initialize(store: ENV, hanami_env: Hanami.env)
@hanami_env = hanami_env
end

def fetch(name, default_value = Undefined, &block)
def fetch(name, default_value = NO_ARG, &block)
name = name.to_s.upcase
args = default_value == Undefined ? [name] : [name, default_value]
args = default_value.eql?(NO_ARG) ? [name] : [name, default_value]

store.fetch(*args, &block)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/hanami/configuration/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "hanami/configuration"
require "hanami/configuration/actions"
require "hanami/action/configuration"
require "hanami/action"

RSpec.describe Hanami::Configuration, "#actions" do
let(:configuration) { described_class.new(app_name: app_name, env: :development) }
Expand Down Expand Up @@ -43,7 +43,7 @@
end

it "includes all base action settings" do
expect(actions.settings).to include(Hanami::Action::Configuration.settings)
expect(actions.settings).to include(Hanami::Action.settings)
end
end
end
Expand Down

0 comments on commit 20a388f

Please sign in to comment.