Skip to content

Instantly share code, notes, and snippets.

View khalilgharbaoui's full-sized avatar
🎯
Focusing

Khalil Gharbaoui khalilgharbaoui

🎯
Focusing
View GitHub Profile
@khalilgharbaoui
khalilgharbaoui / db.rake
Created February 3, 2019 21:29 — forked from hopsoft/db.rake
Rails rake tasks for dump & restore of PostgreSQL databases
# Original source: https://gist.github.com/hopsoft/56ba6f55fe48ad7f8b90
# Merged with: https://gist.github.com/kofronpi/37130f5ed670465b1fe2d170f754f8c6
# Benefits of: https://gist.github.com/e12e/e0c7d2cc1d30d18c8050b309a43450ac
# And fixes of: https://gist.github.com/joelvh/f50b8462611573cf9015e17d491a8a92
namespace :db do
desc 'Dumps the database to backups'
task dump: :environment do
dump_fmt = ensure_format(ENV['format'])
dump_sfx = suffix_for_format(dump_fmt)
backup_dir = backup_directory(Rails.env, create: true)

Settting up a Container Registry with docker-gitlab

This should be used for new users to getting started with the container registry feature on docker-gitlab.

Requirements

@khalilgharbaoui
khalilgharbaoui / rspec_rails_cheetsheet.rb
Created April 9, 2019 12:40 — forked from mlr/rspec_rails_cheetsheet.rb
Rspec Rails cheatsheet with expect syntax (including capybara matchers)
# Model
expect(@user).to have(1).error_on(:username) # checks whether there is an error in username
expect(@user.errors[:username]).to include("can't be blank") # check for the error message
# Rendering
expect(response).to render_template(:index)
# Redirecting
class Hash
def has_keys?(*keys)
keys.inject(self) do |pointer, key|
return false if !pointer.respond_to?(:has_key?) || !pointer.has_key?(key)
pointer[key]
end
true
end
end
@khalilgharbaoui
khalilgharbaoui / register_chrome_with_window_size.rb
Last active September 8, 2019 05:14 — forked from mars/register_chrome_with_window_size.rb
Set window size for Capybara/Selenium/chromedriver
# This works best in 2019 forward because its basically the code from the gem itself with some tweaks added:
# https://github.com/teamcapybara/capybara/blob/master/lib/capybara/registrations/drivers.rb
# The additional option/flags work for me but you probebly want to checkout what you would need here first:
# https://peter.sh/experiments/chromium-command-line-switches/
Capybara.register_driver :selenium_chrome_headless do |app|
# Capybara::Selenium::Driver.load_selenium
browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
opts.args << '--window-size=1920,1080'
@khalilgharbaoui
khalilgharbaoui / rails-jsonb-queries
Created October 7, 2019 00:30 — forked from mankind/rails-jsonb-queries
Rails-5 postgresql-9.6 jsonb queries
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")
@khalilgharbaoui
khalilgharbaoui / spec_helper.rb
Created October 19, 2019 23:19 — forked from pauljamesrussell/spec_helper.rb
RSpec matcher for ensuring a model is accepting nested attributes for an association, and accepting/rejecting the right values.
# Use: it { should accept_nested_attributes_for(:association_name).and_accept({valid_values => true}).but_reject({ :reject_if_nil => nil })}
RSpec::Matchers.define :accept_nested_attributes_for do |association|
match do |model|
@model = model
@nested_att_present = model.respond_to?("#{association}_attributes=".to_sym)
if @nested_att_present && @reject
model.send("#{association}_attributes=".to_sym,[@reject])
@reject_success = model.send("#{association}").empty?
end
model.send("#{association}").clear
@khalilgharbaoui
khalilgharbaoui / vcr_webmock_and_live_http_simultaneously.rb
Created November 21, 2019 08:07 — forked from jspillers/vcr_webmock_and_live_http_simultaneously.rb
Sandbox test to get VCR, WebMock, and "real" http all working side by side in the same spec suite
require 'rubygems'
require 'rspec'
require 'webmock'
require 'vcr'
require 'pry'
# in a Rails app, this would be in an initializer
WebMock.disable_net_connect!(
allow_localhost: true,
net_http_connect_on_start: true
@khalilgharbaoui
khalilgharbaoui / upgrade-postgres-9.5-to-9.6.md
Created December 29, 2019 08:16 — forked from delameko/upgrade-postgres-9.5-to-9.6.md
Upgrading PostgreSQL from 9.5 to 9.6 on Ubuntu 16.04

TL;DR

Install Postgres 9.6, and then:

sudo pg_dropcluster 9.6 main --stop
sudo pg_upgradecluster 9.5 main
sudo pg_dropcluster 9.5 main