Skip to content

Instantly share code, notes, and snippets.

View Samda's full-sized avatar
🏝️
Working from home

Samda Muy Samda

🏝️
Working from home
View GitHub Profile
@Samda
Samda / rails-jsonb-queries
Created March 28, 2022 08:08 — forked from mankind/rails-jsonb-queries
Ruby on 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")
@Samda
Samda / params_to_object.js
Created December 14, 2021 07:45
Parse url params to object
const parseParams = (querystring) => {
// parse query string
const params = new URLSearchParams(querystring);
const obj = {};
// iterate over all keys
for (const key of params.keys()) {
if (params.getAll(key).length > 1) {
@Samda
Samda / reverse_polish_notation.rb
Last active December 8, 2021 08:50
Calculation Reverse Polish Notation with Ruby
#jurias from codewars
def calc(expr)
expr
.split(' ')
.reduce([0]) { |s, i| s << (i =~ /[0-9.]/ ? i.to_f : s.pop(2).reduce(i.to_sym)) }
.pop
end
#my code -_-
def calc1(expression)
@Samda
Samda / gh-pages-deploy.md
Created March 12, 2021 09:55 — forked from cobyism/gh-pages-deploy.md
Deploy to `gh-pages` from a `dist` folder on the master branch. Useful for use with [yeoman](http://yeoman.io).

Deploying a subfolder to GitHub Pages

Sometimes you want to have a subdirectory on the master branch be the root directory of a repository’s gh-pages branch. This is useful for things like sites developed with Yeoman, or if you have a Jekyll site contained in the master branch alongside the rest of your code.

For the sake of this example, let’s pretend the subfolder containing your site is named dist.

Step 1

Remove the dist directory from the project’s .gitignore file (it’s ignored by default by Yeoman).

@Samda
Samda / Capfile
Created February 19, 2021 16:51 — forked from dv/Capfile
Capistrano 3 + Rails 4 + Whenever + DelayedJob + Nginx + Postgresql
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Rails (includes bundler, rails/assets and rails/migrations)
require 'capistrano/rails'
# Whenever
@Samda
Samda / setup.md
Created February 3, 2021 03:51
run rails jobs with crontab ubuntu 18.04LTS

#Note '/home/deploy/apps/sms_system/current' is your production directory. #edit files $crontab -e

#sample excutable file */1 * * * * /bin/bash /home/deploy/apps/sms_system/sms_system.sh #your rails jobs 1 */1 * * * * /bin/bash -l -c 'cd /home/deploy/apps/sms_system/current && RAILS_ENV=production bundle exec rails client_payment_due:import_payment_dues --silent' #your rails jobs 2 0 1 * * * /bin/bash -l -c 'cd /home/deploy/apps/sms_system/current && RAILS_ENV=production bundle exec rails send_sms:client --silent'

@Samda
Samda / message_controller.rb
Created January 18, 2021 06:27
Axios response and Rails validations, errors json response
def create
message = Message.new(message_params.merge({created_by: current_user.id}))
if message.save
render json: MessageSerializer.new(message, options).serialized_json
else
render json: { error: message.errors.messages }.to_json, status: 422
end
end
# Axios
@Samda
Samda / input-files.html
Created January 6, 2021 14:11
Input file accepts file types
<!-- CSV only -->
<input type="file" accept=".csv" />
<!-- only CSV and XLS XLSX -->
<input id="fileSelect" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
<!-- only excel 2007 and up -->
<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
<!-- Only pdf -->
@Samda
Samda / schedule.rb
Created December 25, 2020 10:26
Rails 6 and whenever with rbenv
set :output, "#{path}/log/cron.log"
set :env_path, '"$HOME/.rbenv/shims":"$HOME/.rbenv/bin"'
job_type :rails, %q{ cd :path && PATH=:env_path:"$PATH" RAILS_ENV=:environment bundle exec rails :task --silent :output }
job_type :runner, %q{ cd :path && PATH=:env_path:"$PATH" bin/rails runner -e :environment ':task' :output }
job_type :script, %q{ cd :path && PATH=:env_path:"$PATH" RAILS_ENV=:environment bundle exec bin/:task :output }
every 1.minute do
rails 'message:send_to_clients'
end
@Samda
Samda / http(s)_client.md
Last active December 17, 2020 02:23
HTTP Client Ruby gem for Ruby on Rails app

faraday

rest-client

nestful

http

httpi