Skip to content

Instantly share code, notes, and snippets.

View thomasklemm's full-sized avatar

Thomas Klemm thomasklemm

View GitHub Profile
@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'
@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 / 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 / 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|
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 / 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
@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 / task_list_feature_spec.rb
Created July 14, 2015 16:58
RSpec feature specs: Example feature and scenario structure for a list of records
require 'rails_helper'
feature 'task list' do
feature 'listing tasks' do
scenario 'opening the list with the default filters and sorting'
end
feature 'interacting with a task in the list' do
scenario 'adding a task'
@thomasklemm
thomasklemm / user_spec_helper.rb
Created July 20, 2015 09:10
How to properly encapsulate and include helper methods in RSpec
module UserSpecHelper
def my_spec_helper_method(*args)
# ...
end
end
RSpec.configure do |config|
config.include UserSpecHelper
@thomasklemm
thomasklemm / code_generator.rb
Created September 12, 2012 09:05
Code Generator
LETTERS = ('a'..'z').to_a
NUMBERS = (0..9).to_a
LETTERS_AND_NUMBERS = LETTERS.concat(NUMBERS)
# Generate a random unique ID
# in any length
class CodeGenerator
def generate(n=16)
code = []
n.times {code << LETTERS_AND_NUMBERS.sample}