This file contains 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' |
This file contains 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 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 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 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 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 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 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 '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' |
This file contains 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 UserSpecHelper | |
def my_spec_helper_method(*args) | |
# ... | |
end | |
end | |
RSpec.configure do |config| | |
config.include UserSpecHelper |
This file contains 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
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} |
OlderNewer