Skip to content

Commit

Permalink
Rename presenter controller argument to view_context; fixes #1328
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jul 6, 2016
1 parent 908066f commit 0db4f19
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
18 changes: 10 additions & 8 deletions app/presenters/blacklight/index_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true
module Blacklight
class IndexPresenter
attr_reader :document, :configuration, :view_context

# @param [SolrDocument] document
# @param [ActionController::Base] controller scope for linking and generating urls
# @param [ActionView::Base] view context scope for linking and generating urls
# @param [Blacklight::Configuration] configuration
def initialize(document, controller, configuration = controller.blacklight_config)
def initialize(document, view_context, configuration = view_context.blacklight_config)
@document = document
@view_context = view_context
@configuration = configuration
@controller = controller
end

##
Expand All @@ -21,14 +23,14 @@ def label(field_or_string_or_proc, opts = {})
value = case field_or_string_or_proc
when Symbol
config = field_config(field_or_string_or_proc)
@document[field_or_string_or_proc]
document[field_or_string_or_proc]
when Proc
field_or_string_or_proc.call(@document, opts)
field_or_string_or_proc.call(document, opts)
when String
field_or_string_or_proc
end

value ||= @document.id
value ||= document.id
field_values(config, value: value)
end

Expand Down Expand Up @@ -59,11 +61,11 @@ def field_value field, options = {}
# @param [Blacklight::Configuration::Field] solr field configuration
# @param [Hash] options additional options to pass to the rendering helpers
def field_values(field_config, options={})
FieldPresenter.new(@controller, @document, field_config, options).render
FieldPresenter.new(view_context, document, field_config, options).render
end

def field_config(field)
@configuration.index_fields.fetch(field) { Configuration::NullField.new(field) }
configuration.index_fields.fetch(field) { Configuration::NullField.new(field) }
end
end
end
24 changes: 13 additions & 11 deletions app/presenters/blacklight/show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ class ShowPresenter
extend Deprecation
self.deprecation_horizon = 'Blacklight version 7.0.0'

attr_reader :document, :configuration, :view_context

# @param [SolrDocument] document
# @param [ActionController::Base] controller scope for linking and generating urls
# @param [ActionView::Base] view context scope for linking and generating urls
# @param [Blacklight::Configuration] configuration
def initialize(document, controller, configuration = controller.blacklight_config)
def initialize(document, view_context, configuration = view_context.blacklight_config)
@document = document
@view_context = view_context
@configuration = configuration
@controller = controller
end

##
Expand All @@ -23,7 +25,7 @@ def initialize(document, controller, configuration = controller.blacklight_confi
# @option options [Array<String>] :exclude array of format shortnames to not include in the output
# @deprecated moved to ShowPresenter#link_rel_alternates
def link_rel_alternates(options = {})
LinkAlternatePresenter.new(@controller, @document, options).render
LinkAlternatePresenter.new(view_context, document, options).render
end

##
Expand All @@ -35,7 +37,7 @@ def link_rel_alternates(options = {})
def html_title
if view_config.html_title_field
fields = Array.wrap(view_config.html_title_field)
f = fields.detect { |field| @document.has? field }
f = fields.detect { |field| document.has? field }
f ||= 'id'
field_values(field_config(f))
else
Expand All @@ -51,9 +53,9 @@ def html_title
# @return [String]
def heading
fields = Array.wrap(view_config.title_field)
f = fields.detect { |field| @document.has? field }
f ||= @configuration.document_model.unique_key
field_values(field_config(f), value: @document[f])
f = fields.detect { |field| document.has? field }
f ||= configuration.document_model.unique_key
field_values(field_config(f), value: document[f])
end

# @deprecated
Expand Down Expand Up @@ -88,15 +90,15 @@ def field_value field, options={}
# @param [Blacklight::Configuration::Field] solr field configuration
# @param [Hash] options additional options to pass to the rendering helpers
def field_values(field_config, options={})
FieldPresenter.new(@controller, @document, field_config, options).render
FieldPresenter.new(controller, document, field_config, options).render
end

def view_config
@configuration.view_config(:show)
configuration.view_config(:show)
end

def field_config(field)
@configuration.show_fields.fetch(field) { Configuration::NullField.new(field) }
configuration.show_fields.fetch(field) { Configuration::NullField.new(field) }
end
end
end

0 comments on commit 0db4f19

Please sign in to comment.