Skip to content

Instantly share code, notes, and snippets.

View spencerldixon's full-sized avatar
👾
soju.so

Spencer Dixon spencerldixon

👾
soju.so
View GitHub Profile
@spencerldixon
spencerldixon / active_link_helpers.rb
Last active February 3, 2017 13:31
Various Active Link Helpers for Rails views
@spencerldixon
spencerldixon / csv_exporter_module.rb
Created February 24, 2017 15:35
Simple CSV Exporter Module
module CsvExporter
def self.export_results(results)
folder = Rails.root.join("public", "csv_exports")
FileUtils.mkdir_p(folder)
filepath = folder.join("csv_export_#{DateTime.now.to_i}.csv")
CSV.open(filepath, "wb") do |csv|
csv << self.headers
results.each do |result|
csv << self.format_row(result)
@spencerldixon
spencerldixon / mailchimp_subscriber.rb
Created May 16, 2019 15:37
Mailchimp subscriber
API_KEY = Rails.application.secrets.mailchimp_api_key
LIST_ID = Rails.application.secrets.mailchimp_list_id
BASE_URL = 'https://us17.api.mailchimp.com/3.0'.freeze
AUTH = { username: 'apikey', password: API_KEY }
module Mailchimp
include HTTParty
def self.subscribe(first_name, last_name, email)
request_url = "#{BASE_URL}/lists/#{LIST_ID}/members"
@spencerldixon
spencerldixon / csv_importer.rb
Last active August 19, 2019 20:14
Robust CSV Importer
class CsvImporterJob < ApplicationJob
queue_as :default
# Handle any cleanup after perform
after_perform { |job| clean_up_csv_upload(job) }
def perform(file, user)
# Use begin/rescue - raise StandardError in importer if something goes wrong
# Rescue here with email notification to user
begin
@spencerldixon
spencerldixon / secrets.yml
Created May 19, 2019 15:43
secrets.yml with defaults
default: &default
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
development:
<<: *default
test:
<<: *default
staging:
@spencerldixon
spencerldixon / application_controller.rb
Created June 12, 2019 13:44
Only admins can access this
def require_admin
return if current_user.admin?
redirect_to root_path, notice: "Only admins can access this area"
end
@spencerldixon
spencerldixon / rails_helper.rb
Created July 19, 2019 10:47
RSpec Boilerplate
config.include FactoryBot::Syntax::Methods
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
config.include Devise::Test::IntegrationHelpers, type: :feature
@spencerldixon
spencerldixon / user_registration_spec.rb
Last active August 28, 2020 12:01
Capybara feature specs for a simple Devise installation
require 'rails_helper'
RSpec.feature 'User Registration', type: :feature do
let(:new_user) { FactoryBot.build(:user) }
let(:user) { FactoryBot.create(:user) }
scenario 'User can sign up' do
visit root_path
first(:link, 'Sign up').click
@spencerldixon
spencerldixon / pdf.js
Created November 8, 2019 13:33
html2pdf with Puppeteer
'use strict';
const puppeteer = require('puppeteer');
const fs = require('fs'); //Filesystem
let content = fs.readFileSync(process.argv[2], "utf-8");
const createPdf = async() => {
let browser;
try {
@spencerldixon
spencerldixon / groups_spec.rb
Created August 25, 2020 14:45
RSpec feature tests for simple CRUDing of a resource
require 'rails_helper'
RSpec.feature "Groups", type: :feature do
let(:user) { FactoryBot.create(:user) }
let(:admin) { FactoryBot.create(:user, :with_admin) }
let(:group) { FactoryBot.create(:group) }
let(:groups) { FactoryBot.create_list(:group, 2) }
let(:new_group) { FactoryBot.build(:group) }
scenario 'require authentication' do