Skip to content

Instantly share code, notes, and snippets.

View darrenterhune's full-sized avatar

Darren Terhune darrenterhune

  • Canada
View GitHub Profile
@darrenterhune
darrenterhune / hooks_generator.rb
Created March 20, 2019 22:26
Hook into rails model generators to update a scope that requires changes each time a new model is added
# config/environments/development.rb
config.generators do |generator|
generator.orm :hooks
end
# lib/generators/rails/hooks/hooks_generator.rb
require 'rails/generators/active_record/model/model_generator'
#!/usr/bin/env ruby
# frozen_string_literal: true
class String
def underscore
return self unless /[A-Z-]|::/.match?(self)
word = to_s.gsub("::", "/")
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
word.tr!("-", "_")
@darrenterhune
darrenterhune / table_helpers.rb
Last active January 5, 2020 23:29
A rspec expectations helper that makes expecting things in html tables easier
# frozen_string_literal: true
module WithinTables
# Use to test the values of columns for certain row
#
# Add <tt>offset</tt> to start matching at a certain column
#
# %tr
# %td Hi
# %td Hello
#<RuboCop::Cop::Registry:0x00007fec7340f7b0
@cops_by_cop_name=
{"Migration/DepartmentName"=>[RuboCop::Cop::Migration::DepartmentName],
"Bundler/DuplicatedGem"=>[RuboCop::Cop::Bundler::DuplicatedGem],
"Bundler/GemComment"=>[RuboCop::Cop::Bundler::GemComment],
"Bundler/InsecureProtocolSource"=>[RuboCop::Cop::Bundler::InsecureProtocolSource],
"Bundler/OrderedGems"=>[RuboCop::Cop::Bundler::OrderedGems],
"Gemspec/DuplicatedAssignment"=>[RuboCop::Cop::Gemspec::DuplicatedAssignment],
"Gemspec/OrderedDependencies"=>[RuboCop::Cop::Gemspec::OrderedDependencies],
"Gemspec/RequiredRubyVersion"=>[RuboCop::Cop::Gemspec::RequiredRubyVersion],
class UsersController < ApplicationController
before_action :set_user, only: %i[ show edit update destroy ]
# GET /users or /users.json
def index
@users = User.all
end
# GET /users/1 or /users/1.json
def show
@darrenterhune
darrenterhune / active_admin_export_worker.rb
Last active December 14, 2022 13:37
Simple activeadmin export full table csv using a sidekiq background worker
# app/workers/active_admin_export_worker.rb
class ActiveAdminExportWorker
include Sidekiq::Worker
sidekiq_options queue: 'high'
def perform(options = {})
model = options[:model_name].classify.constantize
path = "#{Rails.root.to_s}/tmp/#{filename(options[:name])}"
columns = model.send(:column_names)