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 IncDec | |
# Mostly copied from RSpec::Matchers::BuiltIn | |
class ChangeDetails | |
attr_reader :actual_before, :actual_after | |
def initialize(matcher_name, receiver=nil, message=nil, &block) | |
if receiver && !message | |
raise( | |
ArgumentError, | |
"`#{matcher_name}` requires either an object and message " \ |
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
twitter.com##.ActivityItem:-abp-contains(liked a Tweet from) | |
twitter.com##.ActivityItem:-abp-contains(liked a video from) | |
twitter.com##.ActivityItem:-abp-contains(liked a photo from) | |
twitter.com##.ActivityItem:-abp-contains(Retweeted a video from) | |
twitter.com##.ActivityItem:-abp-contains(Retweeted a photo from) |
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
$ cat db/migrate/* app/models/*.rb | |
class CreatePosts < ActiveRecord::Migration[5.1] | |
def change | |
create_table :posts do |t| | |
t.string :title | |
t.timestamps | |
end | |
end | |
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
>> Book.select(:author, :isbn).first | |
Book Load (0.4ms) SELECT "books"."author", "books"."isbn" FROM "books" ORDER BY "books"."id" ASC LIMIT $1 [["LIMIT", 1]] | |
=> #<Book id: nil, isbn: "0787967076", author: "Steven B. Sample"> | |
>> Book.select(:id, :author, :isbn).first | |
Book Load (0.3ms) SELECT "books"."id", "books"."author", "books"."isbn" FROM "books" ORDER BY "books"."id" ASC LIMIT $1 [["LIMIT", 1]] | |
=> #<Book id: 5, isbn: "0787967076", author: "Steven B. Sample"> | |
>> Book.find(5).update(isbn: nil, author: "heyo") | |
Book Load (0.5ms) SELECT "books".* FROM "books" WHERE "books"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]] | |
(0.1ms) BEGIN | |
SQL (19.7ms) UPDATE "books" SET "isbn" = $1, "author" = $2, "escaped_author" = $3, "updated_at" = $4 WHERE "books"."id" = $5 [["isbn", nil], ["author", "heyo"], ["escaped_author", "heyo"], ["updated_at", 2017-12-13 20:40:57 UTC], ["id", 5]] |
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 MyTweak | |
def execute(sql, name=nil) | |
if caller.detect {|x| x =~ /20171010151334/ } && sql !~ /SHOW TIME ZONE/ | |
puts sql | |
else | |
super | |
end | |
end | |
end | |
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter |
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 TravelOption < ApplicationRecord | |
has_many :travels | |
end | |
class Travel < ApplicationRecord | |
belongs_to :travel_option | |
end | |
>> TravelOption.joins(:travels).where(location_id: 1).select(:location_id, :price, :tour_id).map(&:tour_id) |
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 Arel | |
module Visitors | |
class MyWhereSsqlWithoutSql < Arel::Visitors::ToSql | |
def initialize(inner_visitor, *args, &block) | |
@inner_visitor = inner_visitor | |
super(*args, &block) | |
end | |
private |
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
a = {zzz: [{b: 1, c: 2}, {b:3, c:4}]} | |
# slight variant on https://stackoverflow.com/questions/10676608/ruby-deleting-all-instances-of-a-particular-key-from-hash-of-hashes | |
class Hash | |
def deep_reject_key!(key) | |
keys.each {|k| delete(k) if k == key || self[k] == self[key] } | |
values.each do |v| | |
v.deep_reject_key!(key) if v.is_a? Hash | |
v.each {|i| i.deep_reject_key!(key) if i.is_a? Hash } if v.is_a? Array |
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
Synvert::Rewriter.new 'rails', 'redirect_with_notice' do | |
description <<-EOS | |
Combines flash setting and redirect to. | |
flash[:notice] = "huzzah" | |
redirect_to root_path | |
=> | |
redirect_to root_path, notice: "huzzah" | |
EOS | |
within_file 'app/controllers/**/*rb' do | |
within_node type: 'def' do |
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
irb(main):014:0> puts RUBY_VERSION ; n = 5000 ; Benchmark.bm {|x| x.report { n.times {' '.strip.empty?} } ; x.report { n.times { ' ' =~ /^\s*$/ } } } | |
2.1.5 | |
user system total real | |
0.010000 0.000000 0.010000 ( 0.007651) | |
0.000000 0.010000 0.010000 ( 0.005907) | |
irb(main):016:0> puts RUBY_VERSION ; n = 5000 ; Benchmark.bm {|x| x.report { n.times {' '.strip.empty?} } ; x.report { n.times { ' ' =~ /^\s*$/ } } } | |
2.2.0 | |
user system total real | |
0.000000 0.000000 0.000000 ( 0.001773) |
NewerOlder