Skip to content

Instantly share code, notes, and snippets.

@the8472
the8472 / gist:1952018
Created March 1, 2012 18:38
Make CanCan 1.6 + arel 2.0 work with multiple scopes
require "cancan/model_adapters/active_record_adapter"
class CanCan::ModelAdapters::ActiveRecordAdapter
# disable the error when multiple conditions with scopes are used
def override_scope
return false
end
# convert where-clauses from relations into sql strings which will be merged by the cancan merge_conditions logic
@the8472
the8472 / gist:2312198
Created April 5, 2012 16:06
MonkeyPatch CanCan to use OUTER JOINs (requires squeel)
require "cancan/model_adapters/active_record_adapter"
class CanCan::ModelAdapters::ActiveRecordAdapter
# use squeel's symbol-predicates to convert all joins into outer joins
# see https://github.com/ryanb/cancan/issues/374
def clean_joins(joins_hash)
joins = []
joins_hash.each do |name, nested|
name = name.outer if nested.is_a? Hash
joins << (nested.empty? ? name : {name => clean_joins(nested)})
@the8472
the8472 / cancan_initializer.rb
Created June 18, 2012 12:59
MonkeyPatch for cancan#646
require "cancan/model_adapters/active_record_adapter"
class CanCan::ModelAdapters::ActiveRecordAdapter
def merge_conditions(sql, conditions_hash, behavior)
conditions_hash = Squeel::Nodes::Literal.new(conditions_hash) if conditions_hash.is_a? String
if conditions_hash.blank?
Squeel::Nodes::Literal.new(behavior ? true_sql : false_sql)
else
case sql
require 'active_record'
class ReferencedModelA < ActiveRecord::Base; end
class ReferencedModelB < ActiveRecord::Base; end
class ReferencedModelC < ActiveRecord::Base; end
class ModelA < ActiveRecord::Base
has_many :referenced_model_a
end
@the8472
the8472 / cancan_squeel_extension.rb
Created November 8, 2012 16:11
outerjoin fixes + rails 3.2.8 compat + scope merging for cancan, using squeel
# encoding: UTF-8
require "cancan/model_adapters/active_record_adapter"
class CanCan::Rule
def unmergeable?
false
end
end
module Mhmr::DescendantsTracker
def descendants_and_self
[self].concat self.descendants
end
end
ActiveSupport::DescendantsTracker.send(:include,Mhmr::DescendantsTracker)
jruby:
@the8472
the8472 / minimal testing up
Created December 12, 2012 16:28
Rails startup performance tuning with jruby. Note that those settings are *not* for optimal steady state operation.
### ruby 1.9.3 + railsexpress patches + GC tuning + -march=native -O3
time ruby script/rails r 'Post.count'
real 0m1.008s
user 0m0.820s
sys 0m0.180s
### jruby 1.7.2-dev 2012-12-12 + java 64bit 1.8.0-ea-b67 hotspot 25.0-b11
@the8472
the8472 / assembly test1()
Created December 31, 2012 16:02
opto test volatile field access
Compiled method (c2) 593 8 Test2::test1 (46 bytes)
total in heap [0x029af8c8,0x029b0184] = 2236
relocation [0x029af99c,0x029af9b8] = 28
main code [0x029af9c0,0x029afd00] = 832
stub code [0x029afd00,0x029afd10] = 16
oops [0x029afd10,0x029afd14] = 4
metadata [0x029afd14,0x029afd18] = 4
scopes data [0x029afd18,0x029afddc] = 196
scopes pcs [0x029afddc,0x029b013c] = 864
dependencies [0x029b013c,0x029b0140] = 4
@the8472
the8472 / gist:4426366
Created January 1, 2013 10:20
volatile/non-volatile/unsafe read test
import java.lang.reflect.Field;
import java.security.SecureRandom;
import sun.misc.Unsafe;
public class Test2 {
static final Unsafe unsafe;
static final long offsetA;
static final long offsetB;
@the8472
the8472 / gist:4446959
Created January 3, 2013 20:32
stamped ivar updater for jruby
diff --git a/src/org/jruby/RubyBasicObject.java b/src/org/jruby/RubyBasicObject.java
index 2bb34e8..6de4bef 100644
--- a/src/org/jruby/RubyBasicObject.java
+++ b/src/org/jruby/RubyBasicObject.java
@@ -38,6 +38,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;