Skip to content

Commit

Permalink
Merge pull request #2950 from projectblacklight/default-config
Browse files Browse the repository at this point in the history
Accept multiple default configuration blocks
  • Loading branch information
tpendragon committed Jan 18, 2023
2 parents 487fe37 + 66282bd commit 4edd9be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ def property(key, default: nil)
end

def default_configuration(&block)
@default_configuration = block
@default_configuration_initialized = false
@default_configurations ||= []

if block
@default_configurations << block

block.call if @default_configuration_initialized
end

@default_configurations
end

def initialize_default_configuration
@default_configuration.call
@default_configurations&.map(&:call)
@default_configuration_initialized = true
end

Expand Down
22 changes: 22 additions & 0 deletions spec/models/blacklight/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,26 @@
expect { config.view.a = '123' }.to raise_error(FrozenError)
end
end

describe '.default_configuration' do
it 'adds additional default configuration properties' do
Blacklight::Configuration.default_configuration do
Blacklight::Configuration.default_values[:a] = '123'
end

Blacklight::Configuration.default_configuration do
Blacklight::Configuration.default_values[:b] = 'abc'
end

expect(Blacklight::Configuration.default_values[:a]).to eq '123'
expect(Blacklight::Configuration.default_values[:b]).to eq 'abc'
ensure
# reset the default configuration
Blacklight::Configuration.default_values.delete(:a)
Blacklight::Configuration.default_values.delete(:b)

Blacklight::Configuration.default_configuration.delete_at(1)
Blacklight::Configuration.default_configuration.delete_at(2)
end
end
end

0 comments on commit 4edd9be

Please sign in to comment.