Skip to content

Instantly share code, notes, and snippets.

View pjbelo's full-sized avatar

Paulo Belo pjbelo

View GitHub Profile
@pjbelo
pjbelo / react-native-watchman-clear.txt
Created May 12, 2018 15:36
React native and watch cache clean
watchman watch-del-all
rm -rf $TMPDIR/react-*
rm -rf node_modules
npm cache clean --force
npm install
@pjbelo
pjbelo / git-delete-history.txt
Last active July 6, 2018 16:07
git delete history
## Step 1: remove all history
cd <project-folder>
rm -rf .git
## Step 2: initialize the Git repo
git init
git add .
@pjbelo
pjbelo / has_no_restrict_dependent.rb
Last active July 12, 2018 19:50
before delete/destroy check for restrict dependents [rails]
# Rails
# to use globally, put in class ApplicationRecord
# before delete/destroy check for restrict dependents
# given an object, iterate its associations, and check if the restricted ones are empty or not.
def has_no_restrict_dependent?
self.class.reflect_on_all_associations.all? do |assoc|
( ([:restrict_with_error, :restrict_with_exception].exclude? assoc.options[:dependent]) ||
(assoc.macro == :has_one && self.send(assoc.name).nil?) ||
@pjbelo
pjbelo / seeds.rb
Created July 20, 2018 16:58
split and organize seeds.rb [rails]
# store all seeds inside the folder db/seeds
# files are sorted alphabetically before loading them, so we can make sure the files are processed in order
# choose the filenames wisely, e.g. something like 0100_operators.rb, 0200_companies.rb, 0300_products.rb ...)
#
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].sort.each { |seed| load seed }
@pjbelo
pjbelo / using-pagy.txt
Last active September 7, 2018 15:23
Using Pagy gem with Rails and Bootstrap
Include in gemfile:
gem 'pagy'
for global access, include in application_controller.rb:
include Pagy::Backend
and in application_helper.rb:
include Pagy::Frontend
In controller (eg. BlogPosts), where items define number of items per page:
@pjbelo
pjbelo / rails-generate-scaffold.txt
Created September 14, 2018 21:52
rails generate scaffold
rails g scaffold Model field:type field:type
rails generate scaffold Model [field[:type][:index/uniq] field[:type][:index/uniq]] [otherModel:references] [options]
references or belongs_to
type: binary, boolean, date, datetime, decimal, float, integer, primary_key, string, text, time, timestamp
Examples:
`rails generate scaffold post`
@pjbelo
pjbelo / Compiling-assets-locally.md
Last active September 26, 2018 12:36
[Rails] Compiling assets locally [Heroku]

Compiling assets locally

If a public/assets/manifest.yml is detected in your app, Heroku will assume you are handling asset compilation yourself and will not attempt to compile your assets. To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated.

RAILS_ENV=production bundle exec rake assets:precompile

@pjbelo
pjbelo / mySQL-cheatsheet.md
Last active October 11, 2018 17:27
mySQL cheatsheet

Access MySQL server from the mysql client using a username and password (MySQL will prompt for a password):

mysql -u [username] -p;

Exit

exit;
@pjbelo
pjbelo / RVM-cheatsheet.md
Last active October 15, 2018 20:37
RVM cheatsheet

Install RVM

https://rvm.io/rvm/install/

Update RVM

rvm get stable

Install Ruby dependencies

@pjbelo
pjbelo / load_environment_variables.rb
Last active October 17, 2018 17:20
[Rails] Load environment variables from file `config/local_env.yml`. Place file in `config/initializers/`
# Loads environment variables from file config/local_env.yml
# access vars using ENV['VAR_NAME']
env_file = File.join(Rails.root, 'config', 'local_env.yml')
if File.exists?(env_file)
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value
end
end