Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocumentPresenter has a view_context, not a controller #1328

Closed
flyingzumwalt opened this issue Nov 12, 2015 · 1 comment
Closed

DocumentPresenter has a view_context, not a controller #1328

flyingzumwalt opened this issue Nov 12, 2015 · 1 comment
Milestone

Comments

@flyingzumwalt
Copy link

The Pattern

A Presenter is a class that is aware of both your Model and your View. Your ApplicationHelper should create these instances by calling MyPresenter.new(object, self), meaning that template within the Presenter is a reference to the view rendering context, giving you access to all of the helper methods that would be available in a view template.

class MyPresenter
  def initialize(object, view_context)
    @object = object
    @view_context = view_context
  end
end

you initialize it like this from a presenter helper method, where the helper method passes self into the presenter. In a View Helper, self is a view context. You could call it view_context, template, or helper. It is not a Controller.

module ApplicationHelper
  def presenter(object)
    presenter_class.new(object, self)
  end
end

Blacklight::DocumentPresenter uses the wrong names to do the right thing

In [app/helpers/concerns/blacklight_helper_behavior.rb]

##
# Returns a document presenter for the given document
def presenter(document)
  presenter_class.new(document, self)
end

This matches with the canonical implementation of this pattern. The second argument is a view context, not a controller. This doesn't match with what the DocumentPresenter says it expects: [app/presenters/blacklight/document_presenter.rb]

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

The DocumentPresenter is mis-naming that variable and the documentation on that class is incorrect. It claims to want an ActionController::Base as its second argument and calls it "@controller" but really it's getting a view_context, which is what it should be getting, and it should call it either view_context, template, or helper.

References

http://nithinbekal.com/posts/rails-presenters/
http://railscasts.com/episodes/287-presenters-from-scratch?autoplay=true

@flyingzumwalt
Copy link
Author

Note: Even the tests call it a request_context, not controller.

# spec/presenters/document_presenter_spec.rb#L5-9
let(:request_context) { double }
let(:presenter) { Blacklight::DocumentPresenter.new(document, request_context, config) }

@cbeer cbeer modified the milestones: 6.x, 7.x Nov 17, 2015
@jcoyne jcoyne closed this as completed in 9ce262b Jul 7, 2016
jcoyne added a commit that referenced this issue Jul 7, 2016
Rename presenter controller argument to view_context; fixes #1328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants