Skip to content

Instantly share code, notes, and snippets.

@the-spectator
the-spectator / read-access.sql
Last active May 17, 2019 07:29 — forked from oinopion/read-access.sql
How to create read only user in PostgreSQL
-- Create a group
CREATE ROLE readaccess;
-- Grant access to existing tables #sidenote: please switch to DB for which you want to give access
GRANT USAGE ON SCHEMA public TO readaccess;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readaccess;
-- Grant access to future tables
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readaccess;
@the-spectator
the-spectator / find_in_batches_with_order.rb
Last active October 18, 2019 12:29
Introduce find_in_batches_with_order for having find in batches with order
# Create file config/initializers/find_in_batches_with_order.rb with follwing code.
ActiveRecord::Batches.class_eval do
## Only flat order structure is supported now
## example: [:forename, :surname] is supported but [:forename, {surname: :asc}] is not supported
def find_in_batches_with_order(ids: nil, order: [], batch_size: 1000)
relation = self
arrangement = order.dup
index = order.find_index(:id)
unless index
@the-spectator
the-spectator / colours.sh
Last active November 2, 2019 05:07
Git Pre-Commit Hook For Rubocop
# Linux Echo colours
red='\033[0;31m'
nc='\033[0m'
black='\033[0;30m'
dark_grey='\033[1;30m'
light_red='\033[1;31m'
green='\033[0;32m'
lgreen='\033[1;32m'
orange='\033[0;33m'
yellow='\033[1;33m'
# .irbrc
IRB.conf[:PROMPT_MODE] = :SIMPLE # default is :DEFAULT
# Or, invoke irb with the prompt mode by
# terminal
❱ irb --prompt simple
{
PROMPT_I => "" # Simple prompt
PROMPT_S => "" # Prompt for continued strings
PROMPT_C => "" # Prompt for continued statement
RETURN => "" # Format to return value
}
%N # command name which is running
%m # to_s of main object (self)
%l # type of string(", ', /, ]), `]' is inner %w[...]
%NNi # indent level. NN is digits and means as same as printf("%NNd").
%NNn # line number.
# IRB Classic prompt
IRB.conf[:PROMPT_MODE][:CLASSIC] = {
:PROMPT_I => "%N(%m):%03n:%i> ", # irb(main):001:0>
:PROMPT_S => "%N(%m):%03n:%i%l ", # irb(main):003:0"
:PROMPT_C => "%N(%m):%03n:%i* ", # irb(main):005:0*
:RETURN => "%s\n" # used to printf #
}
vs
# IRB Simple prompt
# .irbrc
IRB.conf[:PROMPT_MODE] = :SIMPLE # default is :DEFAULT
# Or, invoke irb with the prompt mode by
# terminal
❱ irb --prompt simple
# We can get application class by
>> Rails.application.class.name
# => "Meetup::Application"
## Since we are only interested in application name, we can get it by
>> Rails.application.class.module_parent.name
# => "Meetup"
# We can get rails environment by simply calling
>> Rails.env
# => "development"