Skip to content

Commit

Permalink
Evaluate blacklight configuration conditionals in an explicit evaluat…
Browse files Browse the repository at this point in the history
…ion context, by default the current controller
  • Loading branch information
cbeer committed Nov 5, 2015
1 parent a3a1932 commit ad33243
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 91 deletions.
22 changes: 21 additions & 1 deletion app/controllers/concerns/blacklight/controller.rb
Expand Up @@ -22,7 +22,7 @@ module Blacklight::Controller

# extra head content
helper_method :has_user_authentication_provider?
helper_method :blacklight_config
helper_method :blacklight_config, :blacklight_configuration_context
helper_method :search_action_url, :search_action_path, :search_facet_url
helper_method :blacklight_path

Expand All @@ -44,6 +44,26 @@ def blacklight_config

protected

##
# Context in which to evaluate blacklight configuration conditionals
def blacklight_configuration_context
@blacklight_configuration_context ||= Blacklight::Configuration::Context.new(self)
end

##
# Determine whether to render the bookmarks control
# (Needs to be available globally, as it is used in the navbar)
def render_bookmarks_control?
has_user_authentication_provider? and current_or_guest_user.present?
end

##
# Determine whether to render the saved searches link
# (Needs to be available globally, as it is used in the navbar)
def render_saved_searches?
has_user_authentication_provider? and current_user
end

# @return [Blacklight::Path] a memoized instance of the parameter state.
def blacklight_path
@blacklight_path ||= Blacklight::Path.new(params, blacklight_config)
Expand Down
Expand Up @@ -19,6 +19,10 @@ module DefaultComponentConfiguration
add_nav_action(:search_history, partial: 'blacklight/nav/search_history')
end

def render_sms_action? config, options = {}
sms_mappings.present?
end

module ClassMethods

##
Expand Down
12 changes: 0 additions & 12 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Expand Up @@ -322,18 +322,6 @@ def render_grouped_response? response = @response
return response.grouped?
end

##
# Determine whether to render the bookmarks control
def render_bookmarks_control?
has_user_authentication_provider? and current_or_guest_user.present?
end

##
# Determine whether to render the saved searches link
def render_saved_searches?
has_user_authentication_provider? and current_user
end

##
# Returns a document presenter for the given document
def presenter(document)
Expand Down
4 changes: 0 additions & 4 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Expand Up @@ -231,10 +231,6 @@ def bookmarked? document
current_bookmarks.any? { |x| x.document_id == document.id and x.document_type == document.class }
end

def render_sms_action? config, options = {}
!sms_mappings.blank?
end

def render_search_to_page_title_filter(facet, values)
facet_config = facet_configuration_for_field(facet)
filter_label = facet_field_label(facet_config.key)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/blacklight/component_helper_behavior.rb
Expand Up @@ -55,7 +55,7 @@ def render_results_collection_tools(options = {})

def render_filtered_partials(partials, options={}, &block)
content = []
partials.select { |_, config| evaluate_if_unless_configuration config, options }.each do |key, config|
partials.select { |_, config| blacklight_configuration_context.evaluate_if_unless_configuration config, options }.each do |key, config|
config.key ||= key
rendered = render(partial: config.partial || key.to_s, locals: { document_action_config: config }.merge(options))
if block_given?
Expand Down
40 changes: 2 additions & 38 deletions app/helpers/blacklight/configuration_helper_behavior.rb
Expand Up @@ -182,43 +182,7 @@ def per_page_options_for_select
#
# @param [Blacklight::Solr::Configuration::Field] field_config
# @return [Boolean]
def should_render_field? field_config, *args
evaluate_if_unless_configuration field_config, *args
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 displayable_config, *args
return displayable_config if displayable_config === true or displayable_config === false

if_value = !displayable_config.respond_to?(:if) ||
displayable_config.if.nil? ||
evaluate_configuration_conditional(displayable_config.if, displayable_config, *args)

