This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module IconHelper | |
ICON_MAPPINGS = { | |
# Generic actions | |
:save => 'fa-check', | |
:cancel => 'fa-times', | |
:back => 'fa-arrow-left', | |
# Record actions | |
:new => 'fa-plus', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |