Skip to content

Instantly share code, notes, and snippets.

View thomasklemm's full-sized avatar

Thomas Klemm thomasklemm

View GitHub Profile
@thomasklemm
thomasklemm / associated_records_in_factory_girl.rb
Last active August 29, 2015 14:20
FactoryGirl: Passing attributes to associated records using transient attributes
FactoryGirl.define do
factory :parent do
transient do
child_name nil
child_allowed_to_drive false
end
child do
create(:child, name: child_name, allowed_to_drive: child_allowed_to_drive)
@thomasklemm
thomasklemm / active_record_sqlite_in_memory_db.rb
Created April 27, 2015 21:01
Awesome ActiveRecord bug report script. How to use ActiveRecord and SQLite in a Ruby script.
# Based on http://www.jonathanleighton.com/articles/2011/awesome-active-record-bug-reports/
# Run this script with `$ ruby my_script.rb`
require 'sqlite3'
require 'active_record'
# Use `binding.pry` anywhere in this script for easy debugging
require 'pry'
# Connect to an in-memory sqlite3 database
@thomasklemm
thomasklemm / simplifying_has_many_through.rb
Last active May 15, 2016 22:12
Simplifying a has_many :through (ActiveRecord / Rails). Also a useful boilerplate sharing complex ActiveRecord scenarios.
# Run this script with `$ ruby has_much_fun.rb`
require 'sqlite3'
require 'active_record'
# Use `binding.pry` anywhere in this script for easy debugging
require 'pry'
# Connect to an in-memory sqlite3 database
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
@thomasklemm
thomasklemm / guessing_game.rb
Last active August 29, 2015 14:19
Guess my number game
# Play with `ruby guessing_game.rb`
require 'securerandom'
def next_guess
@guess = STDIN.gets.to_i
# Validate input
next_guess unless @guess >= 1 && @guess <= 100_000
ActiveSupport::Inflector.module_eval do
def parameterize(string, sep = '-', preserve_case: false)
# Overridden to allow for preserving the case with the preserve_case: true option
# Based on https://github.com/rails/rails/blob/5ea3f284a4d07f5572f7ae2a7442cca8761fa8fc/activesupport/lib/active_support/inflector/transliterate.rb#L81
# Replace accented chars with their ascii equivalents
parameterized_string = transliterate(string)
# Turn unwanted chars into the separator
@thomasklemm
thomasklemm / sorted_where.rb
Last active August 29, 2015 14:18
ActiveRecord .sorted_where scope
ActiveRecord::Base.class_eval do
def self.sorted_where(**options)
# Sort records by the first field given
sort_field, ordered_values = options.first[0], options.first[1]
values_and_positions = ordered_values.map.with_index { |id, index| [id, index] }
positions_by_value = Hash[*values_and_positions.flatten]
where(**options).to_a.sort_by do |record|
@thomasklemm
thomasklemm / icon_helper.rb
Created August 26, 2014 19:38
Icon Helper for Font Awesome in Ruby on Rails
module IconHelper
ICON_MAPPINGS = {
# Generic actions
:save => 'fa-check',
:cancel => 'fa-times',
:back => 'fa-arrow-left',
# Record actions
:new => 'fa-plus',
@thomasklemm
thomasklemm / gist:0422fd71a96cfb4cb72a
Last active August 29, 2015 14:01 — forked from trcarden/gist:3295935
SSL on localhost, reusuable across multiple Rails apps
# SSL self signed localhost for rails start to finish, no red warnings.
# 0) Unless present, create `~/.ssl/`
$ mkdir ~/.ssl
# 1) Create your private key (any password will do, we remove it below)
$ openssl genrsa -des3 -out ~/.ssl/localhost.orig.key 2048
@thomasklemm
thomasklemm / project_resource_policy,rb
Created May 11, 2014 18:16
Pundit Policy on Join Models
class Project < AR::B
has_many :project_reources
has_many :resources, through: :project_resources
end
class Resource < AR::B
has_many :project_resources
has_many :projects, through: :project_resources
end
@thomasklemm
thomasklemm / active_record_basic_setup.rb
Last active August 29, 2015 14:00
ActiveRecord without Rails, e.g. in a script
require 'active_record'
ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: ":memory:" )
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define(version: 1) { create_table(:articles) { |t| t.string :title } }
class Article < ActiveRecord::Base; end
Article.create title: 'Quick brown fox'
Article.create title: 'Fast black dogs'
Article.create title: 'Swift green frogs'