0

I'm using a presenter object to make some complex data available to my views. Part of the data I need there is intended to be rendered by javascript, and for this I would like to use the gon gem to share the data to the browser.

This my (simple for now), app/presenters/similarity_presenter.rb object:

class SimilarityPresenter
  attr_reader :similarities
  def initialize(similarities)
    @similarities = similarities
    gon.histories = @similarities.each { |s| [s.id, s.data(10)] }
  end
end

When I run my action:

  def index
    period = Period.default
    @similarity_presenter = SimilarityPresenter.new Similarity.all
  end

I get a undefined local variable or method 'gon',

Don Giulio
  • 2,946
  • 3
  • 43
  • 82

1 Answers1

0

First way I found is to pass the gon variable to the presenter:

  def index
    period = Period.default
    @similarity_presenter = SimilarityPresenter.new gon, Similarity.all
  end

and in the presenter:

class SimilarityPresenter
  attr_reader :similarities
  def initialize(gon, similarities)
    @similarities = similarities
    gon.histories = @similarities.each do |s|
      [s.id, s.data(10)]
    end
  end
end

This works, but I must admit that I'm not too fond of having to pass the variable in every controller I use the presenter from.

Don Giulio
  • 2,946
  • 3
  • 43
  • 82