Skip to content

Instantly share code, notes, and snippets.

View spencerldixon's full-sized avatar

Spencer Dixon spencerldixon

View GitHub Profile
@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 / 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 / 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 / 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_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 / active_link_helpers.rb
Last active February 3, 2017 13:31
Various Active Link Helpers for Rails views