Skip to content

Instantly share code, notes, and snippets.

View xuncheng's full-sized avatar
🎯
Focusing

Xuncheng Wang xuncheng

🎯
Focusing
View GitHub Profile
@xuncheng
xuncheng / spec_has_many_through.rb
Created July 2, 2013 10:30
rspec testing has_many :through association
# video.rb
class Video < ActiveRecord::Base
has_many :categorizations
has_many :categories, through: :categorizations
end
# category.rb
class Category < ActiveRecord::Base
has_many :categorizations
has_many :videos, through: :categorizations
@xuncheng
xuncheng / dash2.rb
Created June 19, 2017 14:31
A cask for Dash 2.x
cask 'dash2' do
version '2.2.6'
sha256 'a7de5e00528611f86d1024164b1b5d536ce039344b6ca3c211918c7458f7e412'
url "https://kapeli.com/downloads/v#{version.major}/Dash.zip"
name 'Dash'
homepage 'https://kapeli.com/dash'
auto_updates true
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems:
@xuncheng
xuncheng / model.template.txt
Last active March 1, 2017 07:55 — forked from joshuapinter/model.template.txt
Rails Model Template
class Klass < ActiveRecord::Base
#
# Attributes
#
#
# Constants & Enums
#
@xuncheng
xuncheng / elasticsearch_cleaner.rb
Last active January 23, 2017 06:15
fix `Elasticsearch::Transport::Transport::Errors::ServiceUnavailable: [503]`
# fixed: Elasticsearch::Transport::Transport::Errors::ServiceUnavailable: [503]
# https://hugotunius.se/2015/06/22/elasticsearch-falkiness-in-tests.html
# https://medium.com/@thetron/dealing-with-503-errors-when-testing-elasticsearch-integration-in-rails-ec7a5f828274#.ex6quza03
# https://github.com/elastic/elasticsearch-ruby/issues/181#issuecomment-112781924
# spec/support/elasticsearch_cleaner.rb
RSpec.configure do |config|
config.before(:each, :es => true) do
ElasticsearchModel.create_index!(:force => true)
ElasticsearchModel.client.cluster.health(wait_for_status: 'yellow')
@xuncheng
xuncheng / database_cleaner.rb
Last active January 3, 2016 07:28
Database Cleaner
# spec/support/database_cleaner.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
@xuncheng
xuncheng / gravatar_for.rb
Created January 4, 2014 14:18
Generates the Gravatar for given user.
# Returns the Gravatar (http://gravatar.com) for the given user.
def gravatar_for(user, options = {size: 40})
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.full_name)
end
@xuncheng
xuncheng / enter_navigation.js
Created January 2, 2014 14:07
Keyboard Navigation between Input Fields using JQuery: enter key
// Need dom_element_find.js
// https://gist.github.com/xuncheng/8219581
$("input[type=text]").bind("keydown", function(event) {
var target;
if (event.keyCode === 13) {
target = $("input[type=text]").elementAfter(this);
if (target) {
target.select().focus();
}
event.preventDefault(); // 禁用提交事件
@xuncheng
xuncheng / dom_element_find.js
Created January 2, 2014 14:02
Find the next or the previous instance for a given selected range
jQuery.fn.elementAfter = function(other) {
for(i = 0; i < this.length - 1; i++) {
if (this[i]() == other) {
return jQuery(this[i + 1]());
}
}
};
jQuery.fn.elementBefore = function(other) {
if (this.length > 0) {
@xuncheng
xuncheng / key_navigation.js
Last active January 1, 2016 23:58
Keyboard Navigation between Input Fields using JQuery: up/down key
// Need dom_element_find.js
// https://gist.github.com/xuncheng/8219581
jQuery(document).ready(function(){
$('input[type=text]()').bind('keyup', function(evt) {
if (evt.keyCode == 40) {
// down key
var target = $('input[type=text]()').elementAfter(this)
if (target) {
target.focus();
}