impressionist icon indicating copy to clipboard operation
impressionist copied to clipboard

Use impressionist within Grape::Endpoint

Open sonxurxo opened this issue 11 years ago • 4 comments

Hi

I'm using Grape to build an API. I'd like to use impressionist within the Grape endpoints, but I'm getting this error:

undefined method `impressionist' for #<Grape::Endpoint:0x007f9205f6bd70

I suppose it's related to use impressionist outside a controller. Is there any way to use it there?

Thank you in advance

sonxurxo avatar Sep 18 '14 10:09 sonxurxo

+1 @sonxurxo have you figured out a solution for your problem?

codespore avatar Sep 21 '14 03:09 codespore

Not yet, I didn't have time to dive on this. In the following days I'll be working on this, I'll post here my findings.

sonxurxo avatar Sep 21 '14 08:09 sonxurxo

just use:

      imp = Impression.new
      imp.impressionable_type = object.class.to_s
      imp.impressionable_id = object.id
      imp.user_id = current_user.nil? ? 0 : current_user.id
      imp.controller_name = "topics"
      imp.action_name = "show"
      imp.request_hash = Digest::SHA2.hexdigest(Time.now.to_f.to_s+rand(10000).to_s)
      imp.ip_address = env['REMOTE_ADDR']
      imp.session_hash = request.session_options[:id]
      imp.referrer = request.referer
      imp.save

And to update the counter cache use this:

  slave = Impressionist::UpdateCounters.new(object)
  slave.update

nguyenchiencong avatar Nov 13 '14 05:11 nguyenchiencong

@nguyenchiencong Thank you, it's working for me.

sonxurxo avatar Dec 03 '14 08:12 sonxurxo