Skip to content

Instantly share code, notes, and snippets.

<h1>Post:</h1>
<h2><%= @post.title %></h2>
<div style="margin:0 250px 0 250px">
<pre>
<code class="<%= @post.language %>"><%= @post.source_code %></code>
</pre>
</div>
<%= raw(disqus_thread) %>

InteractiveCU is a simple interactive create and update for AR.

Examples:

Create:

ruby-1.9.2-p180 :007 > User
 => User(id: integer, name: string, created_at: datetime, updated_at: datetime) 
ruby-1.9.2-p180 :008 > User.interactive_create # or User.ic
[ -f $HOME/.profile ] && . $HOME/.profile
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=$HOME/.zsh_history
setopt append_history
setopt inc_append_history
setopt extended_history
setopt hist_find_no_dups
setopt hist_ignore_all_dups
def attributes
attrs = {}
attribute_names.each { |name| attrs[name] = read_attribute(name) }
attrs
end
#vs
def attributes
attribute_names.inject({}) { |attrs, name| attrs[name] = read_attribute(name); attrs }
@nashby
nashby / README.md
Created September 4, 2011 15:50
Gingerbread

##Gingerbread It's like very simple ActiveRecord for Sinatra but uses cookies instead of databases

Use cookies in Sinatra like a boss!

Example:

require './gingerbread'
require 'sinatra'
@nashby
nashby / gist:1231462
Created September 21, 2011 07:26
fix for broken :all_blank in rails
def nested_all_blank(attributes)
attributes.reject { |key, value| ['_destroy', :_destroy].include?(key) && [false, 0, 'f', 'false'].include?(value) }.all? do |_, value|
if value.is_a?(Hash)
nested_all_blank(value)
else
value.blank?
end
end
end
@nashby
nashby / gist:1303163
Created October 21, 2011 05:30
enter to prompt in capybara
def suppress_prompt(text)
page.evaluate_script 'window.original_prompt_function = window.prompt;'
page.evaluate_script "window.prompt = function(msg) { return '#{text}'; }"
end
def recover_prompt
page.evaluate_script('window.prompt = window.original_prompt_function;')
end
@nashby
nashby / DRYed
Created November 15, 2011 20:54
rubinius sorting benchmarks
~/projects/rubinius (git)-[master] % bin/benchmark benchmark/core/array/bench_sort.rb 23:46
=== bin/rbx ===
sort strings ascending
4244.9 (±0.5%) i/s - 21284 in 5.014169s (cycle=313)
array sort! strings ascending
4207.8 (±0.9%) i/s - 21267 in 5.054594s (cycle=417)
sort strings descending with block
1397.7 (±0.6%) i/s - 7124 in 5.096934s (cycle=137)
sort! strings descending with block
1387.8 (±0.9%) i/s - 6987 in 5.035176s (cycle=137)
~/projects/rails/actionpack (git)-[master] % git diff 22:23
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 1a64b12..58ca5de 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -103,11 +103,6 @@ module ActionView
include FormTagHelper
include UrlHelper
- # Converts the given object to an ActiveModel compliant one.
@nashby
nashby / gist:1482754
Created December 15, 2011 20:33
rails issue #3923
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index ba882be..b72aaa2 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -34,7 +34,7 @@ module ActiveRecord
unless @where_values.empty?
# Remove duplicates, last one wins.
seen = Hash.new { |h,table| h[table] = {} }
- merged_wheres = merged_wheres.reverse.reject { |w|
+ merged_wheres = merged_wheres.uniq.reverse.reject { |w|