Skip to content

Instantly share code, notes, and snippets.

@maxcal
maxcal / home.html.erb
Created January 30, 2020 05:40
have_text with hypenated text
<h1 id="title">Hello-World</h1>
<p>Just your average Rails app. But with no features.</p>
require 'rails_helper'
RSpec.describe Post, type: :model do
let(:blog) { FactoryBot.create(:blog) }
it "can be created from an assocation" do
post = blog.posts.create
expect(post.persisted?).to be_truthy
end
end
require 'benchmark'
require 'active_support/all'
arr = Array.new(1000_0000) { rand(1...20) }
Benchmark.bm do |benchmark|
benchmark.report "arr.sum { |n| n.odd? ? n : 0 }" do
arr.sum { |n| n.odd? ? n : 0 }
end
benchmark.report "arr.select(&:odd?).sum" do
@maxcal
maxcal / policy.rb
Last active January 24, 2019 00:36
Shared context for Pundit Policy specs
# spec/support/shared_contexts/policy.rb
# require this file from rails_helper.rb
require 'pundit/rspec'
RSpec.shared_context "Policy" do
let(:user) { build_stubbed(:user) }
subject { described_class }
let(:model_class) { described_class.to_s.chomp('Policy').constantize }
let(:permitted_attributes) do
described_class.new(User.new, model_class).permitted_attributes
@maxcal
maxcal / path_helpers.rb
Last active November 21, 2018 19:04
Redefining a named path helper in Rails.
module Reviews
module PathHelpers
def self.included(base)
base.module_eval do
alias_method :old_book_reviews_path, :book_reviews_path
[Film, Book].each do |model|
singular, plural = model.model_name.singular, model.model_name.plural
alias_method "old_#{singular}_reviews_path".to_sym,
"#{singular}_reviews_path"
define_method "#{singular}_reviews_path" do |reviewable = nil|
@maxcal
maxcal / gist:2279aec9dcdd52095c42b9b4f984b661
Created October 12, 2018 17:18
Testing lists with sass
$spacers: '0', '4px', '8px', '16px', '24px', '32px', '40px';
// Aliases for easy use
$spacer-0: nth($spacers, 1) !default; // 0
$spacer-1: nth($spacers, 2) !default; // 4px
$spacer-2: nth($spacers, 3) !default; // 8px
$spacer-3: nth($spacers, 4) !default; // 16px
$spacer-4: nth($spacers, 5) !default; // 24px
$spacer-5: nth($spacers, 6) !default; // 32px
$spacer-6: nth($spacers, 7) !default; // 40px
@maxcal
maxcal / stations.json
Created January 17, 2017 17:11
Remote Wind Fixture data
[{"id":43,"latitude":56.4340324,"longitude":12.7268848,"name":"Båstad v","slug":"bastad-v","path":"/stations/bastad-v","status":"unresponsive","observations":[]},{"id":1,"latitude":55.37704,"longitude":14.15016,"name":"Dennis station","slug":"station-1","path":"/stations/station-1","status":"unresponsive","observations":[]},{"id":16,"latitude":63.397091,"longitude":13.075093,"name":"New Camp Åre","slug":"camp","path":"/stations/camp","status":"unresponsive","observations":[]},{"id":99,"latitude":60.5262,"longitude":18.3766,"name":"Örskär","slug":"orskar","path":"/stations/orskar","status":"active","observations":[{"id":2426287,"station_id":99,"speed":11.9,"direction":null,"max_wind_speed":13.8,"min_wind_speed":0.0,"temperature":null,"created_at":"2017-01-17T17:10:44.180Z","updated_at":"2017-01-17T17:10:44.180Z","speed_calibration":1.0}]},{"id":97,"latitude":57.3672,"longitude":17.0986,"name":"Ölands Norra Udde","slug":"olands-norra-udde","path":"/stations/olands-norra-udde","status":"active","observations":[{
class Parent < ActiveRecord::Base
has_many :children
has_many :skills, through: :children
def self.with_skill(skill_type)
eager_load(:skills, :children).map do |p|
p.children = [] unless p.skills.any? { |s| s.skill_type = skill_type }
p
end
end
end
require 'benchmark'
@language = 'english'
string = 'Hello world'
Benchmark.bmbm do |x|
x.report { 100000.times { a = Hash[@language, string] } }
x.report { 100000.times { a = { @language => string } } }
x.report { 100000.times { a = { "{@language}" => string } } }
end
body { background-color: #eeeeee; padding: 0; margin: 5px; overflow-y: scroll; }
#HTMLReporter { font-size: 11px; font-family: Monaco, "Lucida Console", monospace; line-height: 14px; color: #333333; }
#HTMLReporter a { text-decoration: none; }
#HTMLReporter a:hover { text-decoration: underline; }
#HTMLReporter p, #HTMLReporter h1, #HTMLReporter h2, #HTMLReporter h3, #HTMLReporter h4, #HTMLReporter h5, #HTMLReporter h6 { margin: 0; line-height: 14px; }
#HTMLReporter .banner, #HTMLReporter .symbolSummary, #HTMLReporter .summary, #HTMLReporter .resultMessage, #HTMLReporter .specDetail .description, #HTMLReporter .alert .bar, #HTMLReporter .stackTrace { padding-left: 9px; padding-right: 9px; }
#HTMLReporter #jasmine_content { position: fixed; right: 100%; }
#HTMLReporter .version { color: #aaaaaa; }
#HTMLReporter .banner { margin-top: 14px; }