Skip to content

Instantly share code, notes, and snippets.

View jelaniwoods's full-sized avatar

Jelani Woods jelaniwoods

View GitHub Profile

Quickstart: Export Data as CSV

Define a Class method

For example, if you wanted to export Photos records from Photogram.

In the photo.rb model, define a class method called to_csv.

class Photo < ApplicationRecord

Prerequesites

  • have the chartkick gem installed
  • have a background job that updates a record with JSON output
  • have a route that displays the JSON output of the job
  • have a chartkick chart that is charting data from a URL

Add jQuery

In Gemfile,

Prerequisites

  1. Install Chartkick
  2. Have a homepage in your Rails app
  3. Have some script that outputs a String with JSON

For a reference, my script looks like:

FROM buildpack-deps:focal
### base ###
RUN yes | unminimize \
&& apt-get install -yq \
zip \
unzip \
bash-completion \
build-essential \
htop \
@jelaniwoods
jelaniwoods / seeding.md
Last active December 8, 2020 15:53 — forked from raghubetina/seeding.md
Seeding your database

Seeding your database

It is very helpful to take the time to pre-populate your database with some dummy data before you start working on any features. It's nice to have some data to look at to see whether you are going down the right path.

It's also very helpful to other developers on the project, so they can get started quickly right after they clone.

Here are two ways to create seed data:

Manually + seed_dump

class AddStatusToItem < ActiveRecord::Migration
  def change
    add_column :items, :scheduler_type, :string, default: "hotseat"
  end
end

This gem is part of the standard ruby library, but we have to require it in our file so we can use the open method for our new_content variable.

I would still word it in more general terms. We need to require it in order to be able to open files from URLs not just files in our project.

wget -0 is a bash command

I would still say wget is the bash command. The -O is a flag that allows you to name the file that you download from the URL.

Very clear example of wget here

This kind of script only really seems necessary if the person working in the project is not really aware of what all the files do— otherwise they could just copy/paste or use git. Just want to mention that up front in case the audience for this post is any Ruby developer, some context on why we need this instead of some other solution, might be nice.

Specifically in our case, students are not aware that each test is in their workspace and that editing those files can affect their score.

This

wget  -O bin/hello_world "url" && chmod 777 bin/hello_world && bin/hello_world

AJAX

Step 1: Ensure jQuery is installed.

  • Check your Gemfile for gem "jquery3", add it if it’s not there.
    • In app/assets/application.js, ensure it looks like:
/*
*= require_self
*//= require rails-ujs
*//= require jquery3