Skip to content

Instantly share code, notes, and snippets.

View chucai's full-sized avatar

He Xudong chucai

  • 北京
View GitHub Profile
@chucai
chucai / main.rb
Last active January 1, 2016 23:19 — forked from unnitallman/gist:944011
Rails: Single ActiveRecord File For Sqlite3
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false # 注释掉,如果用 rails 4.0
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => ":memory:"
)
class SimpleLinearRegression
def initialize(xs, ys)
@xs, @ys = xs, ys
if @xs.length != @ys.length
raise "Unbalanced data. xs need to be same length as ys"
end
end
def y_intercept
mean(@ys) - (slope * mean(@xs))
@chucai
chucai / dynamic_method_spec.rb
Created December 20, 2013 03:19 — forked from skwp/dynamic_method_spec.rb
Rspec: dynamic method spec test
require 'spec/support/grep_matcher'
describe do
disallow_presence_of pattern: "send(.*#",
location: "app/",
description: "Do not use dynamic method invocations",
failure: "Please change dynamic method call to something more sane."
end
@chucai
chucai / tracepoint_middleware.rb
Last active October 10, 2019 06:32 — forked from mattetti/tracepoint_middlware.rb
Rails: TracePoint rails request
# update application.rb
# config.middleware.insert_before(ActionDispatch::Static, TracePoint::Middleware)
class TracePoint
class Middleware
def initialize(app)
@app = app
end
def call(env)
@chucai
chucai / rails_template.rb
Last active October 13, 2015 03:08 — forked from tonytonyjan/tj_rails_template.rb
Rails: template
# Usage:
# > rails new myapp -m https://gist.github.com/raw/4010690/d9106a1c3af695d1ed4ef8d16e3c8a06b5e3c7f3/tj_rails_template.rb
config = {}
puts "Gems:"
config[:install_devise_cancan_rolify] = yes? "install devise, cancan, rolify?"
config[:install_form_helpers] = yes? "install simple_form, dynamic_form, nested_form?"
config[:install_bootstrap_tjstyle] = yes? "install bootstrap, tjstyle?"
config[:install_rspec_spork] = yes? "install rspec, spork?"
@chucai
chucai / README.markdown
Created September 28, 2012 05:47 — forked from hooopo/README.markdown
Rails: nav_link helper for adding 'selected' class to navigation elements

#Behold, the nav_link:

The nav_link helper works just like the standard Rails link_to helper, but adds a 'selected' class to your link (or its wrapper) if certain criteria are met. By default, if the link's destination url is the same url as the url of the current page, a default class of 'selected' is added to the link.

For full usage details, see: http://viget.com/extend/rails-selected-nav-link-helper

Drop nav_link_helper.rb into app/helpers in your Rails 3.x app and enjoy.

@chucai
chucai / .railsrc
Created August 4, 2012 02:08 — forked from janlelis/.railsrc
13 Rails-specific hints for your rails 3 console.
# .railsrc for Rails 3, encoding: utf-8
# see http://rbjl.net/49-railsrc-rails-console-snippets
if !Rails.application then warn "Rails isn't loaded, yet... skipping .railsrc" else
# # #
def ripl?; defined?(Ripl) && Ripl.instance_variable_get(:@shell); end
# # #
# loggers
@chucai
chucai / faye.rb
Created July 19, 2012 02:52 — forked from betamatt/faye.rb
Faye monit wrapper
#!/usr/env ruby
base_dir = File.expand_path("../..", __FILE__)
NAME="faye"
PID="#{base_dir}/tmp/pids/#{NAME}.pid"
COMMAND="bundle exec rackup -s thin -E production -p 3001 faye.ru"
case ARGV[0]
when "start"
# See http://m.onkey.org/running-rails-performance-tests-on-real-data
# START : HAX HAX HAX
# Load Rails environment in 'test' mode
RAILS_ENV = "test"
require File.expand_path('../../config/environment', __FILE__)
# Re-establish db connection for 'performance' mode
silence_warnings { RAILS_ENV = "performance" }
ActiveRecord::Base.establish_connection
@chucai
chucai / description
Created July 14, 2012 02:19 — forked from irrationalidiot/description
Cucumber, rspec and spork setup using watchr for rails
Gems:
gem 'cucumber'
gem 'cucumber-rails'
gem 'rspec'
gem 'rspec-rails'
gem 'capybara'
gem 'database_cleaner'
gem 'spork'
gem 'watchr'