unless_value = !displayable_config.respond_to?(:unless) ||
displayable_config.unless.nil? ||
!evaluate_configuration_conditional(displayable_config.unless, displayable_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 = method(proc_helper_or_boolean).arity

if arity == 0
send(proc_helper_or_boolean)
else
send(proc_helper_or_boolean, *args_for_procs_and_methods)
end
when Proc
proc_helper_or_boolean.call self, *args_for_procs_and_methods
else
proc_helper_or_boolean
end
def should_render_field?(field_config, *args)
blacklight_configuration_context.evaluate_if_unless_configuration field_config, *args
end
end
47 changes: 47 additions & 0 deletions app/models/blacklight/configuration/context.rb
@@ -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
9 changes: 4 additions & 5 deletions spec/helpers/blacklight_helper_spec.rb
Expand Up @@ -76,12 +76,10 @@ def current_search_session
let(:document) { SolrDocument.new('title_display' => "A Fake Document", 'id'=>'8') }

before do
config.add_show_tools_partial(:bookmark, partial: 'catalog/bookmark_control', if: :render_bookmarks_control?)
config.add_show_tools_partial(:bookmark, partial: 'catalog/bookmark_control')
config.add_results_document_tool(:bookmark, partial: 'catalog/bookmark_control', if: :render_bookmarks_control?)
config.add_nav_action(:bookmark, partial: 'blacklight/nav/bookmark', if: :render_bookmarks_control?)
config.add_nav_action(:bookmark, partial: 'blacklight/nav/bookmark')
allow(helper).to receive(:blacklight_config).and_return(config)
allow(helper).to receive(:has_user_authentication_provider?).and_return(true)
allow(helper).to receive(:current_or_guest_user).and_return(User.new)
allow(helper).to receive_messages(current_bookmarks: [])
end

Expand All @@ -96,12 +94,13 @@ def current_search_session

describe "render_index_doc_actions" do
it "should render partials" do
allow(controller).to receive(:render_bookmarks_control?).and_return(true)
response = helper.render_index_doc_actions(document)
expect(response).to have_selector(".bookmark_toggle")
end

it "should be nil if no partials are renderable" do
allow(helper).to receive(:render_bookmarks_control?).and_return(false)
allow(controller).to receive(:render_bookmarks_control?).and_return(false)
expect(helper.render_index_doc_actions(document)).to be_blank
end

Expand Down
25 changes: 0 additions & 25 deletions spec/helpers/configuration_helper_spec.rb
Expand Up @@ -222,32 +222,7 @@
expect(helper.should_render_field?(field_config)).to be false
end
end

describe "#evaluate_configuration_conditional" do
it "should pass through regular values" do
val = double
expect(helper.evaluate_configuration_conditional(val)).to eq val
end

it "should execute a helper method" do
allow(helper).to receive_messages(:my_helper => true)
expect(helper.evaluate_configuration_conditional(:my_helper)).to be true
end

it "should call a helper to determine if it should render a field" do
a = double
allow(helper).to receive(:my_helper_with_an_arg).with(a).and_return(true)
expect(helper.evaluate_configuration_conditional(:my_helper_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(helper.evaluate_configuration_conditional(one_arg_lambda, 1)).to be true
expect(helper.evaluate_configuration_conditional(two_arg_lambda, 1, 2)).to be true
end
end

describe "#search_field_options_for_select" do

before do
Expand Down
8 changes: 4 additions & 4 deletions spec/helpers/facets_helper_spec.rb
Expand Up @@ -47,8 +47,8 @@
@config = Blacklight::Configuration.new do |config|
config.add_facet_field 'basic_field'
config.add_facet_field 'no_show', :show => false
config.add_facet_field 'helper_show', :show => :my_helper
config.add_facet_field 'helper_with_an_arg_show', :show => :my_helper_with_an_arg
config.add_facet_field 'helper_show', :show => :my_custom_check
config.add_facet_field 'helper_with_an_arg_show', :show => :my_custom_check_with_an_arg
config.add_facet_field 'lambda_show', :show => lambda { |context, config, field| true }
config.add_facet_field 'lambda_no_show', :show => lambda { |context, config, field| false }
end
Expand All @@ -71,14 +71,14 @@
end

it "should call a helper to determine if it should render a field" do
allow(helper).to receive_messages(:my_helper => true)
allow(controller).to receive_messages(:my_custom_check => true)
a = double(:items => [1,2], :name=>'helper_show')
expect(helper.should_render_facet?(a)).to be true
end

it "should call a helper to determine if it should render a field" do
a = double(:items => [1,2], :name=>'helper_with_an_arg_show')
allow(helper).to receive(:my_helper_with_an_arg).with(@config.facet_fields['helper_with_an_arg_show'], a).and_return(true)
allow(controller).to receive(:my_custom_check_with_an_arg).with(@config.facet_fields['helper_with_an_arg_show'], a).and_return(true)
expect(helper.should_render_facet?(a)).to be true
end

Expand Down
33 changes: 33 additions & 0 deletions spec/models/blacklight/configuration/context_spec.rb
@@ -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
4 changes: 4 additions & 0 deletions spec/support/controller_level_helpers.rb
Expand Up @@ -5,6 +5,10 @@ module ControllerViewHelpers
def blacklight_path
@blacklight_path ||= Blacklight::Path.new(params, blacklight_config)
end

def blacklight_configuration_context
@blacklight_configuration_context ||= Blacklight::Configuration::Context.new(controller)
end
end

def initialize_controller_helpers(helper)
Expand Down
2 changes: 1 addition & 1 deletion spec/views/_user_util_links.html.erb_spec.rb
Expand Up @@ -12,7 +12,7 @@
it "should render the correct bookmark count" do
count = rand(99)
allow(view).to receive(:blacklight_config).and_return(blacklight_config)
allow(view).to receive(:render_bookmarks_control?).and_return true
allow(controller).to receive(:render_bookmarks_control?).and_return true
allow(view).to receive(:has_user_authentication_provider?).and_return false
allow(view).to receive_message_chain(:current_or_guest_user, :bookmarks, :count).and_return(count)
render :partial => "user_util_links"
Expand Down

0 comments on commit ad33243

Please sign in to comment.