Skip to content

Instantly share code, notes, and snippets.

View owen2345's full-sized avatar

Owen Peredo Diaz owen2345

View GitHub Profile
@owen2345
owen2345 / uniqueness_job.rb
Created September 20, 2019 08:29
Sidekiq uniqueness jobs. Permits to avoid duplicated jobs already enqueued or scheduled.
# frozen_string_literal: true
require 'sidekiq/api'
module UniquenessJob
extend ActiveSupport::Concern
included do
class << self
alias_method :client_push_old, :client_push
def client_push(item)
@owen2345
owen2345 / Gemfile
Created August 31, 2019 16:51
Multiple man subscribe testing
gem 'multiple_man'
gem 'bunny-mock'
@owen2345
owen2345 / result.yaml
Last active May 8, 2019 09:40
Yaml does not permit to share values between documents in the same file. Then this script helps with that problem.
---
apiVersion: extensions/v1beta1
kind: Deployment
env: &env_vars
val1: "val1"
val2: "val2"
---
apiVersion: extensions/v1beta1
kind: Service
env:
@owen2345
owen2345 / secrets_generator.py
Created May 8, 2019 09:29
Kubernetes secrets generator from env vars (secrets.yml content and env vars for deployment.yml). Original code here: https://github.com/TelluIoT/kubernetes-env-to-secrets/blob/master/README.md
# required python 3
# command: python <location_of_the_file>/secrets_generator.py --env <path_to_env_var_file> --name <secrets_name>
#sample: python ./secrets_generator.py --env .env --name my_secrets
import copy
import argparse
import sys
import configparser
import itertools
import base64
from string import Template
class CommentsController < ApplicationController
def users_comments
# SOLUTION 1: a bit complicated to add pagination and its sql performance is bad when there is a lot of comments,
# because the sql query is returning all matched comments
@user_comments = Post.all.includes(:comments).eager_load(comments: :author)
.where(author: {username: params[:username]}).map(&:comments)
.flatten
# SOLUTION 2: easy to add pagination and its sql performance is much better
@user_comments = Comment.joins(:post, :author).where(author: {username: params[:username]})
#
# Converts multidimensional array into one-dimensional array
# @param array_data [Array]: Multidimensional array to be converted into one-dimensional array
# @param res [Array]: Internal param control (ignore it)
# @sample 1: flatten([1,2,[3,4]]) => [1,2,3,4]
# @sample 1: flatten([1,2,[3,4], [[5], [6, [7]]]]) => [1,2,3,4]
# @return [Array] Returns a new array that is a one-dimensional
def flatten(array_data, res = [])
array_data.each{|v| v.is_a?(Array) ? flatten(v, res) : res.push(v) }
res
@owen2345
owen2345 / sqlite3-to-mysql.rb
Last active December 15, 2016 14:01
Camaleon CMS migrate sqlite3 to mysql DB
#Take it line by line
pending_line = []
query_end = ');' # take care with insert content's ending with ");"
ARGF.each do |line|
# fix for: mapping values are not allowed in this context at (content used by rails for serialized model attributes)
if line.start_with?('INSERT INTO') && !line.strip.end_with?(query_end)
pending_line = [line.gsub("\n", '')]
next
end
if pending_line.length > 0 && !line.strip.end_with?(query_end)
@owen2345
owen2345 / settings.html.erb
Created November 11, 2016 14:13
Sample Theme Settings
<!-- /my_theme/views/admin/settings.html.erb -->
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><%= t('.top_offer', default: 'Top Offer') %></h3>
</div>
<div class="panel-body">
<div class="form-group">
<label><%= t('.top_offer_product', default: 'Offer Product') %></label><br>
<%= select_tag "theme_option[top_offer]", options_from_collection_for_select(post_type.posts.public_posts.decorate, :id, :the_title, current_theme.get_option('top_offer')), include_blank: true, class: 'form-control' %>
</div>
@owen2345
owen2345 / paper_trail.rb
Last active September 7, 2016 19:19
(PaperTrail Extension) If you just want a summary of changes for a particular attribute of a model, you can use the history_from_audits_for method
# config/initializers/paper_trail.rb
PaperTrail::Model.module_eval do
# return an array of changes for specific attribute (old to newer)
# Sample use: Subscriber.find(2).history_from_audits_for(:first_name)
# Sample result: [{version: <PaperTrail::Version>, model: <Subscriber>}, {version: <PaperTrail::Version>, model: <MyModel>},....]
# Sample print: Subscriber.find(2).history_from_audits_for(:first_name).map{|item| item[:model].first_name }
# Required: Diffing Versions of paper trail
def history_from_audits_for(attr_name)
res = []
self.versions.each do |v|
@owen2345
owen2345 / Gemfile
Last active November 13, 2016 14:18
Camaleon CMS Create Own Custom Fields (Simple Country Select)
#/themes/e_shop/config/Gemfile
gem 'country_select'