Skip to content

Instantly share code, notes, and snippets.

View mlomnicki's full-sized avatar

Michał Łomnicki mlomnicki

View GitHub Profile
def find_family_view_for_state_with_caching(state, action_view)
return find_family_view_for_state(state, action_view) unless ActionController::Base.perform_caching
key = "#{state}/#{action_view.template_format}"
self.class.state2view_cache[key] ||= find_family_view_for_state(state, action_view)
end
@mlomnicki
mlomnicki / list_missing_indexes.rb
Created September 23, 2009 13:37
List missing indexes
c = ActiveRecord::Base.connection
c.tables.collect do |t|
columns = c.columns(t).collect(&:name).select {|x| x.ends_with?("_id" || x.ends_with("_type"))}
indexed_columns = c.indexes(t).collect(&:columns).flatten.uniq
unindexed = columns - indexed_columns
unless unindexed.empty?
puts "#{t}: #{unindexed.join(", ")}"
end
end
@mlomnicki
mlomnicki / active_record_alias.rb
Created April 2, 2010 20:34
active_record_protected
# script/generate model Post content:text
class Post < ActiveRecord::Base
alias :full_content :content
end
@mlomnicki
mlomnicki / ruby_private.rb
Created April 2, 2010 20:27
private method with + w/o self
class A
private
def foo
puts "Private method called"
end
end
class B < A
@mlomnicki
mlomnicki / respond_to.rb
Created April 2, 2010 21:41
respond_to? + protected
class A
protected
def foo
end
private
def bar
end
@mlomnicki
mlomnicki / local_vars.rb
Created April 14, 2010 11:22
ruby local varibales
puts local_variables.inspect
y = 4
@mlomnicki
mlomnicki / modules_order.rb
Created April 19, 2010 15:27
Modules order
module ModOne
def hello
"hello1"
end
end
module ModTwo
def hello
"hello2"
end
@mlomnicki
mlomnicki / string_to_proc.rb
Created April 20, 2010 00:11
String to proc
class String
def to_proc
eval "Proc.new { |*args| args.first#{self} }"
end
end
[1,2,3].collect(&'+5')
@mlomnicki
mlomnicki / self_extend.rb
Created April 19, 2010 23:30
Self extending module
module Utils
def foo
puts "bar"
end
def bar
puts "foo"
end
@mlomnicki
mlomnicki / block_comments.rb
Created April 19, 2010 23:48
Block comments
puts "Before block comment"
=begin
The method above prints a string to stdout
Pretty awesome!
And the method below does the same!
=end
puts "After block comment"