Viral Rails

Downloading the winners! -Video 10

Downloading the winners! -Video 10

On February 17, 2016, Posted by , In Winners, With No Comments

Table Of Contents

Chris Oliver from Gorails.com shares How to download your winners

Add an admin to your local machine

AdminUser.create!(:email => ‘admin@prelaunchr.com’, :password => ‘changeme’, :password_confirmation => ‘changeme’)

Navigate to localhost:3000/admin

Now click on users

Copy this code into your admin/users.rb file

  
 
require 'csv'

   ActiveAdmin.register User do
       csv do
       column :id
       column :email
       column :referral_code
       column :referrer_id
       column :created_at
       column :updated_at
     end

     actions :index, :show

    action_item do
      link_to "Download Winners", winners_admin_users_path
    end

    collection_action :winners, method: :get do
      stops = User::REFERRAL_STEPS.map{|stop| stop["count"]}

     winners = Hash.new {|h,k| h[k]=[]}
     User.all.each do |user|
        found = nil

      stops.reverse_each do |stop|
        found = stop if stop <= user.referrals.count and !found
      end

     winners[found] << user if found
    end

      winners = CSV.generate do |csv|
       winners.each do |stop, list|
        csv << [stop, "------"]
         list.each do |user|
          csv << [user.email, user.referrals.count]
         end
        end
       end
       send_data winners, filename: "winners.csv"
      end
  end
 

Add heroku

git add .

git commit -m “your commit”

git push origin master

git push heroku master

heroku run rails c

AdminUser.create!(:email => ‘admin@prelaunchr.com’, :password => ‘changeme’, :password_confirmation => ‘changeme’)

You should be all set and the button should be working on active admin

Leave a Reply

Your email address will not be published. Required fields are marked *