Skip to content

Instantly share code, notes, and snippets.

@jcsky
jcsky / sample.rb
Created Mar 8, 2018
how to refactor nested private method?
View sample.rb
class A
def hi
action_1
action_2
action_3
end
private
def action_1
View enviroments_production.rb
# 原本的,會出現origin is not allowed by Access-Control-Allow-Origin 的錯誤
config.middleware.insert_before 0, Rack::Cors do
allow do
origins /amazingtalker.com\z/
resource '*',
headers: ['AtToken', 'AtLocale'],
methods: [:get, :post, :delete, :put, :patch, :options],
credentials: true
end
end
View iterator.rb
class SystemLogger
def initialize
@sys_logs = []
end
def add(msg)
timestamp = Time.now.strftime('%Y%m%d_%H%M%S_%N')
@sys_logs << [timestamp, msg]
end
View gist:60cbac8ddb36b9d65a0b2fc7fa0c3bde
class ApplicationController < ActionController::Base
before_action :set_locale
private
def set_locale
if current_user
if !current_user.locale.present?
set_i18n_locale_from_browser
current_user.update(locale: I18n.locale.to_s)
@jcsky
jcsky / cookie_locale.txt
Last active Jul 25, 2016
set and get locale through cookie
View cookie_locale.txt
View Taipower-filter-top10-company
library(reshape2)
library(dplyr)
library(vegan)
library(ggplot2)
library(ggdendro)
Taipower <- read.csv(("https://johnsonhsieh.github.io/DSC2016-R/data/Taipower_top100.csv"),fileEncoding = "big5")
aop = Taipower$attr_of_procurement
T = mutate(Taipower, category = substr(aop,1,5), category_detail = unlist(lapply(strsplit(as.character(aop), split = ">"),"[",2)))
View try ruby module and class
module Dojo
A = "Dojo - A"
B = "Dojo - B"
class Dojo_child
def print_a
p "Dojo_child print_a - A: #{A}"
end
end
module Kata
@jcsky
jcsky / db_pull.rake
Created May 26, 2016
dump staging/production db and restore to local db, just use rake:db_pull
View db_pull.rake
namespace :db do
desc 'Pull staging db to development'
task :pull => [:dump, :restore]
task :dump do
dumpfile = "#{Rails.root}/tmp/latest.dump"
puts 'PG_DUMP on staging database...'
staging = Rails.application.config.database_configuration['staging']
system "ssh user@server 'PGPASSWORD=\"#{staging['password']}\" pg_dump -U postgres #{staging['database']} -h #{staging['host']} -F t' > #{dumpfile}"
puts 'Done!'
@jcsky
jcsky / admin-product.rb
Created Apr 5, 2016 — forked from ottodranik/admin-product.rb
Example: Use Acts-As-Taggable-On gem with Active-Admin and rails observers
View admin-product.rb
ActiveAdmin.register Product do
permit_params :name, :category_id, :description, :text, :slug, :in_slider, :tag_ids => []
filter :category
filter :base_tags
filter :name
filter :text
filter :created_at
filter :updated_at
@jcsky
jcsky / index.html
Created Mar 2, 2016
Qunit TDD practice
View index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TDD Practice</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.20.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>