Ask A Question

Notifications

You’re not receiving notifications from this thread.

Chartkick and impressionist gem render not working

Ben John Bagley asked in Rails

Ok so how would I go about getting the data into the area chart, do I put that code into a controller or how to I go about testing and using that code?

Reply
Reply

So I've had a play around and I have something like this

<%= area_chart raffle_impressions, refresh: 60, colors: ["#62518C", "#F7AE07"] %>

raffle.rb

class Dashboard::Raffle < ApplicationRecord
  is_impressionable :counter_cache => true, :column_name => :impressions_count, :unique => :all
  has_many :users

  def raffle_impressions_count
    RaffleImpressions.raffle_impressions_count(self)
  end
end

module RaffleImpressions
  def raffle_impressions_count(Dashboard::Raffle)
    raffles = Dashboard::Raffle.all
    time_ranges = [['2017-12-08 08:00:00 -0600', '2017-12-08 09:00:00 -0600'], ['2017-12-08 09:00:00 -0600', '2017-12-08 10:00:00 -0600']]

    raffles_array = [] # don't need raffles_hash, instead just an array to load

    raffles.each do |raffle|
      impressions = []
      time_ranges.each do |time|
        impressions << [[ time[0], time[1] ], raffle.impressionist_count(start_date: time[0], end_date: time[1]) ]
      end

      raffles_array << { name => raffle.title, data => impressions }
    end

    return raffles_array
  end
end

But I am recieveing an error screenshot error

Reply

That's not how you setup a method to receive params

I would suggest some reading to get a better understanding

And really my example was only to illustrate the general concept you're after. So with your setup, you'd be looking for something more like:

raffle.rb

class Dashboard::Raffle < ApplicationRecord
  is_impressionable :counter_cache => true, :column_name => :impressions_count, :unique => :all
  has_many :users
  include 'RaffleImpressions'
end

raffle_impressions.rb

module RaffleImpressions
  def raffle_impressions_count
    time_ranges = [['2017-12-08 08:00:00 -0600', '2017-12-08 09:00:00 -0600'], ['2017-12-08 09:00:00 -0600', '2017-12-08 10:00:00 -0600']]

    impressions = []

    time_ranges.each do |time|
      impressions << [[ time[0], time[1] ], self.impressionist_count(start_date: time[0], end_date: time[1]) ]
    end

    return { name => self.title, data => impressions }
  end
end

Then you should be able to call Dashboard::Raffle.first.raffle_impressions_count to get an output for a single raffle. If you wanted multiple, you'd have to iterate over each Raffle, calling raffle_impressions_count on each iteration and load that into an array that you'd then pass to chartkick.

Reply

So I've set this up but I get the following error

Here is the file struture with the code in the files above screenshot

Reply

Try this:

raffles.rb

class Dashboard::Raffle < ApplicationRecord
  is_impressionable :counter_cache => true, :column_name => :impressions_count, :unique => :all
  has_many :users
  include RaffleImpressions
end

app/models/concerns/raffle_impressions.rb

module RaffleImpressions
  extend ActiveSupport::Concern

  def raffle_impressions_count
    time_ranges = [['2017-12-08 08:00:00 -0600', '2017-12-08 09:00:00 -0600'], ['2017-12-08 09:00:00 -0600', '2017-12-08 10:00:00 -0600']]

    impressions = []

    time_ranges.each do |time|
      impressions << [[ time[0], time[1] ], self.impressionist_count(start_date: time[0], end_date: time[1]) ]
    end

    return { :name => self.name, :data => impressions }
  end
end

This is using concerns, so I had to add extend ActiveSupport::Concern to the raffle_impressions.rb. Testing the execution real quick I noticed I had the return hash wrong, I forgot to make name and data symbols... so be sure to update that in yours.

Also, to help you with debugging, be sure to watch Chris' video on it: https://gorails.com/episodes/debugging-with-better-errors

Reply

So I have updated the code to the above and now a few errors are being flagged

error #1
error #2
error #3

I think were getting close.

Reply

Did you put raffle_impressions.rb in app/models/concerns/raffle_impressions.rb and restart your console / rails server?

Reply

Forgot to restart the server, oops.

Ok I get this screenshot the impressions are been show as 2017, not the actual count, plus I'm not seeing the time actually being shown in the axis.

So for example showing the amount of impressions on that raffle per raffle or on all raffles by the hour.

I can't seem to be able to use .goup(:title) or anything after Dashboard::Raffle.first.raffle_impressions_count

Reply

Clone this - https://github.com/nanosplit/deleteme

bundle install
rake db:create
rake db:migrate
rake db:seed
rails s
http://lvh.me:3000

Take a look at the concern and controller:
https://github.com/nanosplit/deleteme/blob/master/app/models/concerns/raffle_impressions.rb
https://github.com/nanosplit/deleteme/blob/master/app/controllers/raffles_controller.rb

This isn't a copy/paste, you're going to have to take a look at what's actually happening, try to understand what is going on at each step, and then work out how to replicate the result in code. Google will be your best friend here. Most of the structure is there for you, you just have to figure out how to get your time ranges sorted out for the given time period you want to record, and then feed that into the raffle_impressions_count method.

Reply

Yeah so I still can't get this working, the example provided is if the raffles were a seperate entity to the dashboard and not actually attached to anything.

I don't believe that passing in the data that I'm trying to pass needs x amount of various other files in need for this to work, as the data is already there and can be shown in the graph.

screenshot of data showing the 222 impressions is spread across the week and not just on the one day.

It's not a diffult thing I'm trying to do here, I can get the data to pass but not actually take into account the day, hour etc i.e. 20 impressions on Thurday and 40 impressions on Friday, they just go into the one bar, not multiple as shown in the Chartkick documentation. I have tried their examples, nothing.

I've followed the chartkick documentation and nothing seems to be working here.

Anything else you can provide here, would be appricated, if not I may as well discontinue the project until a later date or all together.

Reply
I can get the data to pass but not actually take into account the day, hour etc i.e. 20 impressions on Thurday and 40 impressions on Friday

If you work out what I gave you, it will do just what you're wanting... to where it will give you a range of impressions for your raffle based on a given time range.

the example provided is if the raffles were a seperate entity to the dashboard and not actually attached to anything

That doesn't matter at all for what you're trying to do.

It's not a diffult thing I'm trying to do here

Just because it's easy to explain doesn't necessariliy mean it's easy to do in code. I've done these exact kind of gigs plenty of times, so trust me, what I provided you will work. However, I can't do the project for you. I got you 3/4 of the way and gave you guidance to complete the other 1/4.

Best of luck.

Reply

This is a personal project and not a paid gig.

So I'll give it a shot, if not I'll think of something else.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.