Skip to content

Instantly share code, notes, and snippets.

View hlxwell's full-sized avatar
⛷️
Focusing

Michael He hlxwell

⛷️
Focusing
View GitHub Profile

This was tested using Ruby 2.0.0 and Rails 4.0.0.rc1.

Bower

  1. Set the install directory for Bower components:

    // .bowerrc
    {

"directory": "app/assets/components"

@hlxwell
hlxwell / render_to_file.rb
Last active December 20, 2015 20:59
How to render text in model
# One thing I don't know is how to pass @var to template.
def to_html
request = ActionDispatch::Request.new Hash.new
# I am using Gon also.
Gon.clear
Gon::Request.instance_variable_set(:@request_id, request.object_id)
Gon::Request.env = {}
# Assign gon variables
Gon.some_variables = self.some_variables
#!/bin/sh
# Optware pre-installation script, Leon Kos 2006, 2008
# added -verbose_wget to some lines, MrAlvin 2009
REPOSITORY=http://ipkg.nslu2-linux.org/feeds/optware/ddwrt/cross/stable
TMP=/tmp
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/opt/sbin
unset LD_PRELOAD
unset LD_LIBRARY_PATH
@hlxwell
hlxwell / gist:f1e0084d1cb5517d86a5
Created June 15, 2014 09:25
Get Primebook book questions
Book.all.each do |book|
next if !book.pages.any? {|p| p.questions.count > 0}
puts "============ #{book.name} ==============="
book.pages.each do |page|
next if page.questions.count == 0
puts " Page##{page.page_number}"
page.questions.each do |question|
puts " * Question: #{question.content}"
question.question_options.each do |option|
puts " - #{option.content} #{"√" if option.is_correct_answer}"
@hlxwell
hlxwell / a.js
Created July 17, 2014 04:24
create a promise
function longRunJob() {
var deferred = $q.defer();
setTimeout(function(){
deferred.notify('About to greet ' + name + '.');
deferred.resolve('Hello, ' + name + '!');
deferred.reject('Greeting ' + name + ' is not allowed.');
}, 10000)
return deferred.promise;
}
@hlxwell
hlxwell / kana_methods.rb
Created August 19, 2014 05:13
Judge if string is Kana or not.
class String
def is_katakana?
(self =~ /^[゠-ヿ -〿]+$/) == 0
end
def is_half_katakana?
(self =~ /^[⦅-゚ -〿]+$/) == 0
end
@hlxwell
hlxwell / gist:4d68db4c628537ceaefd
Created September 1, 2014 16:08
routes.rb level permission control
# Method 1 =============================================
class ProjectManagerChecker
def self.matches?(request)
request.env['warden'].user.role == 'project_manager'
end
end
# routes.rb
get '/' => 'project_managers#index', :constraints => ProjectManagerChecker
@hlxwell
hlxwell / test_wifi.rb
Last active August 29, 2015 14:06
wifi testing
prev_time = Time.now
100000.times do
request = WifiRequest.where(client_mac_addr: "B0:8E:1A:50:03:ED").last
next if request.request_at == prev_time
puts request.slice(:client_mac_addr, :power, :request_at)
prev_time = request.request_at
sleep 5
end
@hlxwell
hlxwell / animal.py
Created September 25, 2014 02:18
Python test
class Animal(object):
def __init__(self):
self.name = "Animal"
def eat(self, food = "nothing"):
print self.name + " eats " + food
# ==================================================
import animal
@hlxwell
hlxwell / fabric.rb
Created September 25, 2014 02:19
fabric Test
from fabric.api import *
env.hosts = ["app1", "app2"]
env.use_ssh_config = True
def host_type():
run('df -h')