Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Evaluate blacklight configuration conditionals in an explicit evaluat…
…ion context, by default the current controller
- Loading branch information
Showing
13 changed files
with
121 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Blacklight | ||
class Configuration | ||
class Context | ||
attr_reader :context | ||
|
||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
## | ||
# Evaluate conditionals for a configuration with if/unless attributes | ||
# | ||
# @param displayable_config [#if,#unless] an object that responds to if/unless | ||
# @return [Boolean] | ||
def evaluate_if_unless_configuration(config, *args) | ||
return config if config === true or config === false | ||
|
||
if_value = !config.respond_to?(:if) || | ||
config.if.nil? || | ||
evaluate_configuration_conditional(config.if, config, *args) | ||
|
||
unless_value = !config.respond_to?(:unless) || | ||
config.unless.nil? || | ||
!evaluate_configuration_conditional(config.unless, config, *args) | ||
|
||
if_value && unless_value | ||
end | ||
|
||
def evaluate_configuration_conditional(proc_helper_or_boolean, *args_for_procs_and_methods) | ||
case proc_helper_or_boolean | ||
when Symbol | ||
arity = context.method(proc_helper_or_boolean).arity | ||
|
||
if arity == 0 | ||
context.send(proc_helper_or_boolean) | ||
else | ||
context.send(proc_helper_or_boolean, *args_for_procs_and_methods) | ||
end | ||
when Proc | ||
proc_helper_or_boolean.call context, *args_for_procs_and_methods | ||
else | ||
proc_helper_or_boolean | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'spec_helper' | ||
|
||
describe Blacklight::Configuration::Context do | ||
|
||
subject { described_class.new(context) } | ||
let(:context) { double } | ||
|
||
describe "#evaluate_configuration_conditional" do | ||
it "should pass through regular values" do | ||
val = double | ||
expect(subject.evaluate_configuration_conditional(val)).to eq val | ||
end | ||
|
||
it "should execute a helper method" do | ||
allow(context).to receive_messages(:my_check => true) | ||
expect(subject.evaluate_configuration_conditional(:my_check)).to be true | ||
end | ||
|
||
it "should call a helper to determine if it should render a field" do | ||
a = double | ||
allow(context).to receive(:my_check_with_an_arg).with(a).and_return(true) | ||
expect(subject.evaluate_configuration_conditional(:my_check_with_an_arg, a)).to be true | ||
end | ||
|
||
it "should evaluate a Proc to determine if it should render a field" do | ||
one_arg_lambda = lambda { |context, a| true } | ||
two_arg_lambda = lambda { |context, a, b| true } | ||
expect(subject.evaluate_configuration_conditional(one_arg_lambda, 1)).to be true | ||
expect(subject.evaluate_configuration_conditional(two_arg_lambda, 1, 2)).to be true | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters